When defining a variable in an Azure DevOps .yaml
pipeline:
variables:
- name: environment
value: "PROD"
How can I use an expression to dynamically set the value of the variable?
I have tried doing it like this (note parameter name/value is not real):
variables:
${{ if eq(parameters['PARAM'], 'VALUE') }}
- name: environment
value: "PROD"
When defining a variable in an Azure DevOps .yaml
pipeline:
variables:
- name: environment
value: "PROD"
How can I use an expression to dynamically set the value of the variable?
I have tried doing it like this (note parameter name/value is not real):
variables:
${{ if eq(parameters['PARAM'], 'VALUE') }}
- name: environment
value: "PROD"
As an alternative to Scott's answer, consider creating several variable templates (one for each environment) and dynamically reference these templates using a parameter.
Example pipeline:
parameters:
- name: environment
type: string
default: development
values:
- development
- production
pool:
vmImage: ubuntu-latest
trigger: none
variables:
- template: /pipelines/variables/${{ parameters.environment }}-variables.yaml
steps:
- checkout: none
- script: |
echo "Selected environment: $(environment)"
displayName: 'Print environment'
/pipelines/variables/production-variables.yaml:
variables:
- name: environment
value: PROD
readonly: true
# other production specific variables here
/pipelines/variables/development-variables.yaml:
variables:
- name: environment
value: DEV
readonly: true
# other development specific variables here
Advantages:
${{ if ... }}
statements in the middle of the code
parameters['PARAM']
syntax I'd recommend usingparameters.PARAM
- it's slightly more readable IMO :-) – Rui Jarimba Commented Jan 31 at 11:41