简体   繁体   English

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

[英]Azure Pipeline dynamic parameters to template file from YAML pipeline

I am currently working with Azure Devops Build Pipelines, and am trying to call a template file to do some tasks from my build yaml.我目前正在使用 Azure Devops Build Pipelines,并尝试调用模板文件从我的构建 yaml 中执行一些任务。

I am facing some difficulties to pass parameters to the template file.我在将参数传递给模板文件时遇到了一些困难。 Let assume that this is my template file (simplified) which works fine :假设这是我的模板文件(简化),它工作正常:

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])"

So I want my iterations parameter contain something like this :所以我希望我的迭代参数包含如下内容:

iterations: ["1_60", "2_40"]

Inside my Yaml pipeline I have the following code(also simplified) :在我的 Yaml 管道中,我有以下代码(也简化了):

Not working scenario不工作场景

- 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)

Working scenario工作场景

- 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"]

As you can see, the problem is that I am unable to use the output variable of my script to pass it as parameter to my template.如您所见,问题在于我无法使用脚本的输出变量将其作为参数传递给模板。 When I run the not working scenario I have the following error :当我运行不工作的场景时,出现以下错误: 在此处输入图片说明

I have found this post , but no solutions yet...我找到了这篇文章,但还没有解决方案......

As what 4c74356b41 said, this is the dilemma at present.正如4c74356b41所说,这是目前的困境。 In another word, the Not working scenario you mentioned does not support to achieve until now.换句话说,您提到的Not working 场景直到现在都不支持实现。

Now, we must let the template know the clear text at compile time.现在,我们必须在编译时让template知道明文。 Because during this compile time, we have difficulty to do 2 or more things in one step at same time, especially contain compiling variable value, passing to corresponding template dynamic arguments and etc.因为在这个编译期间,我们很难在一个步骤中同时做两件或更多的事情,特别是包含编译变量值,传递给相应的模板动态参数等。


Expected a sequence or mapping.需要一个序列或映射。 Actual value '$(calculateIterations.iterations)'实际值 '$(calculateIterations.iterations)'

More detailed, during compile time ( after you click Run but before pipeline start truely ):更详细地,在编译期间(在您单击运行之后但在管道真正启动之前):

1) Firstly we map the value that come from YAML pipeline, to make sure the - ${{ each i in parameters.iterations }} has clear value to start . 1)首先我们映射来自 YAML 管道的值,以确保- ${{ each i in parameters.iterations }}具有明确的start值。

2) After it is done, then parse exact value on name: getKeyValues_${{i}} in script order. 2)完成后,按脚本顺序解析name: getKeyValues_${{i}}上的确切值name: getKeyValues_${{i}}

In your scenario, it cannot even satisfy the first step since what you passed is a variable, and we do not has parse value process here.在你的场景中,它甚至不能满足第一步,因为你传递的是一个变量,我们这里没有解析值过程。 That's why you saw error said Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)'这就是为什么您看到错误提示“ Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)' Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)' . Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)'

Another expression of this error message is: we ( template ) are looking forward to exact value(s) to map our dynamic parameters, but what you give is a unrecognized content $(calculateIterations.iterations) .此错误消息的另一种表达方式是:我们模板)期待精确值来映射我们的动态参数,但是您提供的是无法识别的内容$(calculateIterations.iterations) Sorry, we cannot start to run.抱歉,我们无法开始运行。

After lot of attempts, I got this question in stackoverflow and came to know that there is no way to achieve this in Azure Pipeline.经过多次尝试,我在stackoverflow中得到了这个问题,并知道在Azure Pipeline中没有办法实现这一点。 Pathetic.可怜的。 Waiting from Azure DevOps to give a way for this problem等待 Azure DevOps 提供解决此问题的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将参数值从模板 yaml 文件传递到主管道 - How to pass parameters value from a template yaml file to main pipeline 需要使用 For 循环而不是 Azure 管道模板 yaml 中的每个循环 - Need to use For loop not for each loop in Azure Pipeline Template yaml 如何在 Azure 管道部署中的另一个模板中的 2 个不同任务之间包含 yaml 模板 - How to include a yaml template between 2 different tasks in the another template in Azure pipeline deployment 使用多个管道参数调用模板 - Calling a template with several pipeline parameters Azure DevOps 模板管道函数 - Azure DevOps template pipeline functions Azure DevOps 管道模板 - 如何连接参数 - Azure DevOps pipeline template - how to concatenate a parameter 从 yaml 管道传递的 Helm Concat 模板参数 - Helm Concat tamplate parameter which passed from yaml pipeline 如何将调度程序和触发器添加到 Azure DevOps 管道中的嵌套模板? - How to add Scheduler and Trigger to a nested template in Azure devops pipeline? Azure DevOps Pipeline:一个阶段两次使用相同的模板 - Azure DevOps Pipeline: same template twice in one stage 无法从模板化管道参数访问Jenkins参数 - Cannot access Jenkins parameters from a templatized pipeline argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM