How to enable approvals with Azure Devops YAML pipelines?

Sometimes, our CICD pipelines need to reflect our IT organization governance and continuous delivery is not always possible. Meaning we might need to add approvals between the build and different deployment stages.

In this post I will explain how to achieve this task with Azure Devops YAML pipelines.

Usually, Azure Devops pipelines environments are used to execute deployment tasks on a set of virtual machines or kubernetes containers attached to a specific environment. However, it is possible to create environments without any resource and use them only for the Approvals and checks features.

Create the environment

The first thing we need to do is to create the different environments we need. For example, let’s create an environment called production-environment.

To achive this task:

  • Go to the pipelines menu
  • Select the environments menu
  • Click on the Create environment button
  • Then define a name, for example production-environment and select None for the resource type.

Add the approval check

Once the environment is created, we need to enable the approval checks.

  • Go to the pipelines menu
  • Select the environments menu and click on the environment we just created

  • Then go to the top right menu and select Approvals and checks

  • Add the approval and specify the approvers

Use the environment in your pipeline

Now, it is possible to use this environment in the Azure Devops YAML pipeline.

trigger:
- none

pool:
  name: 'MyCICDPool'

stages:
- stage: Build
  jobs:
  - job: Build
    workspace:
      clean: all
    steps:
    - script: |
        echo "Hello Build"
      displayName: HelloBuild

- stage: PRD
  displayName: Deploy PRD
  jobs:
  - deployment: PRD
    environment: production-environment
    displayName: Deploy on production
    strategy:
        runOnce:
          deploy:
            steps:
            - script: |
                echo "Hello production"
              displayName: HelloProduction

After the Build stage, the pipeline will wait for the manual approval to continue.

Conclusion

We covered in this post how to use approvals with Azure Devops YAML pipelines. Note, that not only manual approvals can be configured. You can use many checks such as Invoke Azure Function, Invoke REST API , Branch control, …