Quick tip: How to use dynamic variable groups in Azure DevOps YAML pipelines

February 20, 2020

The preferred way to implement pipelines these days in Azure DevOps is via YAML. Which makes the pipeline part of your code and is automatically version controlled.

When you were used to the classic way of creating release pipelines, you might have used environment scoped variables. These scoped variables were great to specify to which environments you wanted to provision/install/deploy/… your solutions. Like when you were releasing DEV solutions, it would use the DEV environment credentials.

Variable groups

In the YAML pipelines, the best way would be to make use of variable groups, but these still support only one environment.

A way to support these different environments per branch would be by creating a variable group per branch/environment.

Variable groups
Variable groups

Using dynamic variable groups

The solution to use dynamic variable groups is by using expressions.

Info: More information about these expressions can be found here - Azure DevOps Expressions.

Here you can see an example:

1
2
3
4
5
variables:
- ${{ if eq(variables['build.SourceBranchName'], 'master') }}:
  - group: <name>-master
- ${{ if ne(variables['build.SourceBranchName'], 'master') }}:
  - group: <name>-dev

The funny side note, yesterday Sebastien Levert and I were figuring out how we could make this work. One critical thing we forgot was the - in front of the expression. Once we found the solution, Sebastien said that he was going to blog, as he has not done it, and I needed the same approach today, it is time to share it.

Comments

comments powered by Disqus