I'm trying to make a step conditional on a variable from strategy matrix:
stages:
- stage: Build
displayName: 'Build Stage'
jobs:
- job: BuildJob
strategy:
matrix:
fiets:
model_name: 'fiets'
reis:
model_name: 'reis'
displayName: "Build Job $(model_name)"
steps:
- bash: echo $(model_name)
condition: eq(${{ variables['model_name'] }}, 'fiets')
displayName: $(model_name) == fiets
I get the error that the condition is not valid.
However, if I comment out the condition
line, then the expression $(model_name)
is (as expected) replaced with fiets
and reis
in the displayName
. But apparently this is not so for the condition
of the step (not expected), what is the reason for that?
Update: @Anil's answer (not the accepted answer) below sounds promising:
trigger:
- master
stages:
- stage: Build
displayName: 'Build Stage'
jobs:
- job: BuildJob
strategy:
matrix:
fiets:
model_name: 'fiets'
reis:
model_name: 'reis'
displayName: "Build Job $(model_name)"
steps:
- bash: echo $(model_name)
condition: eq('${{ matrix.model_name }}', 'fiets')
displayName: $(model_name) == fiets
However, it leads to this error:
Matrix configuration is available as runtime variables.
You don't need to use a template expression ${{ ... }}
in the condition:
stages:
- stage: Build
displayName: 'Build Stage'
jobs:
- job: BuildJob
strategy:
matrix:
fiets:
model_name: 'fiets'
reis:
model_name: 'reis'
displayName: "Build Job $(model_name)"
steps:
- checkout: none
- bash: echo $(model_name)
condition: eq(variables['model_name'], 'fiets') # <------------------ fixed
displayName: $(model_name) == fiets
Running the pipeline:
As an alternative, consider using an array parameter and an ${{ each }}
loop to generate the jobs dynamically:
parameters:
- name: models
type: object
default:
- fiets
- reis
stages:
- stage: Build
displayName: 'Build Stage'
jobs:
- ${{ each model in parameters.models }}: # <---------- loop
- job: BuildJob_${{ model }}
displayName: "Build Job ${{ model }}"
steps:
- checkout: none
- bash: echo $(model_name)
displayName: ${{ model }} == fiets # <-------------- fixed
condition: eq('${{ model }}', 'fiets') # <----------- condition
Running the pipeline: