Vsts将变量作为对象数组传递

问题描述 投票:0回答:1

我正在尝试传递变量以将管道释放为对象数组。目标是在我的appsettings.json中有类似

"myArrayObject": [
      {
        "host": "localhost:2948",
        "protocol": "http"
      }
    ]

在变量选项卡中,我创建了名为myArrayObject的变量并分配给它

[{'host':'someUrl','protocol':'https'},{'host':'localhost:44394','protocol':'https'}]

以某种方式释放该变量,因此我将该脚本添加到管道中

powershell -command "&{
 $json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
 $json.myArrayObject = '$(myArrayObject)';
 $json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json'; 
 }"

但是后来我得到了错误

Missing type name after '['.
At line:1 char:129
+ ... llowedUris = [{'host':'someUrl','protoc ...
+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token ':'someUrl'' in expression or 
statement.
At line:1 char:169
+ ... lowedUris = [{'host':'someUrl','protoco ...

有什么方法可以实现?将对象数组作为变量传递?

我也尝试过这种方法:

powershell -command "&{
 $json = Get-Content '.\appsettings.json' -raw | ConvertFrom-Json;
 $json.myArrayObject = '[{"host":"someUrl","protocol":"https"},{"host":"localhost:44394","protocol":"https"}]';
 $json | ConvertTo-Json -Depth 32 | Set-Content '.\appsettings.json'; 
 }"

但是最后我得到了:

"myArrayObject" : "[{host:someUrl,protocol:https},{host:localhost:44394,protocol:https}]"
powershell azure-devops azure-pipelines-release-pipeline azure-artifacts
1个回答
0
投票

检查情况How to pass complex DevOps pipeline template parameter to script

当前,Azure开发人员的任务仅支持 一维数组。它不能接受和传递二维 数组。虽然我们可以定义二维参数 数组,我们需要通过脚本扩展模板的参数 像:

- ${{ each field in parameters.myArray}}:

我们可以像这样使用它:

- ${{ each step in parameters.buildSteps }}:
  #- ${{ each pair in step }}:

    - task: PowerShell@2
      inputs:
        targetType : inline
        script: |
          Write-Host 'Hello World'

我们无法将二维数组直接传递给任务 像:[field1: 'a', field2: 'b']

您可以再检查文档Extend from a template 详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.