Templates Azure管道动态参数从YAML管道到模板文件
我目前正在使用Azure Devops构建管道,并尝试调用一个模板文件来执行构建yaml中的一些任务 我在向模板文件传递参数时遇到了一些困难。假设这是我的模板文件(简化),工作正常:Templates Azure管道动态参数从YAML管道到模板文件,templates,variables,dynamic,azure-devops,yaml,Templates,Variables,Dynamic,Azure Devops,Yaml,我目前正在使用Azure Devops构建管道,并尝试调用一个模板文件来执行构建yaml中的一些任务 我在向模板文件传递参数时遇到了一些困难。假设这是我的模板文件(简化),工作正常: parameters: iterations: [] steps: - ${{ each i in parameters.iterations }}: - task: PowerShell@2 displayName: "Get key values ${{i}}" name: getKeyValues
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管道中,我有以下代码(也简化了):
不工作场景
工作场景
如您所见,问题在于我无法使用脚本的输出变量将其作为参数传递给模板。
运行“不工作”场景时,出现以下错误:
我已经找到了这一点,但还没有解决办法……正如4c74356b41所说,这是目前的困境。换句话说,您提到的不起作用的场景直到现在都不支持实现 现在,我们必须让
模板在编译时知道明文。因为在这个编译过程中,我们很难在一个步骤中同时完成两件或两件以上的事情,特别是包含编译变量值、传递给相应的模板动态参数等
应为序列或映射。实际价值
“$(CalculateInteractions.iterations)”
更详细地说,在编译期间(在单击运行之后,但在管道真正启动之前):
1)首先,我们映射来自YAML管道的值,以确保-${{parameters.iterations}}
中的每个i都有一个清晰的值,即start
2)完成后,按脚本顺序解析name:getKeyValues{{{i}
上的精确值
在您的场景中,它甚至不能满足第一步的要求,因为您传递的是一个变量,我们这里没有解析值过程。这就是为什么您看到错误说需要序列或映射。实际值“$(calculationerations.iterations)
此错误消息的另一种表达方式是:我们(模板)期待精确的值来映射我们的动态参数,但您给出的是一个未识别的内容$(calculateInteractions.iterations)
。很抱歉,我们无法开始运行。我认为这将是一个运行时/编译时的两难问题,我认为这种方法不会像您所说的那样有效,我没有发现任何关于这个主题的内容……您使用这个@RomanLushkin有何收获?谢谢您的完整解释。我怀疑我的问题与您描述的问题有关,但不确定。这里有一个uservoice功能请求:
- 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"]