Azure pipeline conditions#
Azure pipeline has two kinds of conditions:
- With keyword
condition
- With jinja like format
${{if elseif else}}
In both syntax, we have use parameters and variables, but there's a big difference between them which makes DevOps frustrated.
Conditions with keyword ${{if elseif else}}#
With ${{if elseif else}}
condition, the using parameters and variables' values are calculated during the compilation/parsing/loading time
, which means:
- Even if you define a variable before the
${{if elseif else}}
block, but the condition is always evaluated tofalse
if you use this variable in the condition, as it considers the value doesn't exist yet during the compilation, so if you have a- ${{ else }}
block, it will always be executed. - In a
template
, unless the parameters' values can be calculated from the loading time, otherwise they're always evaluated to its default value, if the default value is not defined, Azure pipeline will not raise any error, the condition check just returnsalways false
, so the pipeline will never run into it except for the- ${{ else }}
block. - But in a
root pipeline
out of template, it's the real parameter value being evaluated in the${{if elseif else}}
block. - Some predefined variables cannot be used in
${{if elseif else}}
neither, check the columnAvailable in templates?
in the Use predefined variables doc, which means these values are always evaluated tonull
. - When evaluated to
false
, the tasks, scripts, etc. wont even be shown as skipped in the Azure pipelines UI, they're justnot shown
. - The official doc calls the parameters as runtime parameters, but in fact they're runtime only when they're not in a template.
Conditions with keyword condition#
The official doc puts the condition
keyword format in the Jobs and stages
level, but in fact, we can also use it in tasks
or scripts
level.
- Same as to
${{if elseif else}}
condition, if you useparameters
incondition
keyword conditions, it's value is calculated in the compilation time, so be careful with their usages. Variables
in the conditions are evaluatedin real time
, this is the only point that make DevOps happy.- If you really want to evaluate
in real time the parameters
, the workaround is to add a script task in advance that define some variables taking the values of parameters, and then use these variables in the conditions withcondition
keyword. - When evaluated to
false
, the tasks, scripts, etc. bound by the conditions will beshown as skipped
in the Azure pipelines UI. - As
condition
keyword is bound to a single task, script, jobs, stages, etc., if you want to for example run 3 tasks under the same condition, you need to add the same condition to the 3 tasks respectively, whereas with${{if elseif else}}
, we can group the 3 tasks under the same condition, but as explained above, the values of compared parameters or variables referenced in the${{if elseif else}}
format conditions are evaluated during the compilation/loading time, so${{if elseif else}}
will not work for all the use cases, this is the biggest pity of Azure Pipeline from my point of view. - We can add
condition
tojobs
, and inside the jobs, we can have multiple tasks, this could be a workaround of above pity if we do not want add condition to each task with the same condition.
A table to sum up#
Inputs Conditions | ${{if elseif else}} keyword | condition keyword |
---|---|---|
parameter | compilation/parsing/loading time | compilation/parsing/loading time |
variable | compilation/parsing/loading time | real time (except for some predefined variables) |