首页 > 解决方案 > 根据签出的分支/标签使用不同的 azure 管道模板

问题描述

我们现在正在组织中迁移到 Azure 管道,我们将使用模板机制集中更改构建逻辑。

请参阅下面的当前示例。

name: $(Date:yyyyMMddHHss).$(SourceBranchName).$(Build.SourceVersion)

resources:
  repositories:
  - repository: build_resources  # id for reuse in below script code
    type: git  # use 'git' for Azure git repository
    name: build-resources  # repository name
    ref: refs/tags/1.1.2

jobs:
- template: build/azure-pipelines-template.yml@build_resources  # Template reference
  parameters:
    projectfolder: 'blabla'

这将始终检查构建模板的特定版本。

我们现在想实现以下几点:当签出分支是“开发”时,将始终使用最新版本的构建模板。但是当我们说一个“发布”分支甚至一个标签正在被构建时,一个固定版本的构建模板应该被使用。

我的想法是在 YAML 中使用一些字符串处理,然后选择不同的存储库资源。但我不确定这在 YAML 中是否可行,以及是否有更好的方法可以推荐给我。

谢谢

标签: azure-pipelines

解决方案


您可以根据条件从管道 YAML 调用不同的模板。在此示例中,stable.yml YAML 将在运行分支测试时运行。

稳定的.yml

steps:

- script: echo "stable.yml"

实验.yml

steps:

- script: echo "experimental.yml"

YAML 代码:

steps:

- ${{ if eq(variables['Build.SourceBranchName'], 'develop') }}:

  - template: experimental.yml

- ${{ if eq(variables['Build.SourceBranchName'], 'release') }}:

  - template: stable.yml

请参阅此文档以获取更多详细信息。


推荐阅读