azure devops - How to manage variable declarations according to parameters - Stack Overflow

admin2025-04-21  1

In a yaml pipeline I have defined a string parameter with multiple values.
According to the selected value, I want to assign multiple variables.
The goal here is to write elegant and readable code. I want to avoid multiple ifs statements.

# I want to avoid this
parameters:
- name: virtualApp
  type: string
  default: 'value1'
  values:
  - 'value1'
  - 'value2'
  - 'value3'
  - 'value4'

variables:
- name: poolName
  value: 'pool'
- name: BuildConfiguration
  value: 'Release'
- ${{ if eq(${{ parameters.virtualApp }}, 'value1') }}:
  my variables for value 1
- ${{ if eq(${{ parameters.virtualApp }}, 'value2') }}:
  my variables for value 2

I tried the following code which is more elegant but I get an error: unexpected value 'condition'.

variables:
- name: poolName
  value: 'pool'
- name: BuildConfiguration
  value: 'Release'
- template: variables-value1.yml
  condition: eq( ${{ parameters.virtualApp }}, 'value1')
- template: variables-value2.yml
  condition: eq( ${{ parameters.virtualApp }}, 'value2')

Is there a way to manage this?

In a yaml pipeline I have defined a string parameter with multiple values.
According to the selected value, I want to assign multiple variables.
The goal here is to write elegant and readable code. I want to avoid multiple ifs statements.

# I want to avoid this
parameters:
- name: virtualApp
  type: string
  default: 'value1'
  values:
  - 'value1'
  - 'value2'
  - 'value3'
  - 'value4'

variables:
- name: poolName
  value: 'pool'
- name: BuildConfiguration
  value: 'Release'
- ${{ if eq(${{ parameters.virtualApp }}, 'value1') }}:
  my variables for value 1
- ${{ if eq(${{ parameters.virtualApp }}, 'value2') }}:
  my variables for value 2

I tried the following code which is more elegant but I get an error: unexpected value 'condition'.

variables:
- name: poolName
  value: 'pool'
- name: BuildConfiguration
  value: 'Release'
- template: variables-value1.yml
  condition: eq( ${{ parameters.virtualApp }}, 'value1')
- template: variables-value2.yml
  condition: eq( ${{ parameters.virtualApp }}, 'value2')

Is there a way to manage this?

Share Improve this question edited Mar 31 at 14:28 bryanbcook 18.9k2 gold badges47 silver badges79 bronze badges asked Jan 22 at 20:53 Steph09Steph09 114 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Conditions are not natively supported in templates.

If you have variable templates such as variables-valueX.yml you can dynamically reference the template using the parameter value:

parameters:
  - name: virtualApp
    type: string
    default: 'value1'
    values:
      - 'value1'
      - 'value2'
      - 'value3'
      - 'value4'

variables:
  # other variables here

  - template: variables-${{ parameters.virtualApp }}.yml

You can't use a condition to select a template. Because template is expanded at compile time, but the condition is evaluated at runtime.

Currently, you can use if expression to select variable template based on the value of a parameter. See the example below for reference.

variables-value1.yml

variables:
  myVar1: 'value1'
  myVar2: 'value1'

variables-value2.yml

variables:
  myVar1: 'value2'
  myVar2: 'value2'

main.yml

pool:
  vmImage: ubuntu-latest

parameters:
- name: virtualApp
  type: string
  default: 'value1'
  values:
  - 'value1'
  - 'value2'

variables: 
- ${{ if eq(parameters.virtualApp, 'value1') }}:
  - template: variables-value1.yml
- ${{ if eq(parameters.virtualApp, 'value2') }}:
  - template: variables-value2.yml

steps:
- task: CmdLine@2
  inputs:
    script: |
      echo $(myVar1)
      echo $(myVar2)

When set the value of parameter virtualApp to value2, variables-value2.yml will be expended. Result:

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