Azure Pipeline 动态参数从 YAML 管道传递到模板文件

Rom*_*kin 5 variables templates yaml dynamic azure-devops

我目前正在使用 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])"
Run Code Online (Sandbox Code Playgroud)

所以我希望我的迭代参数包含这样的内容:

iterations: ["1_60", "2_40"]
Run Code Online (Sandbox Code Playgroud)

在我的 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)
Run Code Online (Sandbox Code Playgroud)

工作场景

- 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"]
Run Code Online (Sandbox Code Playgroud)

如您所见,问题是我无法使用脚本的输出变量将其作为参数传递给我的模板。当我运行不工作的场景时,出现以下错误: 在此输入图像描述

我找到了这篇文章,但还没有解决方案......

Mer*_*SFT 4

正如4c74356b41所说,这就是目前的困境。换句话说,你提到的Notworking场景目前还不支持实现。

现在,我们必须在编译template时让他们知道明文。因为在这个编译期间,我们很难在一个步骤中同时做两件或更多的事情,特别是编译变量值、传递给相应的模板动态参数等。


需要一个序列或映射。实际值'$(calculateIterations.iterations)'

更详细的是,在编译时(单击“运行”之后但在管道真正启动之前):

1)首先我们映射来自YAML管道的值,以确保具有明确的启动- ${{ each i in parameters.iterations }}值。

2)完成后,然后按name: getKeyValues_${{i}}脚本顺序解析精确值。

在你的场景中,它甚至不能满足第一步,因为你传递的是一个变量,而我们这里没有解析值过程。这就是为什么你看到错误说的Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)'

此错误消息的另一种表达是:我们模板)期待精确的值来映射我们的动态参数,但您给出的是无法识别的内容$(calculateIterations.iterations)。抱歉,我们无法开始运行。