这个问题的变体:
TL;DR:
组变量在模板编译时似乎不可用,例如当基于该组中的变量有条件地设置要运行的模板时 - 似乎只有在管道中显式设置的变量可用。
长版:
我有一组变量组; 'Group-NonProd'、'Group-Prod' 等。每个都包含变量 'identifier',具有不同的值(以下示例为“dev”、“prod”)。
我有一个主管道“main-pipeline.yml”,其中有几个阶段,每个阶段对应于“标识符”的值,例如:
- stage: NonProd
variables:
- group: 'Group-NonProd' ## includes the variable 'identifier' with value "nonprod"
jobs:
- template: nonprod.yml
parameters:
identifier: $(identifier)
- script: echo "environment is $(identifier)"
- stage: Prod
variables:
- group: 'Group-Prod' ## includes the variable 'identifier' with value "prod"
jobs:
- template: prod.yml
parameters:
identifier: $(identifier)
- script: echo "environment is $(identifier)"
但是,当我运行管道时,参数“标识符”不会扩展到组变量“标识符”中的值 - 它是空白的 - 我看到当我尝试在以下条件逻辑中使用变量时(目的是使用此逻辑来确定调用哪个模板 - 查看注释掉的行 - 并将该参数传递给它们):
steps:
- ${{ if eq(parameters.identifier, 'nonprod') }}:
# - template: nonprod.yml
- script: echo "Using nonprod template, environment is ${{ parameters.identifier }}"
- ${{ if not(eq(parameters.identifier, 'prod')) }}:
# - template: prod.yml
- script: echo "Using prod template, environment is ${{ parameters.identifier }}"
上面的脚本总是求助于第二个条件,因为结果总是“Using prod template, environment is”(空白)。
但奇怪的是 - 如果我在每个阶段明确设置变量“标识符”,它确实有效!
例如这行得通:
- stage: NonProd
variables:
- group: 'Group-NonProd'
- name: identifier
value: nonprod
jobs:
- template: nonprod.yml
parameters:
identifier: $(identifier)
- script: echo "environment is ${{ parameters.identifier }}"