You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何遍历Azure Pipelines中类型为Object的参数?

Azure DevOps管道二维集合遍历的替代实现方式

针对你的二维集合遍历需求,除了已有的YAML嵌套each循环写法,还有以下几种实用的实现方式:

1. 直接按对象键名遍历(适用于键名固定的场景)

如果你的myObject参数的键(比如things0、things1)是固定已知的,可以直接通过键名访问对应数组,避免外层的键遍历:

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

steps:
# 遍历things0数组
- ${{ each item in parameters.myObject.things0 }}:
  - script: echo ${{ item }}
# 遍历things1数组
- ${{ each item in parameters.myObject.things1 }}:
  - script: echo ${{ item }}

这种方式更直观,适合键名固定、不需要动态适配未知键的场景。

2. 将参数定义为数组嵌套数组(纯二维数组结构)

如果你的场景更偏向标准二维数组,而非带命名键的对象,可以直接把参数定义为数组类型,遍历逻辑会更简洁:

parameters:
- name: my2DArray
  type: object
  default:
  - [one, two, three]
  - [four, five, six]

steps:
- ${{ each row in parameters.my2DArray }}:
  - ${{ each item in row }}:
    - script: echo ${{ item }}

这种结构更贴近传统二维集合的定义,遍历层级逻辑更清晰。

3. 在脚本层实现遍历(减少YAML模板复杂度)

如果不想在YAML层面写多层循环,可以把二维集合传递给脚本,在Shell/PowerShell中实现遍历,适合复杂的遍历逻辑:

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

steps:
- script: |
    # Bash脚本遍历示例
    obj='${{ convertToJson(parameters.myObject) }}'
    # 解析JSON并遍历
    echo "$obj" | jq -r 'keys[] as $key | "\($key): \(.[$key] | .[])"' | while read line; do
      echo "$line"
    done
  displayName: 'Bash遍历二维集合'

- pwsh: |
    # PowerShell脚本遍历示例
    $obj = ${{ convertToJson(parameters.myObject) }} | ConvertFrom-Json
    foreach ($key in $obj.PSObject.Properties.Name) {
      Write-Host "Key: $key"
      foreach ($item in $obj.$key) {
        Write-Host "  Item: $item"
      }
    }
  displayName: 'PowerShell遍历二维集合'

这种方式把遍历逻辑下沉到脚本层,YAML模板更简洁,也支持更复杂的业务处理逻辑。


内容的提问来源于stack exchange,提问作者clemensoe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 02:15:58