如何迭代对象类型的Azure管道参数

问题描述 投票:0回答:1

我指的是这篇文章,其中对管道参数的选项进行了很好的描述。

MS Learn:管道.参数.参数定义

包含此示例(我根据自己的目的对其进行了修改),但我错过了有关如何使用它的片段:

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

我想类似的事情

- ${{ 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

需求基本上是提供一个二维集合并能够在两个级别上进行迭代。也许这也可以通过其他方式实现?

azure-devops yaml azure-pipelines
1个回答
1
投票

您可以像字典(某种)一样迭代参数的所有属性,并访问键(属性名称)和相应的值:

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 }}'

运行管道:

Pipeline tasks

© www.soinside.com 2019 - 2024. All rights reserved.