azure devops - Variable evaluates fine for displayName but not for condition, why is that? - Stack Overflow

admin2025-04-25  2

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:

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:

Share Improve this question edited Jan 17 at 8:37 Ytsen de Boer asked Jan 16 at 12:32 Ytsen de BoerYtsen de Boer 3,1072 gold badges28 silver badges41 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Fixing the condition

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:

Fixing the display name

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:

转载请注明原文地址:http://anycun.com/QandA/1745531986a90849.html