Here we come to the real reason for writing this article. Generating parameters in one stage and passing them on to the next stage turned out to be not the easiest task. And here's why. Because theoretically, each
stage can be run on a separate agent, there can be no "shared" memory or global variables that could store values generated at runtime in one
stage for use in another
stage. However, Microsoft couldn't completely deprive DevOps of this capability due to the sheer banality of the task and found perhaps the only working solution within this architecture.
But before we move on to reviewing this solution, let's delve into the approach to variables in YAML pipelines in general.
There are only 3 types of variables:
- macro,
- template expression
- runtime expression.
Each of them works slightly differently.
Template expression is processed at the time of pipeline compilation. Essentially, it's a copy-paste of the value you defined in a variable of this type in the YAML file where you use this variable. You cannot change such variables after the pipeline has started. The syntax for such variables is
${{ variables.var }}.
But
Macro and
Runtime expressions are a bit more complicated. They both work at runtime, but
Macro is executed before a task executes. Also, runtime expressions are optimized for use in
conditions and
expressions. Their syntax is
$(var) for Macro and
$[variables.var] for
Runtime expressions.
For the purpose of this article, this information is sufficient, but if you want to delve deeper into working with variables, you can refer to the documentation provided in
the link.
Now, let's talk about output variables. You can find all the details
here, but we'll discuss transferring variables between stages to implement our idea.
So, to make a variable visible "outside" the stage, there is a special syntax: