Azure Pipeline使用具有排队变量的模板表达式 [英] Azure Pipeline use template expression with queued variables

查看:93
本文介绍了Azure Pipeline使用具有排队变量的模板表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在azure中定义了一个YAML构建管道:

 变量:测试:"$ {{variables.Environment}}"水池:vmImage:'ubuntu-latest'脚步:-脚本:|回声$(测试)displayName:显示测试" 

我将环境变量定义为'abc'的管道排队:

我希望它会,但是abc被什么都没有替换-变量.环境似乎未定义.

稍后,我想根据环境变量加载不同的变量组,这就是为什么我不直接在脚本中回显$(Environment)的原因.这只是一个简化的示例.

解决方案

我希望它会回显abc,但是abc不会替换为-变量.环境似乎未定义.

根据

3.单击运行管道:

4.管道运行良好,并显示在变量组中定义的变量:

希望它会有所帮助:)

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.

解决方案

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 :)

这篇关于Azure Pipeline使用具有排队变量的模板表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆