Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"
              }
            }
          }
        }
like image 502
Michael Holman Avatar asked Oct 19 '25 20:10

Michael Holman


1 Answers

Also needed to get list of all variables from previous stages, and in result came up with solution by converting stageDependencies to JSON in pipeline and then back to object in Powershell.

Here is the example of pipeline that sets 2 variables in first stage and reads them in second stage by initial variable name - someVariable and another.variable.with.dots without need to use stage/job/step names.

trigger:
  - master

stages:
  - stage: SetVars
    jobs:
      - job: SetVarsJob
        steps:
          - script: echo "##vso[task.setvariable variable=someVariable;isOutput=true]firstVarValue"
          - script: echo "##vso[task.setvariable variable=another.variable.with.dots;isOutput=true]secondVarValue"
  - stage: ReadVars
    dependsOn: SetVars
    jobs:
      - job: ReadVars
        variables:
          stageDependenciesSerialized: $[convertToJson(stageDependencies)]
        steps:
          - powershell: |
              # extract only initial variables as hashtable
              function ExtractHashtableByVariableNames {
                  param ( [Parameter(Position=0)] [string] $stageDependenciesJson )

                  $result = @{}
                  $stageDependencies = $stageDependenciesJson | ConvertFrom-Json
                  foreach($stage in $stageDependencies.psobject.Properties) {
                      foreach($job in $stage.Value.psobject.Properties) {
                          foreach($step in $job.Value.outputs.psobject.Properties) {
                              $varName = $step.Name.Substring($step.Name.IndexOf('.') + 1)
                              $result[$varName] = $step.Value
                          }
                      }
                  }
                  return $result
              }
              $myHashtable = ExtractHashtableByVariableNames '$(stageDependenciesSerialized)'
              Write-Host $myHashtable["someVariable"]
              Write-Host $myHashtable["another.variable.with.dots"]

In the result it prints

firstVarValue
secondVarValue
like image 53
Daniil B Avatar answered Oct 22 '25 13:10

Daniil B