在自定义Azure DevOps任务/扩展中发送对象数组 [英] Send array of object inside custom Azure DevOps tasks / extensions
问题描述
我想为Azure DevOps创建一个自定义任务,并且希望将对象数组作为参数并在yaml中使用它:
I want to create a custom Task for Azure DevOps and I would like to have an array of object as parameters and use it like this in yaml:
data:
- field1: "data1"
field2: "data2"
- field1: "data3"
field2: "data4"
但是,在这种情况下,我没有找到任何可用的方法或示例,只有简单的数组,例如 string
数组.如何将来自Yaml的对象列表传递给我的自定义Azure DevOps任务?如果需要,请随时问我更多细节.
However I didn't found any avalable methods or examples for this kind of case, only simple array like string
array. How an I pass a listof object from yaml to my custom Azure DevOps task? Feel free to ask me some more details if needed.
例如,我要发送一个复杂的lisf对象:
For example, I want to send a complexe lisf of object:
data: # Data should be an array
- object:
file1: "/myDestinationPath"
file2: "/TheFilePathToSend"
override: true
- object:
file1: "/myDestinationPath2"
file2: "/TheFilePathToSend2"
override: false
...
然后我希望加载它,例如:
Then I expect to load it, something like:
let dataArray = task.getPathInput('data', true);
dataArray
可以在其中放置一个简单的JSON数组,以后将很容易进行管理.
Where dataArray
can ba a simple JSON array, it will be easy to manage after.
推荐答案
范德罗(Fandro),一个建议可能是改变组织参数和使用 taskgetDelimitedInput
函数的方式.
Fandro, a suggestion could be changing the way you organize your parameters and use taskgetDelimitedInput
function.
假设您在TypeScript中使用 Azure管道任务库并熟悉构建任务创建.
Assuming you are using Azure pipeline task lib in TypeScript and familiarity with Build task creation.
第1步)在 task.json
文件中使用定界符(例如 |
)定义变量.
Step 1) Define your variables using delimiter (e.g. |
) inside the task.json
file.
dataOrigin: "/myDestinationPath|/myDestinationPath2"
dataDestination: "/TheFilePathToSend|/TheFilePathToSend2"
dataOverwriteCriteria: "true|false"
第2步)编辑您的任务文件(例如 index.ts
),您可以使用
Step 2) Edit your task file (e.g. index.ts
) you may use taskgetDelimitedInput, it returns the string array.
步骤3)处理您的值:
let dataOriginArray = task.taskgetDelimitedInput('dataOrigin', "|");
let dataDestinationArray = task.taskgetDelimitedInput('dataDestination', "|");
let dataOverwriteCriteriaArray = task.taskgetDelimitedInput('dataOverwriteCriteria', "|");
这篇关于在自定义Azure DevOps任务/扩展中发送对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!