Is there a way to loop over all outputs from stageDependencies in Azure DevOps?

Viewed 153

I want to loop over all the outputs of dependent stages and check if any of them are set to a specific value which would indicate there was a problem. Is this possible, and if so, how?

I'm having trouble finding any relevant documentation on stageDependencies outside of using them in job conditions. I know the code below doesn't work, but I've tried a few variations on this and I'm not even seeing the stageDependencies object.

- task: PowerShell@2
  displayName: Check for Failures
    inputs:
      targetType: inline
      script: |
        foreach ($dependency in $[stageDependencies]) {
          foreach ($job in $dependency) {
            foreach ($output in $job.outputs) {
              if ("$output" -eq "Started") {
                write-host "##vso[task.logissue type=error]Task failed in a previous step"
              }
            }
          }
        }
1 Answers

The pipeline can output variables through the logging command, but the variables can only be strings:

https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#set-variables-in-scripts

All variables set by this method are treated as strings.

It is not difficult to parse a string and put it into an array, just use the function(split()) that comes with the string type to split and restore.

Here is an example:

trigger:
- none

# 1
stages:
- stage: s1
  displayName: setvars
  jobs:
    - job: testJob
      steps:
        - task: PowerShell@2
          name: setvar
          inputs:
            targetType: 'inline'
            script: |
              # logic here. For example you get the vars and put it into this format:
              $testvars = "testvar1,testvar2,testvar3" 
              Write-Host "##vso[task.setvariable variable=outputvars;isOutput=true]$testvars"


# 2
- stage: s2
  displayName: getvars
  dependsOn: s1  
  variables:
    vars: $[ stageDependencies.s1.testJob.outputs['setvar.outputvars'] ]
  jobs:
    - job:
      steps:  
      - task: PowerShell@2
        inputs:
          targetType: 'inline'
          script: |
            $varsArr = "$(vars)".Split(',')
            foreach ($var in $varsArr)
            {
              Write-Host "$var`r`n"
              if($var -eq 'testvar1')
              {
                Write-Host 'The value is correct.'
              }
            }

Result:

enter image description here

Related