{"meta":{"title":"部署到 Azure Static Web App","intro":"了解如何将 Web 应用部署到Azure静态 Web 应用作为持续部署（CD）工作流的一部分。","product":"GitHub Actions","breadcrumbs":[{"href":"/zh/actions","title":"GitHub Actions"},{"href":"/zh/actions/how-tos","title":"操作方法"},{"href":"/zh/actions/how-tos/deploy","title":"部署"},{"href":"/zh/actions/how-tos/deploy/deploy-to-third-party-platforms","title":"部署到第三方平台"},{"href":"/zh/actions/how-tos/deploy/deploy-to-third-party-platforms/azure-static-web-app","title":"Azure静态 Web 应用"}],"documentType":"article"},"body":"# 部署到 Azure Static Web App\n\n了解如何将 Web 应用部署到Azure静态 Web 应用作为持续部署（CD）工作流的一部分。\n\n## 先决条件\n\n在创建 GitHub Actions 工作流程之前，首先需要完成以下设置步骤：\n\n1. 使用部署源的“其他”选项创建Azure静态 Web 应用。 有关详细信息，请参阅 [Quickstart：在 Azure 门户中生成第一个静态站点](https://docs.microsoft.com/azure/static-web-apps/get-started-portal)Azure文档中。\n\n1. 使用静态 Web 应用部署令牌的值创建名为 `AZURE_STATIC_WEB_APPS_API_TOKEN` 的机密。 有关如何查找部署令牌的详细信息，请参阅 Azure 文档中的 [Azure Static Web Apps 重置部署令牌](https://docs.microsoft.com/azure/static-web-apps/deployment-token-management)。\n\n## 创建工作流程\n\n完成先决条件后，可以继续创建工作流程。\n\n以下示例工作流演示了，当推送到 `main` 分支或针对 `main` 的拉取请求被打开、同步或重新打开时，如何生成和部署 Azure 静态 Web 应用。 当面向 `main` 的拉取请求关闭时，工作流还会破坏相应的预生产部署。\n\n在工作流 `env` 键下，更改以下值：\n*               `APP_LOCATION` 到客户端代码的位置\n* 请将 `API_LOCATION` 指向 API 源代码的位置。 如果 `API_LOCATION` 不相关，则可以删除该变量以及使用它的行。\n*               `OUTPUT_LOCATION` 到客户端代码构建输出的位置\n\n有关这些值的详细信息，请参阅 Azure 文档中的 [Azure Static Web Apps 构建配置](https://docs.microsoft.com/azure/static-web-apps/build-configuration?tabs=github-actions)。\n\n```yaml copy\n# 此工作流使用未经 GitHub 认证的操作。\n# 它们由第三方提供，并受\n# 单独的服务条款、隐私政策和支持\n# 文档。\n\n# GitHub 建议将操作固定到提交 SHA。\n# 若要获取较新版本，需要更新 SHA。\n# 还可以引用标记或分支，但该操作可能会更改而不发出警告。\n\nname: Deploy web app to Azure Static Web Apps\n\nenv:\n  APP_LOCATION: \"/\" # location of your client code\n  API_LOCATION: \"api\" # location of your api source code - optional\n  OUTPUT_LOCATION: \"build\" # location of client code build output\n\non:\n  push:\n    branches:\n      - main\n  pull_request:\n    types: [opened, synchronize, reopened, closed]\n    branches:\n      - main\n\npermissions:\n  issues: write\n  contents: read\n  pull-requests: write\n\njobs:\n  build_and_deploy:\n    if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')\n    runs-on: ubuntu-latest\n    name: Build and Deploy\n    steps:\n      - uses: actions/checkout@v6\n        with:\n          submodules: true\n      - name: Build And Deploy\n        uses: Azure/static-web-apps-deploy@1a947af9992250f3bc2e68ad0754c0b0c11566c9\n        with:\n          azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}\n          repo_token: ${{ secrets.GITHUB_TOKEN }}\n          action: \"upload\"\n          app_location: ${{ env.APP_LOCATION }}\n          api_location: ${{ env.API_LOCATION }}\n          output_location: ${{ env.OUTPUT_LOCATION }}\n\n  close_pull_request:\n    if: github.event_name == 'pull_request' && github.event.action == 'closed'\n    runs-on: ubuntu-latest\n    name: Close Pull Request\n    steps:\n      - name: Close Pull Request\n        uses: Azure/static-web-apps-deploy@1a947af9992250f3bc2e68ad0754c0b0c11566c9\n        with:\n          azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}\n          action: \"close\"\n```\n\n## 其他阅读材料\n\n* 对于原始工作流模板，请参阅 GitHub Actions `starter-workflows` 存储库中的 [`azure-staticwebapp.yml`](https://github.com/actions/starter-workflows/blob/main/deployments/azure-staticwebapp.yml)。\n* 用于部署 Web 应用程序的是官方的 Azure [`Azure/static-web-apps-deploy`](https://github.com/Azure/static-web-apps-deploy) 操作。\n* 有关部署到 Azure 的 GitHub 操作工作流的更多示例，请参阅 [actions-workflow-samples](https://github.com/Azure/actions-workflow-samples) 存储库。"}