Passing a variable to a release pipeline in Azure DevOps is one of the major challenges when building your CI/CD pipelines. While it should be something straightforward and intuitive, there’s no way to do this directly. Fortunately, there are variable groups, which can help in achieving the goal.
The “intuitive” approach
As in DevOps, many build variables are available in release pipelines, one would think, that we can just declare a variable in a build pipeline and voilà – magic happens:
As nothing can be easy, the result of running a release pipeline with the above approach is a shiny error telling us, that there’s no such a variable defined for a release:
The intuitive approach will just not work – what are our possibilities?
Passing a variable
My initial though was “if I cannot just pass a variable like this, I will write it to a file, publish a file as artifact and read it during a release”. Such an approach feels a little bit “stinky” doesn’t it? To make it work, we need the following actions:
add a step which saves a value to a file(TXT/JSON/YAML/etc…)
include the file in the publish artifacts step
download the file and extract the value
For me, it was once again a no-go. I don’t want to complicate my pipelines only for the sake of passing variables. This is when I discovered variable groups in Azure DevOps!
Variable group for a rescue
The variable group is a special feature of Azure DevOps, which allows you to declare sets of variables. The important thing here is they can be reused across pipelines:
What is important here, that you can use other variables as values(as in the above screen). Because they are evaluated during running a pipeline, you link them to a build and a release, they will be passed from one place to another.
The important thing here is the fact, that you can link different variables groups to different stages of your pipelines. This gives you more options, especially when passing different parameters per environment.