【问题标题】:Azure DevOps - Pass Powershell list variable to yml template arrayAzure DevOps - 将 Powershell 列表变量传递给 yml 模板数组
【发布时间】:2020-09-23 12:37:20
【问题描述】:

我想在 Powershell 任务中生成一个列表作为输出变量,并在 yml 模板中使用它作为列表来执行任务的循环。

#Main
- task: PowerShell@2
  condition: succeeded()
  displayName: "Create a list"
  inputs:
    targetType: 'inline'
    script: |

      $myList = New-Object System.Collections.ArrayList

      $myList.Add("wow")

      Write-Output ("##vso[task.setvariable variable=MyList;]$myList")

- template: myRandomListTaskTemplate.yml
  parameters:
      MyList: $(MyList)


#Template
parameters:
  MyList: []

steps:

- ${{ each myList in parameters.MyList }}:
    - task: PowerShell@2
      condition: succeeded()
      displayName: "WOW a list"
      inputs:
          targetType: 'inline'
          script: |
            Write-Host("A list string: ${{ myList }}")

我收到了错误

Expected a sequence or mapping. Actual value '$(MyList)'

注意:模板中的 Powershell 任务只是一个示例,它可以是其他与 Powershell 无关的任务(例如:DotNetCoreCLI@2)

【问题讨论】:

  • 这个问题怎么样?下面的答案是否解决了您的问题,如果没有,请告诉我有关此问题的最新信息吗?
  • 嗨@LeoLiu-MSFT,我仍在努力寻找实现这一目标的方法。
  • 真的很抱歉,我以为我发布了我的答案,但我似乎忘记了。希望这个迟到的答案能给你任何帮助,请检查我的回答是否有帮助,祝你有美好的一天。
  • 这个问题怎么样?下面的答案是否解决了您的问题,如果没有,请告诉我有关此问题的最新信息吗?

标签: powershell azure-devops yaml


【解决方案1】:

它不会像你期望的那样工作。

$myList = New-Object System.Collections.ArrayList

$myList.Add("wow")

Write-Output ("##vso[task.setvariable variable=MyList;]$myList")

你不会在这里有一个数组。一旦将其分配给变量,它将是一个纯字符串。这就是您收到此错误的原因。

如果你运行这个:

- template: myRandomListTaskTemplate2.yml
  parameters:
    MyList: 
    - item1
    - item2

一切顺利。但是,目前无法执行您的操作。

请查看this developer community topic

【讨论】:

    【解决方案2】:

    Azure DevOps - 将 Powershell 列表变量传递给 yml 模板数组

    正如 Krzysztof Madej 所说,你的 powershell 代码不能有一个数组。

    作为测试,我用 powershell 脚本创建了一个数组,然后将 Powershell 列表变量传递给 yml 模板数组:

    stages:
      - stage: Deploy_Cluster
        jobs:
        - job: A
          pool:
            name: MyPrivateAgent
          steps:
          - task: PowerShell@2
            condition: succeeded()
            displayName: "Create a list"
            inputs:
              targetType: 'inline'
              script: |
                  $id = '2,0,1,2'
                      Write-Host "##vso[task.setvariable variable=valuefiles;isOutput=true]$id"                
            name: fileoutput
    
    
        - job: B
          dependsOn: A
          pool:
            name: MyPrivateAgent
          variables: 
            allfiles: $[dependencies.A.outputs['fileoutput.valuefiles']]
          steps:
          - template: loopTemplate.yml
            parameters: 
              files : $(allfiles)
    

    我的测试loopTemplate.yml

    parameters:
      files: []
    
    steps :
    - task: PowerShell@2
      displayName: 'Fetching ValueFiles'
      inputs:
        targetType: 'inline'
        script: >
    
          foreach ($i in ${{parameters.files}})
          {
            Write-Host "filenames=$i"
          }
    

    我的测试结果:

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2022-06-29
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 2022-06-29
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      相关资源
      最近更新 更多