templates - 从 YAML 管道到模板文件的 Azure Pipeline 动态参数

标签 templates variables dynamic azure-devops yaml

我目前正在使用 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"]

如您所见,问题在于我无法使用脚本的输出变量将其作为参数传递给模板。
当我运行不工作的场景时,出现以下错误:
enter image description here

我找到了这个 post ,但还没有解决方案...

最佳答案

正如4c74356b41所说,这是目前的困境。换句话说,您提到的 Not working 场景直到现在都不支持实现。

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

Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)'



更详细地说,在编译期间(在您单击 运行 之后,但在管道真正启动之前):

1) 首先我们映射来自 YAML 管道的值,以确保 - ${{ each i in parameters.iterations }} 有明确的值(value)开始 .

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

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

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

关于templates - 从 YAML 管道到模板文件的 Azure Pipeline 动态参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60614248/

相关文章:

c++ - 以模板作为模板参数的函数

c++ - 在共享对象/DLL 中使用模板化类和函数

c++ - 减少类的模板参数数量

mysql使用带有正则表达式的变量

javascript - Node 中带有 Express 框架的动态路由

templates - 使用 Scala Play 在 tempplace 上进行复杂模式匹配

javascript - 简单的 jQuery 函数在 for 循环后仅返回零

android - 在字符串中使用变量,执行 Html.fromHtml

java - 分词和排列之间的时间复杂度差异

c++ - 读取txt文件的行和列的最佳方法