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:
  - stageBuild
    jobs:
    - jobCompilation
      pool:
        vmImageubuntu-latest

      steps:
      - scriptecho Build build build!
        displayName'Compile code'

  - stageTest01
    jobs:
    - jobDeployToTest01
      pool:
        vmImageubuntu-latest

      steps:
      - scriptecho Steps to deploy to Test01
        displayName'Deploy'

    - jobRunTestsOnTests01
      dependsOnDeployToTest01
      pool:
        vmImageubuntu-latest

      steps:
      - scriptecho 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:
nameenv
  typestring 
  defaultfalse

stages:
  - stageAzure${{ parameters.env }}
    dependsOnBuild
    jobs:
    - deploymentDeployTo${{ parameters.env }}Dev
      environment${{ parameters.env }}
      pool:
        vmImageubuntu-latest
      strategy:
        runOnce:
          deploy:
            steps:
            - scriptecho Deploy to ${{ parameters.env }} Dev
              displayName'Deploy'

    - jobRunTestsOn${{ parameters.env }}Dev
      dependsOnDeployTo${{ parameters.env }}Dev
      pool:
        vmImageubuntu-latest

      steps:
      - scriptecho 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:
  - stageBuild
    jobs:
    - jobCompilation
      pool:
        vmImageubuntu-latest

      steps:
      - scriptecho Build build build!
        displayName'Compile code'

  - templateazure-environment.yaml
    parameters:
      envTest01

  - templateazure-environment.yaml
    parameters:
      envTest02

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.