Thursday, 23 December 2021

Using templates in a Azure YAML pipeline

I was recently asked by a colleague how to use templates within a YAML pipeline, as they wanted to template a part of the deployment, this was because they have the option to deploy to different Azure App Services for testing.

To do this we created a simple dummy pipeline:

trigger:
- main

stages:
  - stage: Build
    jobs:
    - job: Compilation
      pool:
        vmImage: ubuntu-latest

      steps:
      - script: echo Build build build!
        displayName: 'Compile code'

  - stage: Test01
    jobs:
    - job: DeployToTest01
      pool:
        vmImage: ubuntu-latest

      steps:
      - script: echo Steps to deploy to Test01
        displayName: 'Deploy'

    - job: RunTestsOnTests01
      dependsOn: DeployToTest01
      pool:
        vmImage: ubuntu-latest

      steps:
      - script: echo Tests on Tests01
        displayName: 'Run tests'

This shows the initial stage which would be used to build the code and another stage that would be for deploying to the App Service.

Everything in the Test01 stage needs to be duplicated for other test environments but ideally we didn't want to bloat the pipeline with a lot of duplication.  Also, as Test01 is a deployment phase that should be updated as well.

We created a new YAML file in the repo called azure-environment.yaml:

parameters:
- name: env
  type: string 
  default: false

stages:
  - stage: Azure${{ parameters.env }}
    dependsOn: Build
    jobs:
    - deployment: DeployTo${{ parameters.env }}Dev
      environment: ${{ parameters.env }}
      pool:
        vmImage: ubuntu-latest
      strategy:
        runOnce:
          deploy:
            steps:
            - script: echo Deploy to ${{ parameters.env }} Dev
              displayName: 'Deploy'

    - job: RunTestsOn${{ parameters.env }}Dev
      dependsOn: DeployTo${{ parameters.env }}Dev
      pool:
        vmImage: ubuntu-latest

      steps:
      - script: echo Tests on ${{ parameters.env }}Dev
        displayName: 'Run tests'


The first four lines show that a parameter is expected called env, this is then used in the tasks to create the stage and deployment name.


This can then be used by updating the main yaml script:

trigger:
- main

stages:
  - stage: Build
    jobs:
    - job: Compilation
      pool:
        vmImage: ubuntu-latest

      steps:
      - script: echo Build build build!
        displayName: 'Compile code'

  - template: azure-environment.yaml
    parameters:
      env: Test01

  - template: azure-environment.yaml
    parameters:
      env: Test02

This means that the template block can be easily added to branches if for a period of time the code needs to be deployed to another test environment.


As the deployment uses an environment these would need to be configured in Azure DevOps but it doesn't have to contain anything, although it does provide the functionality for approvals which could be useful if used for higher end deployments (such as PreProd and Production).


To create an environment simply select environments:


Then follow the steps and create an empty one with the names (in our case Test01 and Test02).
If you are interested in having someone approve the deployment then use the 'Approvals and Checks' to add a group.