Search code examples
azure-devopsyamlazure-pipelines

How to iterate Azure pipelines parameter of type object


I'm referring to this article, where the options of pipeline parameters are well described.

MS Learn: pipeline.parameters.parameter definition

This sample (I adapted it for my purpose) is contained but I miss a snippet on how to use it:

parameters:
- name: myObject
  type: object
  default:
    things0:
    - one
    - two
    - three
    things1:
    - four
    - five
    - six

I think about something like

- ${{ each element in parameters.myObject }}:
  - script: echo ${{ element }} # prints things0, things1
 
  - ${{ each subelement in element }}:
    - script: echo ${{ subelement }} # prints one, two, three resp. four, five, six

The requirement is basically to provide a two dimensional collection and being able to iterate over both levels. Maybe this can also be acheived elseway?


Solution

  • You can iterate over all the properties of a parameter like a dictionary (kind of), and access the key (property name) and corresponding value:

    parameters:
      - name: myObject
        type: object
        default:
          things0:
          - one
          - two
          - three
          things1:
          - four
          - five
          - six
    
    trigger: none
    
    pool:
      vmImage: 'ubuntu-latest'
    
    steps:
    - ${{ each element in parameters.myObject }}:
      - script: echo ${{ element.key }}
        displayName: 'Echo ${{ element.key }}'
     
      - ${{ each subelement in element.value }}:
        - script: echo ${{ subelement }}
          displayName: 'Echo ${{ subelement }}'
    

    Running the pipeline:

    Pipeline tasks