【发布时间】:2021-11-07 17:42:07
【问题描述】:
在这种情况下,我想将传递到模板中的作业的作业变量与模板中定义的公共变量列表(特别是模板中固定作业的输出变量)结合起来。
为了证明这一点,在这个例子中,我扩展了Microsoft sample,尝试将模板中的变量组合到传入的作业中。为了示例的缘故,假设 bar.foo 确实存在,并且是通过 CredScan 中的步骤输出的:
parameters:
- name: jobs
type: jobList
default: []
jobs:
- job: CredScan # Cred scan first
pool: MyCredScanPool
steps:
- task: MyCredScanTask@1
- ${{ each job in parameters.jobs }}: # Then each job
- ${{ each pair in job }}: # Insert all properties other than "dependsOn" and "variables"
${{ if ne(pair.key, 'dependsOn') }}:
${{ if ne(pair.key, 'variables') }}:
${{ pair.key }}: ${{ pair.value }}
dependsOn: # Inject dependency
- CredScan
- ${{if job.dependsOn}}:
- ${{ job.dependsOn }}
variables:
bar.foo: $[ dependencies.CredScan.outputs['bar.foo'] ]
${{ if job.variables }}:
${{ job.variables }}
这会给出错误“预期映射”。移动缩进并不能改变这一点。添加连字符将其更改为“解析块映射时,未找到预期的键”。
我猜这是因为您可以将各种结构用于变量,例如带有冒号或显式定义名称/值的密钥对。以某种方式组合变量对象不起作用。尝试循环变量的内容,然后插入键和值也失败了。
${{ insert }}: ${{ job.variables }}
至少编译的是自己添加作业变量:
${{ if job.variables }}:
variables: ${{ job.variables }}
但是添加其他变量会再次导致错误,我认为是因为数据类型结构。
${{ if job.variables }}:
variables: ${{ job.variables }}
bar.foo: $[ dependencies.CredScan.outputs['bar.foo'] ]
或者
${{ if job.variables }}:
variables:
${{ job.variables }}
bar.foo: $[ dependencies.CredScan.outputs['bar.foo'] ]
注意:我实际上不希望 if job.variables 检查整个变量部分,因为我想添加我的公共变量,而不管作业是否已经拥有一些自己的变量。目前尚不清楚这是否可以完全删除或存在于变量部分中。
帮助不胜感激!
【问题讨论】:
标签: azure-devops yaml azure-pipelines azure-pipelines-yaml