基于分支的条件构建,用于在阶段下使用不同模板的多阶段管道

conditional build based on branch for multi stage pipeline using different templates under stages

如何通过分支触发使用"stages"下的特定模板?

触发器:

 branches

   include:

     - ci

     - prod

阶段:

试过上述条件但没有成功。我得到 "unexpected value condition"。感谢任何帮助

***** 尝试了一种解决方案,将条件作为参数传递给模板:

阶段:

获得"unexpected parameter condition"

流水线结构:

master.yml(包含运行时参数) 阶段:

模板:ci.yml

参数:

条件:and(成功(), eq(变量['Build.SourceBranch'], 'refs/heads/ci'))

模板:prod.yml

参数:

条件:and(成功(), eq(变量['Build.SourceBranch'], 'refs/heads/prod'))

ci.yml

阶段:

prod.yml

阶段:

How to trigger by branch to use specific template under "stages"?

要解决此问题,我们可以在作业级别添加 条件, 如:

stages:
- stage: Test1
  jobs:
  - job: ci
    displayName: ci
    pool:
      name: MyPrivateAgent
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/ci'))
    steps:
        - template: ci.yml

  - job: prod
    displayName: prod
    pool:
      name: MyPrivateAgent
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/prod'))
    steps:
        - template: prod.yml

查看文档 Specify conditions 了解更多详细信息。

另一方面,我们也可以将条件设​​置为模板yml的参数,例如:

- template: ci.yml
  parameters:
    doTheThing: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/ci'))

模板yml文件:

# template.yml
parameters:
  doTheThing: 'false'
steps:
- script: echo This always happens!
  displayName: Always
- script: echo Sometimes this happens!
  condition: ${{ parameters.doTheThing }}
  displayName: Only if true

您可以查看线程 YAML - Support conditions for templates 了解更多详细信息。

希望对您有所帮助。