Azure DevOps - YAML 管道触发器在源完成之前开始运行

时间:2021-06-07 08:46:16

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

我有两个管道 - 一个用于 CI,另一个用于 CD。想在 CI 管道完成后触发 CD 管道。设置触发器(通过 YAML)后,我的两个管道都会一起触发,因此 CD 甚至在 CI 完成之前就完成了。

如何在 CI 完成后触发 CD 管道?

我的 CI 管道如下:

pr:
  - develop
trigger:
  - develop


resources:
- repo: self

variables:
  tag: '$(Build.BuildId)'

stages:
- stage: Build
  displayName: Build image
  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: ubuntu-latest
    steps:
    - task: Docker@2
      displayName: Build an image
      inputs:
        command: build
        dockerfile: '$(Build.SourcesDirectory)/dockerfile'
        tags: |
          $(tag)
    
- stage: Push
  displayName: Push to Reg
  condition: and(succeeded(), in(variables['Build.SourceBranch'], 'refs/heads/develop'))
  jobs:
    - job: push
      steps:
      - task: Bash@3
        inputs:x
          targetType: 'inline'
          scriptx: 'echo "this is the push to reg task"'

我的CD管道如下:

resources:
  pipelines:
  - pipeline: cd
    source: pipeline-trigger-ci
    trigger:
      branches:
        include:
        - refs/heads/develop

pool:
  vmImage: ubuntu-latest

steps:
- script: echo Hello, world!
  displayName: 'Run a one-line script'

- script: |
    echo Add other tasks to build, test, and deploy your project.
    echo See https://aka.ms/yaml
  displayName: 'Run a multi-line script'

3 个答案:

答案 0 :(得分:1)

一种方法是在 CI 的末尾创建一个标签,这个标签可以触发 CD。

ADO CI 管道示例代码:

trigger:
  - main

ADO CD 管道示例代码:

trigger:
  tags:
    include:
      - v*

答案 1 :(得分:1)

其中一种方法可能是在新版本发布时触发您的 CD 管道。所以,事件流可以这样发生:

  • 主合并触发 CI => 它将运行生成新版本的可选任务
  • 发布版本时会触发 CD 管道
trigger:
  tags:
    include:
      - v*

答案 2 :(得分:0)

向 CD 管道添加 none 触发器有效。感谢 Rohit 提供类似问题的链接 - Azure Pipeline to trigger Pipeline using YAML

# needed to add this trigger
trigger: none

resources:
  pipelines:
  - pipeline: cd
    source: pipeline-trigger-ci
    trigger:
      branches:
        include:
        - refs/heads/develop
...