You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Static Web App发布任务未创建预览环境的配置咨询

问题解决:Azure Static Web App PR分支部署到生产环境而非预览环境

问题原因

AzureStaticWebApp@0任务的production_branch参数匹配的是常规分支名称(如develop、feature/*),但PR触发的流水线中,Build.SourceBranch的值是refs/pull/xxx/merge这种特殊格式,任务无法识别这是非生产分支,因此直接部署到了生产环境。

解决方案

方案1:通过动态变量指定部署环境

利用Azure DevOps的流水线变量,根据构建原因判断是否为PR,动态设置deployment_environment参数:

steps:
- task: AzureStaticWebApp@0
  displayName: 'Static Web App: '
  inputs:
    workingDirectory: '$(System.DefaultWorkingDirectory)/my-portal'
    app_location: /
    skip_app_build: true
    skip_api_build: false
    is_static_export: false
    verbose: true
    azure_static_web_apps_api_token: 'token'
    production_branch: develop
    # 若为PR构建,指定预览环境名称;否则留空使用生产环境
    deployment_environment: $[if(eq(variables['Build.Reason'], 'PullRequest'), 'pr-preview-${System.PullRequest.PullRequestId}', '')]

这个配置会为每个PR生成唯一的预览环境(如pr-preview-1028),非PR的develop分支则部署到生产环境。

方案2:基于PR源分支判断

如果需要严格匹配源分支是否为生产分支,可以提取PR的源分支变量System.PullRequest.SourceBranch进行判断:

variables:
  isProductionBranch: $[eq(variables['System.PullRequest.SourceBranch'], 'refs/heads/develop')]

steps:
- task: AzureStaticWebApp@0
  displayName: 'Static Web App: '
  inputs:
    workingDirectory: '$(System.DefaultWorkingDirectory)/my-portal'
    app_location: /
    skip_app_build: true
    skip_api_build: false
    is_static_export: false
    verbose: true
    azure_static_web_apps_api_token: 'token'
    production_branch: develop
    deployment_environment: $[if(not(variables.isProductionBranch), 'pr-preview-${System.PullRequest.PullRequestId}', '')]

这种方式确保只有源分支是develop的PR才部署到生产,其他PR都走预览环境。

额外检查点

  • 确认Azure门户中你的Static Web App已开启预览环境功能(默认开启,可在"部署管理"页面查看);
  • 确保使用的AzureStaticWebApp@0任务是最新版本,可在Azure DevOps市场检查更新。

内容的提问来源于stack exchange,提问作者ruffen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 01:40:44