繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM