我的 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”變數?我必須在使用前保存變數嗎?如果是 - 如何保存陣列?
- 如果這樣做是不可能的,可能有一些方法可以解決,例如使用多個階段、作業......等等?
uj5u.com熱心網友回復:
不,你不能。它僅適用于引數,不適用于變數:
您可以使用 each 關鍵字回圈引數
這是因為:
- 在運行之前,yaml 被決議并編譯成管道結構,其中定義了所有階段、作業和任務。這是評估回圈條件的點,回圈被擴展為多個階段/作業/任務。
- 動態運行時變數在編譯時尚不可用,因此此時不能使用它們來定義每個回圈。
- 在運行時,當動態變數可用時,管道結構已經固定,因此此時無法添加額外的任務。
解決方法
您不能回圈管道中的動態陣列變數;但是您可以這樣做的一個地方是在任務中,例如在powershell 腳本中:
- 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 任務都可用,但它可能是回圈這個陣列的唯一方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487519.html
標籤:电源外壳 天蓝色的devops 天蓝色管道
