0

I have two pipelines - one for CI and the other for CD. Want to trigger the CD pipeline after CI pipeline has completely finished. After setting up the triggers (through YAML), both of my pipelines get triggered together so CD finishes even before CI has completed.

How can I trigger the CD pipeline, only after the CI has finished off?

My CI pipeline is as follows:

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"'

My CD pipeline is as follows:

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'

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
mrsauravsahu
  • 2,046
  • 2
  • 15
  • 21

3 Answers3

1

One way would be to create a tag at the end of the CI and CD can be triggered by this tag.

ADO CI pipeline sample code :

trigger:
  - main

ADO CD pipeline sample code :

trigger:
  tags:
    include:
      - v*
Rohit
  • 439
  • 8
  • 18
  • This is a great alternative. Trying it now! Would love to know what's wrong with the completion trigger though. Thanks! – mrsauravsahu Jun 07 '21 at 10:19
  • I faced a similar issue with pipeline triggering another pipeline approach (as given here - https://learn.microsoft.com/en-us/azure/devops/pipelines/process/pipeline-triggers?view=azure-devops#combining-trigger-types). The code change always triggers both CI and CD pipelines. Switching to a tag based trigger solved it for me. Alternatively you can try the solution given here https://stackoverflow.com/a/60835195/1153885 to see if it works for you. – Rohit Jun 07 '21 at 10:40
  • Thanks! The above question helped. I've added it as an answer. – mrsauravsahu Jun 07 '21 at 10:56
1

One of the ways could be triggering your CD pipeline on a new version release. So, the flow of events can happen this way:

  • Master merge triggers CI => which would run the optional task of generating a new version
  • CD pipeline is triggered when a version is released
trigger:
  tags:
    include:
      - v*
0

Adding the none trigger to the CD pipeline worked. Thanks to Rohit for the link to similar question - 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
...
mrsauravsahu
  • 2,046
  • 2
  • 15
  • 21