Photo by Fleur

How to use variables and parameters inside your Azure DevOps YAML templates

Quick tips on how to use correctly variables inside your templates.

Posted by Damien Aicheh on 02/10/2021 · 4 mins

In the previous article we have shown you the basics of using variables inside your Azure DevOps pipelines. Today we will go further and take a look at different ways to use variables and parameters inside reusable templates.

Variables usage

For the purpose of this tutorial let us take this extract of template:


variables:
- name: minimumVersionNumber
  value: 1000

steps:
# ...steps before

- task: Bash@3
  displayName: 'Calculate a build number'
  inputs:
    targetType: 'inline'
    script: |
      echo Computing with $(minimumVersionNumber) or ${{ variables.minimumVersionNumber }}

# ...steps after

In this example, we have a basic shell task Bash@3 that calculates the build version number for our application. Because we define the minimumVersionNumber inside the default Azure DevOps variables object, there are two options to access it:

  • $(minimumVersionNumber): This notation is accessible anywhere in your template
  • ${{ variables.minimumVersionNumber }}: This is only accessible at the same level of the variables definition

Pass parameters to a template

With that done we will create a new template inside a file called: compute-build-number.yml to be able to reuse this task in different projects:


# compute-build-number.yml

# Define parameter first way:
parameters:
  minVersion: 0

# Or second way:
parameters:
- name: minVersion
  type: number
  value: 0

steps:
- task: Bash@3
  displayName: 'Calculate a build number'
  inputs:
    targetType: 'inline'
    script: |
      echo Computing with ${{ parameters.minVersion }}

As you can see the minVersion is defined inside the object parameters, to access it you need to use ${{ parameters.minVersion }}. You can call the object parameters the way you need, the goal is to define all parameters needed for this template to work. This will also be useful when someone else want to know the necessary parameters to provide to be able to reuse this template.

Now, let us update our pipeline to use this template:


variables:
- name: minimumVersionNumber
  value: 1000

steps:
# ...steps before

- template: compute-build-number.yml
  parameters:
    minVersion: $(minimumVersionNumber)

# OR:
  
- template: compute-build-number.yml
  parameters:
    minVersion: ${{ variables.minimumVersionNumber }}

# ...steps after

Why do not we use the $(my_variable) notation everywhere inside our pipeline and avoid using parameters for our templates?

It is not recommended because if you do that you will:

  • Lose the general property of your templates and you will not have the ability to reuse it,
  • You will not know which parameters are needed to use the template.

Final touch

As you can see in these tutorials, you have multiple ways to use parameters and variables inside Azure DevOps, be sure to check the context to apply the right one, you do not access your variables and parameters the same way.

Do not hesitate to follow me on to not miss my next tutorial!