2

I'm trying to access an output variable from one stage in multiple consecutive stages.

The first time I access the variable I can access the value, but the next time I try it's an empty string.

In the example below B1 prints 'production' but C1 prints ''

trigger:
  - yaml-testing

pool:
  vmImage: windows-2019

stages:
- stage: A
  jobs:
  - job: A1
    steps:
      - task:  InlinePowershell@1
        name: setvar
        inputs:
          Script: |
            Write-Host "##vso[task.setvariable variable=Environment;isOutput=true]production"

- stage: B
  dependsOn: A
  variables:
    Environment: $[ stageDependencies.A.A1.outputs['setvar.Environment'] ]
  jobs:
  - job: B1
    steps:
      - task:  InlinePowershell@1
        name: printvar1
        inputs:
          Script: |
            Write-Host 'B1' 
            Write-Host $(Environment)

- stage: C
  dependsOn: B
  variables:
    Environment: $[ stageDependencies.A.A1.outputs['setvar.Environment'] ]
  jobs:
  - job: C1
    steps:
      - task:  InlinePowershell@1
        name: printvar2
        inputs:
          Script: |
            Write-Host 'C1' 
            Write-Host $(Environment)   

Logs look like this:

// Stage: B
Job preparation parameters
Variables:
  Environment:
    Parsing expression: <stageDependencies.A.A1.outputs['setvar.Environment']>
    Evaluating: stageDependencies['A']['A1']['outputs']['setvar.Environment']
    Result: 'production'

// Stage: C
Job preparation parameters
Variables:
  Environment:
    Parsing expression: <stageDependencies.A.A1.outputs['setvar.Environment']>
    Evaluating: stageDependencies['A']['A1']['outputs']['setvar.Environment']
    Expanded: Null
    Result: ''
Victor
  • 359
  • 1
  • 6
  • 14

1 Answers1

0

The difference is that stage B has an explicit dependency on stage A, so it can find out A1's output. But stage C has no explicit dependency on stage A, so A1's output is not available.

Try changing stage C like this:

- stage: C
  dependsOn:
    - A
    - B
Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56