问题描述
在Azure DevOps管道模板中,我将参数声明为数组/序列
In an Azure DevOps pipeline template, I am declaring a parameter as an array/sequence
parameters:
mySubscription: ''
myArray: []
steps:
- AzureCLI@2
inputs:
azureSubscription: ${{ parameters.mySubscription }}
scriptType: pscore
scriptPath: $(Build.SourcesDirectory)/script.ps1
arguments: '-MyYAMLArgument ${{ parameters.myArray }}'
然后将参数值从管道定义传递为
Value for the parameter is then passed from pipeline definition as
steps:
- template: myTemplate.yml
parameters:
mySubscription: 'azure-connection'
myArray:
- field1: 'a'
field2: 'b'
- field1: 'aa'
field2: 'bb'
我的问题是我无法按YAML语法(类型为ToString()
)按原样传递该数组,以便能够在我的模板中使用PowerShell处理和处理该数组.尝试运行此管道时,出现以下错误:/myTemplate.yml (Line: X, Col: X): Unable to convert from Array to String. Value: Array
.错误消息中引用的行/列对应于模板中的arguments: '-MyYAMLArgument ${{ parameters.myArray }}'
.
My problem is I can't pass that array as-is in YAML syntax (kind of ToString()
) to be able to consume and treat that array from PowerShell in my template. When trying to run this pipeline, I get the following error:/myTemplate.yml (Line: X, Col: X): Unable to convert from Array to String. Value: Array
. The line/column referenced in the error message correspond to arguments: '-MyYAMLArgument ${{ parameters.myArray }}'
from my template.
我还尝试将参数映射为脚本的环境
I also tried to map the parameter as an environment for my script
- AzureCLI@2
inputs:
azureSubscription: ${{ parameters.mySubscription }}
scriptType: pscore
scriptPath: $(Build.SourcesDirectory)/script.ps1
arguments: '-MyYAMLArgument $Env:MY_ENV_VAR'
env:
MY_ENV_VAR: ${{ parameters.myArray }}
这也不起作用:/myTemplate.yml (Line: X, Col: Y): A sequence was not expected
.该时间轴/列指的是MY_ENV_VAR: ${{ parameters.myArray }}
.
This does not work too: /myTemplate.yml (Line: X, Col: Y): A sequence was not expected
. That time line/column refers to MY_ENV_VAR: ${{ parameters.myArray }}
.
是否有人面临过类似的要求,即将管道定义中定义的复杂类型(此处为对象的数组/对象序列)传递给PowerShell脚本?如果是这样,您是如何实现的?
Does anyone ever faced a similar requirement to pass complex types (here an array/sequence of object) defined from the pipeline definition to a PowerShell script? If so, how did you achieve it?
推荐答案
恐怕我们无法将复杂的DevOps管道模板参数传递给PowerShell脚本.
I am afraid we could not pass complex DevOps pipeline template parameters to a PowerShell script.
当前,Azure开发人员的任务仅支持一维数组的传输.它不能接受和传送二维数组.尽管我们可以定义二维数组的参数,但是我们需要使用以下脚本从模板扩展参数:
Currently, the task of Azure devops only supports the transfer of one-dimensional arrays. It cannot accept and transfer two-dimensional arrays. Although we can define the parameters of a two-dimensional array, but we need to extend the parameters from a template by the scripts like:
- ${{ each field in parameters.myArray}}:
我们可以像这样使用它:
We could use it like:
- ${{ each step in parameters.buildSteps }}:
#- ${{ each pair in step }}:
- task: PowerShell@2
inputs:
targetType : inline
script: |
Write-Host 'Hello World'
但是我们不能将二维数组直接传递给任务,例如:[field1: 'a', field2: 'b']
.这就是您收到错误Unable to convert from Array to String
的原因.
But we could not pass the two-dimensional arrays directly to the task like: [field1: 'a', field2: 'b']
. That the reason why you got the error Unable to convert from Array to String
.
您可以检查文档从模板扩展以获取更多详细信息.
You could check document Extend from a template for some more details.
希望这会有所帮助.
这篇关于如何将复杂的DevOps管道模板参数传递给脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!