Usage of Variable Template in Azure DevOps Pipelines

 Author: Akhil M Anil || DevOps Engineer

Let's understand what variables in Azure DevOps are: 

Variables give us a convenient way to get key bits of data into various parts of the pipeline. The most common use of variables is to define a value that we can then use in our pipeline. The value of a variable can change from run to run or job to job in your pipeline.

Different types of variables are:

  1. User-defined variables
  2. System variables
  3. Environment variables

User-defined Variables

The variables defined by the user are called user-defined variables. In YAML pipelines, we can set variables at the root, stage, and job levels. We can use a variable group to make variables available across multiple pipelines or we can use templates to define variables that are used in multiple pipelines in one file.

In this blog, we will discuss variables defined in a template file and create a simple pipeline for getting variables from the variable template. 

Advantages of using Variable Templates over Variable Groups:

  • YAML template variables can be expanded at compile-time, and variables defined in variable groups can only be expanded at runtime.
  • For YAML templates, whatever changes are done in the variable templates are available in the history of the repository where the file resides whereas in Variable groups the last changed user's name will be available unless it is connected to any log analytics workspace.
  • YAML templates offer more freedom in terms of the structure of the data.
The main disadvantage of the YAML template is that we cannot store secrets in the template file.

Variable Template definition

definition of variable template in the azure-pipelines.yml file: 


defining variables in variable-template.yml


Let's create an ADO pipeline and define variables in both the Variable Group and Variable Template and invoke those variables in our pipeline.

Download the code from Azure-DevOps/Variables

Steps:

1. Download the code and upload it to a repository in Azure Repos.



2. Let's start with creating a variable group and define a variable in VG and use the variable in our pipeline. Navigate to Library under the pipelines and click on Variable Group. Create a variable group VG-Test with variable VG.


 
3. Now we can create an ADO pipeline. Click on pipelines and create a new pipeline. Select the repository with the yml pipeline file. 

If you are not familiar with pipeline creation or having any issue, feel free to refer to this blog which contains the steps for pipeline creation. (Blog)


Here in the pipeline, we have defined two scripts one for fetching values from the variable group and another for fetching values from the variable template. 


For variable groups, Macro syntax ($VG) is used which will be processed during runtime and for variable templates, template expression (${{ variables.var }}) is used which will be processed during the compile time.
In a pipeline, template expression variables (${{ variables.var }}) get processed at compile time, before runtime starts. Macro syntax variables ($(var)) get processed during runtime before a task runs.

Now let's run the pipeline.

Variable Group Task:

Here we can see the script $VG, since It's a macro expression the value from the variable group will get replaced in the run time only.

Variable Template Task:

Here, we are using template expression and the value ${{ variables.VGTemplate }} will get replaced in compile time.

Connect me via:

References: 

Comments

Popular posts from this blog

Install Java silently using powershell in Azure Windows VM

Configure an Azure DevOps self-hosted Windows agent in Docker

List of Repositories - Authorize Rest API calls using Job Access Token