Azure Pipeline use template expression with queued variables

百般思念 提交于 2020-07-23 06:29:05

问题


I defined a YAML build-pipeline in azure:

variables:
  test: '${{ variables.Environment }}'

pool:
  vmImage: 'ubuntu-latest'

steps:
- script: |
    echo $(test)
  displayName: 'Show test'

I queue that pipeline with the Environment variable defined as 'abc':

I expect it to echo abc but instead abc is replaced with nothing - variables.Environment seems to be undefined.

Later on I want to load a different Variable Group depending on the Environment variable, which is why I do not echo $(Environment) directly in the script. It's just a simplified example.


回答1:


I expect it to echo abc but instead abc is replaced with nothing - variables.Environment seems to be undefined.

According to this document:

Runtime happens after template expansion. Template variables are processed at compile time, and are replaced before runtime starts. Template variables silently coalesce to empty strings when a replacement value isn't found.

So in your case echo $(test) print out nothing but empty string. Cause the queue variables are used for runtime. For this, you can consider using macro or runtime expression which is for runtime. Both test: $(Environment) and test: $[variables.Environment] work well on my side.

Later on I want to load a different Variable Group depending on the Environment variable, which is why I do not echo $(Environment) directly in the script. It's just a simplified example.

As I know, linking different variable groups depending on the dynamic Environment variable is not supported yet, here's one discussion about that topic. And this is one good workaround in that scenario.

Nowadays Azure Devops Service is rolling out the new feature runtime parameters, I think it can meet most of your requirements. It could be a better choice for you, use runtime parameters instead of not supported dynamic Environment variable.

My simple test about this option:

1.Content in yaml:

parameters:
- name: group
  displayName: Group Name
  type: string
  default: TestGroup
  values:
  - TestGroup
  - Group2
  - Group3
  - Group4

variables:
- group: ${{ parameters.group }}

steps:
- script: |
    echo $(Name)
  displayName: 'Show group name'

2.My variable group TestGroup:

3.Click run pipeline:

4.The pipeline runs well and the displays the variable defined in variable group:

Hope it helps :)



来源:https://stackoverflow.com/questions/60947836/azure-pipeline-use-template-expression-with-queued-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!