azure-devops - 如何将复杂的 DevOps 管道模板参数传递给脚本
问题描述
在 Azure DevOps 管道模板中,我将参数声明为数组/序列
parameters:
mySubscription: ''
myArray: []
steps:
- AzureCLI@2
inputs:
azureSubscription: ${{ parameters.mySubscription }}
scriptType: pscore
scriptPath: $(Build.SourcesDirectory)/script.ps1
arguments: '-MyYAMLArgument ${{ parameters.myArray }}'
然后参数的值从管道定义传递为
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 }}'
于我的模板。
我还尝试将参数映射为我的脚本的环境
- 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 }}
.
有没有人遇到过类似的要求,将管道定义中定义的复杂类型(这里是对象的数组/序列)传递给 PowerShell 脚本?如果是这样,您是如何实现的?
解决方案
您现在可以使用 convertToJson
ADO 管道中的函数将这些类型的参数转换为字符串:
parameters:
- name: myParameter
type: object
default:
name1: value1
name2: value2
...
- task: Bash@3
inputs:
targetType: inline
script: |
echo "${{ convertToJson(parameters.myParameter) }}"
convertToJson:https ://docs.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#converttojson
推荐阅读
- pyspark - 使用 pyspark 执行存储在数据框中的 SQL
- lua - 如何从 LUA 表中选择多个随机元素?
- c++ - MS Visual c++“源文件的数量和相应的输出必须匹配”
- javascript - 如何选择单击元素后生成的元素(回调?)?
- javascript - 如果我想为不同的用户进行某些工作流治理,我是否必须将其合并到我的数据模型中?
- java - 除了 CPU 和内存,线程还竞争哪些资源
- django - 使用 django 动态创建文件
- node.js - 如何解决 Angular 12 应用程序创建问题
- javascript - 在我的 Javascript 文件中传播不可交互实例的尝试无效?
- python - 如何在 FastAPI 中建立 WebSocket 安全(wss)连接