azure-devopsyamlpipeline

Azure Pipelines: Why does parameter comparison work in one job condition but fail in others?


I have an Azure Pipelines YAML file with multiple jobs, each with a condition that compares a parameter value to a string. However, only one of these conditions works as expected. Here's a simplified version of my pipeline:

parameters:
  - name: environment
    type: string
    default: dev

jobs:
  - job: Job1
    condition: and(eq('dev', '${{ parameters.environment }}'), succeeded())
    steps:
      - script: echo "This job runs"

  - job: Job2
    condition: and(eq("dev", "${{ parameters.environment }}"), succeeded())
    steps:
      - script: echo "This job doesn't run"

  - job: Job3
    condition: and(eq(dev, ${{ parameters.environment }}), succeeded())
    steps:
      - script: echo "This job doesn't run"

  - job: Job4
    condition: and(eq("dev", '${{ parameters.environment }}'), succeeded())
    steps:
      - script: echo "This job doesn't run"

Only Job1 runs when the environment parameter is set to 'dev'. The conditions for Job2, Job3, and Job4 all fail, even though they seem to be doing the same comparison.

Why does the condition in Job1 work while the others fail? What's the correct way to write these conditions to ensure they all work as intended? Are there any best practices for writing conditions with parameter comparisons in Azure Pipelines?

I'm confused about the role of single quotes, double quotes, and no quotes in these conditions. Any clarification would be greatly appreciated!

I have tried browsing and looking for similar cases, but I cannot understand why.

Example error which occurs (Job3) is:

Unrecognized value: dev. Located at position 15 within expression: and(eq(dev, dev), succeeded()). For more help, refer to https://go.microsoft.com/fwlink/?linkid=842996

Solution

  • I can reproduce the same error when using your YAML sample.

    The cause of the issue is that the value in the expression (eq) is not a valid literal.

    Valid literal sample in expression:

    # Examples
    variables:
      someBoolean: ${{ true }} # case insensitive, so True or TRUE also works
      someNumber: ${{ -1.2 }}
      someString: ${{ 'a b c' }}
      someVersion: ${{ 1.2.3 }}
    

    In the Pipeline expressions or conditions, we need to add single quotes to make the string a valid text.

    For example:

    condition: and(eq('dev', '${{ parameters.environment }}'), succeeded())
    

    For more detailed info about Pipeline condition, you can refer to this doc: Pipeline conditions