Azure YAML - 使用变量动态设置 stage/job 执行顺序

Azure YAML - Dynamically set stage/job execution order with variable

我正在尝试创建一个具有三个阶段的 yaml 管道,这些阶段可以根据 Pull Request 源分支以不同的顺序执行。例如:

如果 var = default 那么

  1. 阶段A
  2. B阶段
  3. C 阶段

如果 var != default 那么

  1. 阶段A
  2. C 阶段
  3. B阶段

这是我目前的情况: 变量:

- name: abcDependsOn
  ${{ if not(contains(variables['System.PullRequest.SourceBranch'], 'hotfix' )) }}:
    value: 'Publish'
  ${{ if contains(variables['System.PullRequest.SourceBranch'], 'hotfix' ) }}:
    value: 'DeployXYZ'
- name: xyzDependsOn
  ${{ if not(contains(variables['System.PullRequest.SourceBranch'], 'hotfix' )) }}:
    value: 'DeployABC'
  ${{ if contains(variables['System.PullRequest.SourceBranch'], 'hotfix' ) }}:
    value: 'Publish'

stages: 
- stage: Publish
  jobs:
  - job: publish

- stage: DeployABC
  dependsOn: ${{ variables.abcDependsOn }}
  jobs:
  - job: deployabc

- stage: DeployXYZ
  dependsOn: ${{ variables.xyzDependsOn }}
  jobs:
  - job: deployxyz

这是行不通的,因为 dependsOn 模板表达式变量在运行时表达式变量之前被求值(我认为)。这甚至可能吗?

${{ if not(contains(variables['System.PullRequest.SourceBranch'], 'hotfix' )) }}: 无法在编译时解析。因此,变量不能根据源分支条件动态设置其值

作为解决方法,您可以添加另一组 (DeployXYZ,DeployABC) 阶段并交换阶段顺序。设置如下:

stages: 
- stage: Publish
  jobs:
  - job: publish

- stage: DeployABC
  condition: contains(variables['System.PullRequest.SourceBranch'], 'hotfix' )) 
  jobs:
  - job: deployabc

- stage: DeployXYZ
  condition: contains(variables['System.PullRequest.SourceBranch'], 'hotfix' )) 
  jobs:
  - job: deployxyz

- stage: DeployXYZ
  condition: not(contains(variables['System.PullRequest.SourceBranch'], 'hotfix' ))
  jobs:
  - job: deployxyz

- stage: DeployABC
  condition: not(contains(variables['System.PullRequest.SourceBranch'], 'hotfix' ))
  jobs:
  - job: deployabc

Select阶段顺序运行根据条件