admin管理员组文章数量:1313362
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"
Share
Improve this question
edited Jan 31 at 12:07
Rui Jarimba
18.2k11 gold badges64 silver badges98 bronze badges
Recognized by CI/CD Collective
asked Jan 30 at 21:55
user29437807user29437807
1
|
1 Answer
Reset to default 2As 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:
- Code is cleaner: no
${{ if ... }}
statements in the middle of the code - Better variables anization: each environment has its own specific template
本文标签: How do I use expressions when defining variables in Azure DevOps yaml pipelinesStack Overflow
版权声明:本文标题:How do I use expressions when defining variables in Azure DevOps yaml pipelines - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741934447a2405769.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
parameters['PARAM']
syntax I'd recommend usingparameters.PARAM
- it's slightly more readable IMO :-) – Rui Jarimba Commented Jan 31 at 11:41