【问题标题】:Azure YAML - Dynamically set stage/job execution order with variableAzure YAML - 使用变量动态设置阶段/作业执行顺序
【发布时间】:2021-01-23 00:00:24
【问题描述】:

我正在尝试创建一个包含三个阶段的 yaml 管道,这些阶段可以根据拉取请求源分支以不同的顺序执行。例如:

如果 var = 默认则

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

如果 var != 默认则

  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 模板表达式变量(我认为)。这甚至可能吗?

【问题讨论】:

    标签: azure-devops yaml azure-pipelines azure-pipelines-yaml


    【解决方案1】:

    ${{ 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
    

    根据条件选择要运行的阶段顺序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-16
      • 2020-06-10
      • 2015-06-30
      • 1970-01-01
      • 2019-09-21
      • 2023-03-12
      • 1970-01-01
      • 2022-09-23
      相关资源
      最近更新 更多