Friday 2 October 2020

Trigger Deployment YAML Pipeline Once YAML Build Completed

Now it is possible to implement multi stage pipelines with YAML facilitating the implementation of deployment pipelines as well with YAML instead of classic release pipelines in Azure DevOps. However, having build and deployment steps all together in a single pipeline script is not ideal as it looks bit not a nice implementation from my point of view. With the possibility of triggering another YAML pipeline based on completion of another YAML pipeline, it is possible to separate the Build and Deployment concerns into two different YAML scripts implementing two different pipelines. Let’s have a quick look at how we can trigger a YAML deployment pipeline based on another YAML build pipeline.

A pipeline can be set as trigger to another pipeline using the syntax below. The simplest implementation would be specifying the resource pipeline name as source (which is the name you set for the pipeline in the Azure DevOps portal). Then specify an identifier as pipeline to use any additional resources such as build artifacts available in the source pipeline (We will discuss usage of the pipeline artifacts in another pipeline with YAML in a next post). If the source pipeline is from a different team project you can specify the source team project as well. Then setting the trigger as true will allow the pipeline to be triggered once the source pipeline execution completes.



resources:
  pipelines:
  - pipeline: App_CI 
    projectYAML_CICD
    source: App.CI 
    trigger: true

Below is a full demo pipeline using a dummy stage to execute as Dev environment deployment based on the build pipeline completion. Build pipeline is an ASP.NET Core 3.1 web app build which is a YAML pipeline.

Source Pipeline - App.CI



CD Pipeline – App.CD

resources:
  pipelines:
  - pipeline: App_CI 
    projectYAML_CICD
    source: App.CI 
    trigger: true

stages:
- stage: Dev
  displayName: Deploy to Dev
  jobs:
  - job: DeployDev
    pool:
      vmImage: 'windows-latest'
    steps:
    - pwsh: |
        Write-Host "Deploy to Dev"
        Write-Host "We do nothing here"

In addition to just specifying to trigger when source pipeline is completed you can enhance the trigger with filters, such as to trigger the CD YAML pipeline if the build pipeline is based on a given branch pattern. It is possible to use include as well as exclude filters for branches.

  pipelines:
  - pipeline: App_CI 
    projectYAML_CICD
    source: App.CI 
    trigger:
      branches:
        include:
          - version/*
          - master

No comments:

Popular Posts