Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure DevOps YAML "each" syntax

I'm struggling with the syntax of the each keyword in YAML templates.

Essentially, I'm passing a list of jobs to a template. The template should execute those jobs, filtering out unwanted variable groups.

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
...

But I'm getting the compilation errors Expected a mapping and Object reference not set to an instance of an object. on the line:

- group: ${{ pair.value }}

Can anyone offer advice on what syntax or indenting that I may be missing?

like image 486
bensgroi Avatar asked Sep 06 '25 03:09

bensgroi


1 Answers

I figured it out. I was missing a dash on the if clause. The specifics of when to use dashes and how much to indent are extremely arcane and poorly documented in the docs.

Working solution:

# 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
...
like image 137
bensgroi Avatar answered Sep 07 '25 21:09

bensgroi