首页 > 解决方案 > Azure DevOps YAML“每个”语法

问题描述

我正在为eachYAML 模板中关键字的语法而苦苦挣扎。

本质上,我将作业列表传递给模板。模板应该执行这些作业,过滤掉不需要的变量组。

template.yml:

parameters:
- name: DeployJobs
  type: jobList

jobs:
- ${{ each job in parameters.DeployJobs }}:
    ${{ if job.variables }}:
      variables:
      - ${{ each var in job.variables }}:
        - ${{ each pair in var }}:
            ${{ if ne(pair.value, 'var-group-blue') }}:
              - group: ${{ pair.value }}

azure-pipelines.yml:

jobs:
- template: template.yml
  parameters:
    DeployJobs:
    - deployment: mydeployjob
      variables:
      - group: var-group-blue # the template should filter out this var group
      - group: var-group-green
...

但是我收到了编译错误Expected a mapping并且Object reference not set to an instance of an object.在线:

- group: ${{ pair.value }}

任何人都可以就我可能遗漏的语法或缩进提供建议吗?

标签: azure-devopsyaml

解决方案


我想到了。我在if条款上遗漏了一个破折号。何时使用破折号以及缩进多少的细节非常神秘,并且在文档中没有记录。

工作解决方案:

# template.yml

parameters:
- name: DeployJobs
  type: jobList

jobs:
- ${{ each job in parameters.DeployJobs }}:
    ${{ if job.variables }}:
      variables:
      - ${{ each var in job.variables }}:
        - ${{ each pair in var }}:
          - ${{ if ne(pair.value, 'var-group-blue') }}:
            - group: ${{ pair.value }}
# azure-pipelines.yml

jobs:
- template: template.yml
  parameters:
    DeployJobs:
    - deployment: mydeployjob
      variables:
      - group: var-group-blue # the template should filter out this var group
      - group: var-group-green
...

推荐阅读