问题描述
我有两个管道 - 一个用于 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'
解决方法
一种方法是在 CI 的末尾创建一个标签,这个标签可以触发 CD。
ADO CI 管道示例代码:
trigger:
- main
ADO CD 管道示例代码:
trigger:
tags:
include:
- v*
,
其中一种方法可能是在新版本发布时触发您的 CD 管道。所以,事件流可以这样发生:
- 主合并触发 CI => 它将运行生成新版本的可选任务
- 发布版本时会触发 CD 管道
trigger:
tags:
include:
- v*
,
向 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
...