无法将Blazor WASM部署到Azure静态Web应用生产槽,始终部署到预览槽
问题描述
- 尝试通过Visual Studio和GitHub CI/CD将Blazor WASM发布到Azure静态Web应用生产环境,但所有发布操作均部署至预览环境
- 已尝试手动编辑yml文件(结果创建了名为
production的新预览槽)、GitHub强制合并(未触发生产部署),均无法部署到生产槽

原yml文件:
name: Build and deploy .NET Core application to Web App HspBlazorWasmClient on: pull_request: types: - opened - synchronize - reopened - closed branches: - master push: branches: - master env: AZURE_STATIC_WEBAPP_NAME: HspBlazorWasmClient APP_DIRECTORY: HspBlazorWasm.Client #API_DIRECTORY: HspBlazorWasm OUTPUT: wwwroot DEPLOYMENT_ENVIRONMENT: Production jobs: build_and_deploy_job: if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed') name: Build and Deploy Job runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: submodules: true - id: builddeploy name: Build And Deploy uses: Azure/static-web-apps-deploy@v1 with: azure_static_web_apps_api_token: ${{ secrets.HspBlazorWasmClient_SPN }} repo_token: ${{ secrets.GITHUB_TOKEN }} action: upload deployment_environment: ${{env.DEPLOYMENT_ENVIRONMENT}} app_location: ${{ env.APP_DIRECTORY }} #api_location: ${{ env.API_DIRECTORY }} output_location: ${{ env.OUTPUT }} close_pull_request_job: if: github.event_name == 'pull_request' && github.event.action == 'closed' name: Close Pull Request Job runs-on: ubuntu-latest steps: - id: closepullrequest name: Close Pull Request uses: Azure/static-web-apps-deploy@v1 with: azure_static_web_apps_api_token: ${{ secrets.HspBlazorWasmClient_SPN }} action: close repo_token: ${{ secrets.GITHUB_TOKEN }}
解决方案
核心问题:Azure静态Web Apps的生产槽不需要指定deployment_environment参数,当显式设置该参数时,系统会将其识别为预览环境——哪怕名称是Production,这就是你创建出同名预览槽的原因。
具体修改步骤
- 移除全局环境变量
DEPLOYMENT_ENVIRONMENT: Production - 拆分任务逻辑,分别处理
push到master分支(生产部署)和pull_request(预览部署)的场景:- Push到master时,不配置
deployment_environment,直接部署到生产槽 - PR事件触发时,指定带PR编号的预览环境名称,确保每个PR对应独立预览槽
- Push到master时,不配置
修改后的完整yml文件
name: Build and deploy .NET Core application to Web App HspBlazorWasmClient on: pull_request: types: - opened - synchronize - reopened - closed branches: - master push: branches: - master env: AZURE_STATIC_WEBAPP_NAME: HspBlazorWasmClient APP_DIRECTORY: HspBlazorWasm.Client #API_DIRECTORY: HspBlazorWasm OUTPUT: wwwroot jobs: build_and_deploy_job: if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed') name: Build and Deploy Job runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: submodules: true - id: builddeploy name: Build And Deploy uses: Azure/static-web-apps-deploy@v1 with: azure_static_web_apps_api_token: ${{ secrets.HspBlazorWasmClient_SPN }} repo_token: ${{ secrets.GITHUB_TOKEN }} action: upload # 仅在PR事件时指定预览环境,自动关联PR编号 ${{ if eq(github.event_name, 'pull_request') }}: deployment_environment: pr-${{ github.event.number }} app_location: ${{ env.APP_DIRECTORY }} #api_location: ${{ env.API_DIRECTORY }} output_location: ${{ env.OUTPUT }} close_pull_request_job: if: github.event_name == 'pull_request' && github.event.action == 'closed' name: Close Pull Request Job runs-on: ubuntu-latest steps: - id: closepullrequest name: Close Pull Request uses: Azure/static-web-apps-deploy@v1 with: azure_static_web_apps_api_token: ${{ secrets.HspBlazorWasmClient_SPN }} action: close repo_token: ${{ secrets.GITHUB_TOKEN }} # 关闭PR时清理对应预览环境 deployment_environment: pr-${{ github.event.number }}
效果说明
- 生产部署:代码push到master分支时,Azure SWA会自动部署到生产槽
- 预览部署:每个Pull Request会生成独立的预览环境(名称格式为
pr-xxx),关闭PR时会自动清理该预览环境
内容的提问来源于stack exchange,提问作者LHill
相关产品推荐
相关产品推荐

