首页 > 解决方案 > 基于触发器的不同代理池或对同一 Azure Pipeline 的需求

问题描述

master我有一个用 YAML 编写的 Azure Pipeline,只要对分支进行更改,它就会从 CI 触发器运行。它也可以从拉取请求或用户针对任何分支手动触发。

由于使用了许多许可组件,因此构建master需要在特定代理上运行。其他构建没有,实际上我宁愿它们在其他代理上运行。

所以我的问题是,有没有办法根据触发构建的原因或构建正在构建的分支在 YAML 管道中指定不同的代理/池?我希望这是在管道中永久配置的行为,而不是要求用户更新他们希望在其他地方构建的每个分支上的 YAML。

在有关 pool/demands/condition 关键字的文档部分中,我看不到任何明显的内容。

标签: azure-devopsazure-pipelines

解决方案


我通过将stepsfor 作业放入模板来解决这个问题,然后在管道中创建一组具有不同condition条目的作业,以便我们可以demands根据这些条件进行设置。

骨架版本如下所示:

- stage: Build
    jobs:
    - job: TopicBranchAndPullRequestBuild
      condition: or(startsWith(variables['Build.SourceBranch'], 'refs/heads/topic'), startsWith(variables['Build.SourceBranch'], 'refs/pull'))
      displayName: 'Build topic Branch or Pull Request'

      pool:
        name: the-one-and-only-pool
        demands:
          - HasLicensedComponents -equals false
      steps:
        - template: build-template.yml

    - job: MasterAndReleaseBranchBuild
      condition: or(eq(variables['Build.SourceBranch'], 'refs/heads/master'), startsWith(variables['Build.SourceBranch'], 'refs/heads/release'))
      displayName: 'Build master or release Branch'
      pool:
        name: the-one-and-only-pool
        demands:
          - HasLicensedComponents -equals true
      steps:
        - template: build-template.yml

显然,此处给出的值仅作为示例,否则这就是我的工作。


推荐阅读