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"
}
}
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With