我的 Azure DevOps 管道中有一个 PowerShell 脚本:
- task: PowerShell@2
displayName: Get_records
inputs:
targetType: 'inline'
script: |
<...>
$records.Records
$records.Records是一些包含记录数组的变量。我需要以这种方式使用这些数据:对于该数组中的每条记录,我需要在一项作业中执行多项任务。像这样的事情:
stages:
- stage: stage_1
jobs:
- job: Job_1
- task: PowerShell@2
displayName: Get_records
inputs:
targetType: 'inline'
script: |
<..getting this records..>
$records.Records
- ${{each records in variables.records.Records}}:
- task: not_powershell
displayName: name1
inputs:
AsyncOperation: true
MaxAsyncWaitTime: '60'
- task: not_powershell
displayName: name2
inputs:
AsyncOperation: true
MaxAsyncWaitTime: '60'
我怎样才能做到这一点?对此有几个问题:
- 如何在 foreach 循环中使用 '$records.Records' 变量?使用前必须保存变量吗?如果是 - 如何保存数组?
- 如果不可能以这种方式做到这一点,可能有一些方法,例如使用多个阶段、作业...等?
不,你不能。它只适用于参数 https://learn.microsoft.com/en-us/azure/devops/pipelines/process/runtime-parameters?view=azure-devops,不是变量:
您可以使用each关键字循环遍历参数 https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#each-keyword
这是因为:
- 在运行之前,会解析 yaml 并compiled https://learn.microsoft.com/en-us/azure/devops/pipelines/process/runs?view=azure-devops#process-the-pipeline进入管道结构,定义了所有阶段、作业和任务。这是评估循环条件的点,并且循环扩展到多个阶段/作业/任务。
-
在编译时尚不可用,因此此时不能使用它们来定义each循环。
- 在运行时,当动态变量可用时,管道结构已经固定,因此此时不可能添加额外的任务。
解决方法
You can't循环管道中的动态数组变量;但有一个地方你can这样做是在一个任务内,例如在一个powershell脚本 https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/powershell?view=azure-devops:
- task: PowerShell@2
displayName: Loop Over Records
inputs:
targetType: 'inline'
script: |
# first, get the records
ForEach ($record in $records.Records) {
# do something with $record
}
这不像管道循环那么强大,可以使用所有不同类型的非 powershell 任务,但它可能是循环该数组的唯一方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)