[英]Azure Pipeline dynamic parameters to template file from YAML pipeline
我目前正在使用 Azure Devops Build Pipelines,並嘗試調用模板文件從我的構建 yaml 中執行一些任務。
我在將參數傳遞給模板文件時遇到了一些困難。 假設這是我的模板文件(簡化),它工作正常:
parameters:
iterations: []
steps:
- ${{ each i in parameters.iterations }}:
- task: PowerShell@2
displayName: "Get key values ${{i}}"
name: getKeyValues_${{i}}
inputs:
targetType: 'inline'
script: |
$item = "${{i}}"
Write-Host "item : $($item)"
$keyVal = $item -split "_"
Write-Host $keyVal
Write-Host "key: $($keyVal[0]) | value: $($keyVal[1])"
echo "##vso[task.setvariable variable=key;isOutput=true]$($keyVal[0])"
echo "##vso[task.setvariable variable=value;isOutput=true]$($keyVal[1])"
所以我希望我的迭代參數包含如下內容:
iterations: ["1_60", "2_40"]
在我的 Yaml 管道中,我有以下代碼(也簡化了):
- task: PowerShell@2
displayName: Calculate iterations for $(copies) copies
name: calculateIterations
inputs:
targetType: 'inline'
script: |
# Do some stuf here to get the arrow below from int value = 100
$iterations = ["1_60, "2_40"]
echo "##vso[task.setvariable variable=iterations;isOutput=true]$($iterations)"
- template: container-template.yml
parameters:
iterations: $(calculateIterations.iterations)
- task: PowerShell@2
displayName: Calculate iterations for $(copies) copies
name: calculateIterations
inputs:
targetType: 'inline'
script: |
# Do some stuf here to get the arrow below from int value = 100
$iterations = ["1_60, "2_40"]
echo "##vso[task.setvariable variable=iterations;isOutput=true]$($iterations)"
- template: container-template.yml
parameters:
iterations: ["1_60, "2_40"]
如您所見,問題在於我無法使用腳本的輸出變量將其作為參數傳遞給模板。 當我運行不工作的場景時,出現以下錯誤:
我找到了這篇文章,但還沒有解決方案......
正如4c74356b41所說,這是目前的困境。 換句話說,您提到的Not working 場景直到現在都不支持實現。
現在,我們必須在編譯時讓template
知道明文。 因為在這個編譯期間,我們很難在一個步驟中同時做兩件或更多的事情,特別是包含編譯變量值,傳遞給相應的模板動態參數等。
需要一個序列或映射。 實際值 '$(calculateIterations.iterations)'
更詳細地,在編譯期間(在您單擊運行之后但在管道真正啟動之前):
1)首先我們映射來自 YAML 管道的值,以確保- ${{ each i in parameters.iterations }}
具有明確的start值。
2)完成后,按腳本順序解析name: getKeyValues_${{i}}
上的確切值name: getKeyValues_${{i}}
。
在你的場景中,它甚至不能滿足第一步,因為你傳遞的是一個變量,我們這里沒有解析值過程。 這就是為什么您看到錯誤提示“ Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)'
Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)'
。
此錯誤消息的另一種表達方式是:我們(模板)期待精確值來映射我們的動態參數,但是您提供的是無法識別的內容$(calculateIterations.iterations)
。 抱歉,我們無法開始運行。
經過多次嘗試,我在stackoverflow中得到了這個問題,並知道在Azure Pipeline中沒有辦法實現這一點。 可憐的。 等待 Azure DevOps 提供解決此問題的方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.