Search code examples
azureazure-devopsparametersyamlpipeline

ADO Can I get a parameter based on a string and a different parameter?


I have stages as a parameter like this:

parameters:
  - name: stages
    type: object
    default:
      - stage: dev
        dependsOn: build
      - stage: test
        dependsOn: dev

  - name: execute_infra_tests_dev
    displayName: Execute infra tests on DEV
    type: boolean
    default: false

  - name: execute_infra_tests_test
    displayName: Execute infra tests on TEST
    type: boolean
    default: false

I loop through the stages using:

stages:
- stage: build
  displayName: Build
  jobs:
  - job:
    displayName: Build Job
...

- ${{ each stage in parameters.stages }} :
  - stage: ${{ stage.stage }}
    displayName: Deploy ${{ stage.stage }} Environment
    dependsOn: ${{ stage.dependsOn }}
...

This al works fine, no problem. But I want to do a stage that tests infra, context not important, but this should be a switch for dev to turn on and off. In the parameters there is the execute_infra_tests_XXX, where the XXX needs to be filled in by using ${{ stage.stage }}.

I have tried a lot of options, but none seem to work. In my head it should be something like this:

  - stage: ${{ stage.stage }}_infra_test
    displayName: ${{ stage.stage }} Infra Test
    dependsOn: ${{ stage.stage }}
    condition: eq( ${{ parameters.execute_infra_tests_${{ stage.stage }} }}, true)
    jobs:
    - job:

This obviously doesn't work, but trust me, I tried a millions ways and can't seem to figure it out.

ChatGPT came with this:

  condition: eq(${{ parameters['execute_infra_tests_' + parameters.stage] }}, true)

This felt like a solid option, but didn't work either.

I've tried a lot of (desperate) options, like:

  condition: eq(${{ parameters.execute_infra_tests_${{ stage.stage }} }}, true)

I can't remember what I tried, it was a lot...


Solution

  • You only option is to use index syntax here

    parameters:
    - name: 'solution'
      default: '**/*.sln'
      type: string
    
    steps:
    - task: msbuild@1
      inputs:
        solution: ${{ parameters['solution'] }}  # index syntax
    - task: vstest@2
      inputs:
        solution: ${{ parameters.solution }}  # property dereference syntax
    

    In you case it would be (using a format expression):

      condition: eq(${{ parameters[format('execute_infra_tests_{0}', parameters.stage)] }}, true)