0

I'd like to have the result of a previous stage in a multi-stage pipeline available to me via a variable.

I know that it's possible to use the dependencies object to access the result of a previous stage via the dependency object (see: Expressions in Azure Pipelines).

For example, if I have a previous Stage called "deploy", then I'd be able to access the result as a condition for another stage, like: condition: in(dependencies.deploy.result, 'Skipped')

This works exactly as intended, however, I'd like to use this variable outside of condition, i.e. to echo the stage result in a bash task, or add it to a work item description.

However, this does not appear to be possible outside of conditions. If I want to use the result as a variable, or for an if query using the result as condition, it doesn't appear to work. For example the following code block, testString and anotherTestString end up being empty strings:

- stage: afterDeploy
  dependsOn: 
  - deploy
  condition: in(dependencies.deploy.result, 'Skipped')
  variables:
    testString: $[dependencies.deploy.result]
    ${{ if in(dependencies.deploy.result, 'Skipped') }}:
      anotherTestString: "Skipped"

I also tried using different ways to access the variable, i.e. ${{ dependencies.deploy.result }}, $(dependencies.deploy.result), or using stageDependency instead of dependency (stageDependencies.deploy.result).

Is there a nice way to access the previous stage results directly, or do I have to resort to getting the result via the REST API? Alternatively I could add an extra task exporting the status result as a new variable. Both solutions just don't strike me as optimal, since the stage result is technically available via the dependency object...

Exzelsios
  • 3
  • 1

1 Answers1

2

Testing in my side and I can see the same result, it seems that currently we cannot set variable to print stage result using keyword dependencies.deploy.result. One workaround is using API: Status - Get, see: How to get stage results from YAML pipelines in Azure DevOps for details.

Or you could add an extra task exporting the status result as a new output variable. See: Set a multi-job output variable for details.

stages:
- stage: A
  jobs:
  - job: A1
    steps:
     - bash: echo "##vso[task.setvariable variable=myStageOutputVar;isOutput=true]this is a stage output var"
       name: printvar

- stage: B
  dependsOn: A
  variables:
    myVarfromStageA: $[ stageDependencies.A.A1.outputs['printvar.myStageOutputVar'] ]
  jobs:
  - job: B1
    steps:
    - script: echo $(myVarfromStageA)
Edward Han-MSFT
  • 2,879
  • 1
  • 4
  • 9
  • Alright, that was what I was expecting too, thanks for confirming it. I ended up using an extra task to export the stage result. It works, I'm just not a big fan of it. Thanks a lot regardless! – Exzelsios May 03 '21 at 11:06
  • It is actually also required to use this approach. To directly access the `stageDependencies` inside a task is not possible, it must be made available as a variable before. – kap Apr 06 '22 at 08:12