Using script variables in Azure Release Pipelines
Azure Pipeline still has 'classic' Release Pipelines. It has some limitations compared to the newer YAML-based deployment jobs, but sometimes the visualisation it provides for both pipeline design and deployment progress is still preferred.
I recently wanted to make a task in a release pipeline conditional on the value of a variable set in an earlier script task, but I wasn't quite sure how to reference the variable in the conditional expression.
To figure it out, I did a little experiment. I started with a script task that contained this content:
echo "##vso[task.setvariable variable=myJobVar]this is the same job"
echo "##vso[task.setvariable variable=myOutputJobVar;isoutput=true]this is the same job too"
The script sets two variables, the second being an 'output' variable.
In the Output Variables section, the script task also sets the Reference name to setOutput
.
I then created some additional script tasks, and under the Control Options section, change Run this task to Custom conditions
, and then entered one of these expressions in each:
and(succeeded(), ne(variables.myVar, ''))
and(succeeded(), ne(variables['myOutputJobVar'], ''))
and(succeeded(), ne(variables['setOutput.myOutputJobVar'], ''))
Note that as we're working with conditions and expressions, we use the runtime expression syntax to reference variables.
So from this, I can conclude:
- For a regular variable, use
variables.myVar
, where the name of the variable wasmyVar
- For an output variable, use
variables['setOutput.myOutputJobVar']
, where the name of the variable wasmyOutputJobVar
and the task had a reference name ofsetOutput
.
What about referencing a variable from a subsequent job?
Unfortunately, this is not possible with classic release pipelines.
The closest you can get to this is a hack involving using the Azure DevOps API to update a predefined release definition variable.
Output variables are primarily intended to allow consumption in downstream jobs and stages. Given this is not possible with release pipelines, there's no real advantage to creating output variables in your scripts. To keep things simple just define them as regular variables.
Categories: Azure Pipelines