[英]Is there a way to parametrize/dynamically set variable group names in Azure DevOps Pipelines YAML?
[英]Is there a way to dynamically assign names to tasks in yaml pipelines for Azure Devops?
我正在使用 DownloadPipelineArtifact@2 任務並循環遍歷一系列管道以下載每個管道的工件。 我想在后續任務中使用每個任務的 $(BuildNumber) 輸出。
${{ each p in pipelines }}:
- task: DownloadPipelineArtifact@2
displayName: 'Download ${{ p.alias }} Artifact - ${{ p.artifact }}'
name: 'Download_${{ p.alias }}'
inputs:
buildType: specific
project: $(System.TeamProjectId)
pipeline: ${{ p.id }}
preferTriggeringPipeline: false
runVersion: latestFromBranch
runBranch: 'refs/heads/${{ p.branch }}'
artifact: ${{ p.artifact }}
patterns: ${{ p.patterns }}
targetPath: '$(Pipeline.Workspace)/artifacts/${{ p.alias }}/${{ p.artifact }}'
不幸的是,我無法這樣做,因為我需要通過任務名稱來引用任務,並且我無法為任務分配動態名稱。 我收到一條錯誤消息
有效名稱只能包含字母數字字符和“_”,並且不能以數字開頭。
我嘗試使用運行時變量、表達式和名稱計數器,但都失敗並出現相同的錯誤。 我在這里假設名稱字段不是動態分配的。
沒有名稱字段一切正常。 但據我了解,我需要任務的名稱來引用它並提取它的輸出。 就像是:
{{ taskName.BuildNumber }}
如果有人知道我可以動態分配名稱或通過它為循環中的每個任務提取 $(BuildNumber) 的任何方式,請告訴我。 謝謝!
看起來你可以做到,這家伙讓它工作1 ...只要迭代數組在 YAML 編譯時是已知的。
這對我有用(必須清理名稱才能獲得 /A-z0-9_/ ):
- ${{ each environmentFull in parameters.environmentFullList }}:
- bash: |
echo "##vso[task.setvariable variable=environment]$(echo ${{ environmentFull }} | cut -d. -f1)"
echo "##vso[task.setvariable variable=namespace]$(echo ${{ environmentFull }} | cut -d. -f2)"
failOnStderr: true
displayName: Important pipeline variables
- bash: |
echo "Namespace: $(namespace)"
echo "Environment: $(environment)"
echo "Task name: kubectl_${{ replace(replace(environmentFull,'.','_'),'-','_') }}"
failOnStderr: true
displayName: Print info
- task: Kubernetes@1
name: kubectl_${{ replace(replace(environmentFull,'.','_'),'-','_') }}
inputs:
connectionType: 'Kubernetes Service Connection'
kubernetesServiceEndpoint: '${{ environmentFull }}'
namespace: '$(namespace)'
command: 'get'
arguments: 'pods'
secretType: 'dockerRegistry'
containerRegistryType: 'Azure Container Registry'
outputFormat: 'jsonpath=''{range .items[*].status.containerStatuses}{@}{end}'''
displayName: "Get all pods"
- bash: |
export TASKNAME_LC="kubectl_${{ replace(replace(environmentFull,'.','_'),'-','_') }}_KubectlOutput"
export TASKNAME_UC=${TASKNAME_LC^^}
echo "====Using name: $TASKNAME_UC"
export TASK_VALUE=${!TASKNAME_UC}
echo "$TASK_VALUE"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.