运行 不同 stages/pipelines 不同的 azure devops 触发器

Run different stages/pipelines for different azure devops triggers

我最近开始使用 Azure 管道(具有 GitHub 操作经验)。我正在尝试 运行 基于不同触发器的阶段,例如:

根据我从 Azure 管道的文档中读到的内容,这是不可能的。如果我理解正确的话,我只能在我的 azure-pipelines.yml 值之上定义 1 个触发值。这是真的吗?还是我在监督什么?在 github 个操作中,我知道这可以通过创建不同的工作流(lint-workflow、test-workflow 等)来解决,每个工作流都有一个单独的触发器。

在 Azure DevOps 中,Trigger 字段用于触发整个管道。所以你只能在 yaml 文件的顶部定义 1 个触发值。请参阅此文档:Triggers

根据您的要求,您需要控制阶段的 运行ning。

我建议您可以在每个阶段添加条件。请参阅有关 Condition.

的文档

这是一个示例:

trigger:
- '*'

pool:
  vmImage: windows-latest

stages:
  - stage: linter_stage
    condition: eq(variables['Build.Reason'], 'IndividualCI')
    jobs:
      - job: A
        steps:
          xxx

  - stage: test_stage
    condition: eq(variables['Build.Reason'], 'PullRequest')
    jobs:
      - job: B
        steps:
          xxx
  - stage: deployment_stage
    condition:  and(eq(variables['Build.Reason'], 'IndividualCI'), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
    jobs:
      - job: C
        steps:
          xxx

解释:

  1. 在 YAML 的顶部,您需要将 Trigger 定义为 * 以确保任何更改都会触发管道。

  2. 您可以使用 build.reasonBuild.SourceBranch 变量来确定阶段是否需要 运行

这是关于 predefined variable 的文档。

结果: