How do I dynamically set the display name of a task based on a variable inside azure-pipelines.yml? This doesn't really work:
variables:
- name: 'x'
${{ if eq(variables['env.foobar'], true) }}:
value: 'Yes'
${{ else }}:
value: 'No'
resources:
repositories:
- repository: self
type: git
ref: refs/heads/develop
- job: 'Skip_Build'
displayName: "x=$(x)" # also tried "x=${{x}}" but it results in an error
steps:
- checkout: ...
For some reason when the pipeline gets triggered the display name is printed like this:
x=$(x) <-- doesn't get substituted
I must be missing something obvious - how can I resolve this?
The reason why this does not work, is because the structure of the pipeline, which is stages
, jobs
& steps
are all evaluated under compile time, which is right when you press the Run pipeline
button.
In comparison, variables
are evaluated under run time.
So what happends is that when the engine compiles the pipeline, and sees your variable as part of the displayName
for a job, it simply skips to evaluate your variable, leading to an empty value.
In order to get the engine to actually evaluate your variable during compile time, you have to wrap it in a compile time expression, that is ${{ <expression }}
.
So based on my explanation above, the answer for your example should be to set it as following:
displayName: ${{ variables.x }}
Or if exactly like you had it:
displayName: x=${{ variables.x }}
I'm writing it without quotations as it is a really common misconception that the quotations are actually needed, everything will be considered strings unless it starts with an integer
, square brackets [
(for YAML array), curly braces {
(for YAML object) or true
/ false
(for boolean), and some other hexadecimal stuff.