0

I want to loop through the pipeline artifacts and pass them as variables to a task.

Followed this answer here, but no luck: https://stackoverflow.com/a/59451690/5436341

Have this powershell script in the build stage to get the artifact names and store them in a variable:

- task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          # Write your PowerShell commands here.
          
          Write-Host "Fetching value files"
          cd $(Build.ArtifactStagingDirectory)\MSI
          $a=dir -Filter "*.msi"
          $List = $a | foreach {$_}
          Write-Host $List
          $d = '"{0}"' -f ($List -join '","')
          Write-Host $d  
          Write-Host "##vso[task.setvariable variable=MSINames;isOutput=true]$d"
      name: getMSINames

And passing them as parameters to a template from another stage as below:

- stage: deployPoolsStage
  displayName: Deploy Pools
  dependsOn:
  - Build
  jobs:
  - job: CloudTest_AgentBased_Job
    displayName: 'CloudTest AgentBased Job'
    timeoutInMinutes: 120
    variables:
      MSIFiles: $[dependencies.Build.outputs['getMSINames.MSINames']]
    steps:
    - template: TestPipeline.yml
      parameters:
        files : $(MSIFiles)

Now, my template looks like this:

parameters:
  files : []

steps:
- ${{ each filename in parameters.files }}:
  - task: SomeTask
    inputs:
      Properties: worker:VsTestVersion=V150;worker:MSIFile=${{ filename }}
      displayName: 'tests'

Now this is failing with an error saying: "Expected a sequence or mapping. Actual value '$(MSIFiles)'". It's the same error even without using the template and directly accessing the variable in the original yml file.

Please let me know of a way to loop through my pipeline artifacts and pass them to my task.

Mounika
  • 371
  • 4
  • 18
  • Does this answer your question? [How to use a variable in each loop in Azure DevOps yaml pipeline](https://stackoverflow.com/questions/72528343/how-to-use-a-variable-in-each-loop-in-azure-devops-yaml-pipeline) – Vince Bowdren Nov 09 '22 at 10:14

1 Answers1

1

You are exporting the variable MSINames from a task in Build stage that needs to be inside a job (say BuildJob). Also, you are trying to access the exported variable from another stage but inside a job. Thus, you should use the format to read variable from another stage but within a job. What you have used is a wrong format in deployPoolsStage stage. Try correcting the format as below inside CloudTest_AgentBased_Job job,

variables:
      MSIFiles: $[stageDependencies.Build.BuildJob.outputs['getMSINames.MSINames']]

NOTE: I have assumed that the getMSINames task is defined inside the BuildJob job under Build stage according to what you have provided.

See docs related to this here.