Introduction
The use of variables is important in Azure DevOps pipelines, especially when using YML templates. They allow a single pipeline to be used for multiple branches/configurations etc.
The most common form of variables you see is are the predefined built in variables e.g. $(Build.BuildNumber) and your own custom ones e.g. $(var). Usually the value of these variables are set before/as the build is run, as an input condition.
But this is not the only way variables can be used. As noted in the documentation there are different ways to access a variable…
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. Runtime expressions $[variables.var] also get processed during runtime but were designed for use with conditions and expressions.
Azure DevOps Documentation
99% of the time I have been fine using just the $(var) syntax, but I recently was working on a case where this would not work for me.
The Issue
I had a pipeline that made heavy use of YML templates and conditional task insertion to include sets of task based upon the manually entered and pre-defined variables.
The problems that one of the tasks, used in a template, set a boolean output variable $(outVar) by calling
echo '##vso\[task.setvariable variable=outvar;isOutput=true\]true'
This task created the output variable could be accessed by other tasks as the variable $(mytask.outvar), but it was set at runtime it not available at the time of the YML compilation.
This caused me a problem as it meant that it could not be used in the template’s conditional task inclusion blocks as it as not present art compile time when this code is evaluated e.g.
\- ${{ if eq(mytask.outvar, 'true') }} :
# the task to run if the condition is met
- task: Some.Task@1
....
I tied referencing the variable using all forms of $ followed by brackets syntax I could think of, but it did not help.
The lesson here is that you cannot make a runtime value a compile time value by wishing it to change.
The only solution I could find was to make use of the runtime variable in a place where it can be resolved. If you wish to enable or disable a task based on the variable value then the only option is to use the condition parameter
# the task to run if the condition is met
- task: Some.Task@1
condition: and(succeeded(), eq(mytask.outvar, 'true'))
....
The only downside of this way of working as opposed to the conditional insertion is that
- If you conditional insertion, non required tasks are never shown in the pipeline as they are not compiled into it
- If using the condition property to exclude a task, it will still appear in the log, but it can be seen that it has not been run.
So I got there in the end, it was just not as neat as I had hoped, but I do have a clearer understanding of compile and runtime variables in Azure DevOps YML