gpt4 book ai didi

Azure Pipeline 使用模板表达式和排队变量

转载 作者:行者123 更新时间:2023-12-03 00:41:27 26 4
gpt4 key购买 nike

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

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

pool:
vmImage: 'ubuntu-latest'

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

我使用定义为“abc”的环境变量对该管道进行排队:Environment = abc

我希望它回显 abc,但 abc 被替换为任何内容 - 变量。环境似乎未定义。

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

最佳答案

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

根据this document :

运行时发生在模板扩展之后。模板变量在编译时进行处理,并在运行时启动之前进行替换。当找不到替换值时,模板变量会默默地合并为空字符串。

因此,在您的情况下 echo $(test) 只打印空字符串。因为队列变量用于运行时。为此,您可以考虑使用宏或用于运行时的运行时表达式。 test: $(Environment)test: $[variables.Environment] 在我这边工作得很好。

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.

据我所知,尚不支持根据动态Environment变量链接不同的变量组,这里是one discussion关于那个话题。这是一件好事workaround在那种情况下。

如今 Azure DevOps 服务正在推出新功能 runtime parameters ,我想它可以满足你的大部分要求。使用运行时参数而不是不支持的动态环境变量对您来说可能是更好的选择。

我对此选项的简单测试:

1.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.我的变量组TestGroup:

enter image description here

3.点击运行管道:

enter image description here

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

enter image description here

希望有帮助:)

关于Azure Pipeline 使用模板表达式和排队变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60947836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com