azure-devopsazure-pipelinesazure-pipelines-yaml

Azure DevOps Pipeline Condition with parameters didn't work


I'm following the Microsoft reference:

Parameters in conditions

and this is my yml file:

parameters:
  - name: poolType # pool type for the agent, valid values are "windows" or "linux"
    type: string
    default: "windows"

steps:
  # Echo the poolType parameterAzure
  - script: |
      echo "Pool Type: ${{ parameters.poolType }}"
    displayName: "Echo poolType"
  # set gittoken for authentications
  - ${{ if eq(parameters.poolType, 'linux') }}:
      - script: |
          git config --global --list
          git config --global url."https://token:$MODUL_TOKEN@dev.azure.com".insteadOf "https://XXXX@dev.azure.com"
          git config --global --list
        displayName: 'set extra header for Git in Linux'
        env:
          MODUL_TOKEN: $(modules_token)
  - ${{ if eq(parameters.poolType, 'windows') }}:
      - script: |
          git config --global --list
          git config --global url."https://token:%MODUL_TOKEN%@dev.azure.com".insteadOf "https://XXXX@dev.azure.com"
          git config --global --list
        displayName: 'set extra header for Git in Windows'
        env:
          MODUL_TOKEN: $(modules_token)

And this the output of echo task: Echo Output

Why the "'set extra header for Git in Linux'" job is not executing? The condition is true!

First Try:

I used the following code, but it's not working again

- script: |
      git config --global --list
      git config --global url."https://token:$MODUL_TOKEN@dev.azure.com".insteadOf "https://osprod@dev.azure.com"
      git config --global --list
    displayName: 'set extra header for Git in Linux'
    env:
      MODUL_TOKEN: $(modules_token)
    condition: ${{ eq(parameters.poolType, 'linux') }}
  - script: |
      git config --global --list
      git config --global url."https://token:%MODUL_TOKEN%@dev.azure.com".insteadOf "https://osprod@dev.azure.com"
      git config --global --list
    displayName: 'set extra header for Git in Windows'
    env:
      MODUL_TOKEN: $(modules_token)
    condition: ${{ eq(parameters.poolType, 'windows') }}

Solution

  • I am able to use both techniques as you've written them without issue.

    Parameters exist at compile-time only. The pipeline run sequence expands the runtime expressions ${{ <expr> }} at compile-time.

    The condition element on stages, jobs and tasks are runtime expressions that evaluate as true or false. What you've written condition: ${{ eq( parameters.poolType, 'linux' ) }} will evaluate at compile-time to true or false. Which is an awkward syntax, but is valid.

    Note: if you enable system diagnostics by introducing the variable system.debug set to true, the debug logs on the step will show you how the condition was evaluated at runtime: debug view

    Here is a working pipeline with your code as you've written it (minus the git config logic):

    trigger: none
    
    parameters:
      - name: poolType # pool type for the agent, valid values are "windows" or "linux"
        type: string
        default: "windows"
        values:
        - linux
        - windows
    
    steps:
    # Echo the poolType parameterAzure
    - script: |
        echo "Pool Type: ${{ parameters.poolType }}"
      displayName: "Echo poolType"
    
    # Conditional Insertion
    - ${{ if eq(parameters.poolType, 'linux') }}:
        - script: |
            echo "${{ parameters.poolType }}"
          displayName: 'Conditional insertion Linux'
          env:
            MODUL_TOKEN: $(modules_token)
    - ${{ if eq(parameters.poolType, 'windows') }}:
        - script: |
            echo "${{ parameters.poolType }}"
          displayName: 'Conditional insertion Windows'
          env:
            MODUL_TOKEN: $(modules_token)
    
    # Runtime conditions
    - script: |
          echo "${{ parameters.poolType }}"
      displayName: 'Runtime condition Linux'
      env:
          MODUL_TOKEN: $(modules_token)
      condition: ${{ eq(parameters.poolType, 'linux') }}  # compile-time expression translates to "true" or "false"
      
    - script: |
         echo "${{ parameters.poolType }}"
      displayName: 'Runtime condition Windows'
      env:
        MODUL_TOKEN: $(modules_token)
      condition: ${{ eq(parameters.poolType, 'windows') }} # compile-time expression translates to "true" or "false"
    

    The pipeline when run with "linux":

    linux

    The pipeline when run with "windows":

    windows