I am trying to pass a value to a template parameter using a variable defined in Azure Variable Groups.
Variable group:
Here the pipeline
---
trigger: none
jobs:
- job: PipelineTasks
displayName: 'Pipeline Tasks'
pool:
vmImage: 'ubuntu-latest'
variables:
- group: Testing
steps:
- script: |
echo "$(variable1)"
displayName: 'Display Value'
- template: "/template.yml"
parameters:
isEnabled: $(variable1)
Here's the template.yml
---
parameters:
- name: isEnabled
displayName: "Whether a scan is enabled or disabled"
type: string
default: true
steps:
- ${{ if eq(parameters.isEnabled, true) }}:
- script: |
echo "${{ parmeters.isEnabled }}"
displayName: 'Task 1'
I expect Task 1
to run, to my surprise it isn't.
The template expression is evaluated at compile-time before runtime, and the variables from variable group are evaluated at runtime. So, when evaluating the conditional set in the template expression, the value of the variable is still empty that cause the conditional always return False
result.
You can use condition
key on the script task to directly check the value of the variable. It is evaluated at runtime.
The pipeline main YAML.
# azure-pipelines.yml
jobs:
- job: PipelineTasks
displayName: 'Pipeline Tasks'
variables:
- group: Testing
steps:
- script: |
echo "$(variable1)"
displayName: 'Display Value'
- template: /template.yml
The template YAML.
# template.yml
steps:
- script: |
echo "variable1 = $(variable1)"
displayName: 'Task 1'
condition: eq(variables.variable1, 'true')
Result.
When the value of "variable1
" is "true
", the step 'Task 1
' gets executed.
When the value of "variable1
" is NOT "true
", the step 'Task 1
' gets skipped.