Magikarp
Magikarp

Reputation: 502

Azure Pipeline: Passing variable to multiple stages

is it possible to declare a variable and then pass it downstream? I have an image below StageA -> StageB -> StageC where I obtain a url for my storage account on StageA, and I want to use it for both Stage B and StageC

But if I use the [stagedependencies.StageA.JobA.outputs['var'], it only works on StageB and not on StageC

- stage: 'StageC'
  dependsOn: 'StageB'
  pool:
    vmImage: 'windows-latest'
  variables:
    blobUri: $[stageDependencies.StageA.JobA.outputs['createOutput.blobUri']]
  jobs:
  - job: 'JobC'
    steps:
    - checkout: none
    - download: none
    - powershell: |
        echo JobBUri: $(blobUri)    

enter image description here

Maybe I missed it somewhere but does this mean you can only obtain the variable from the immediate stage you depend?

Example:

trigger:
- master

pool:
  vmImage: ubuntu-latest

stages:
- stage: 'StageA'
  jobs:
  - job: 'JobA'
    steps:
    - task: Powershell@2
      name: 'createOutput'
      inputs:
        targetType: 'inline'
        script: |
          Write-Output "##vso[task.setvariable variable=blobUri;isOutput=true]www.google.com"
- stage: 'StageB'
  dependsOn: 'StageA'
  pool:
    vmImage: 'windows-latest'
  variables:
    blobUri: $[stageDependencies.StageA.JobA.outputs['createOutput.blobUri']]
  jobs:
  - job: 'JobB'
    steps:
    - powershell: |
        echo JobBUri: $(blobUri)    

- stage: 'StageC'
  dependsOn: 'StageB'
  pool:
    vmImage: 'windows-latest'
  variables:
    blobUri: $[stageDependencies.StageA.JobA.outputs['createOutput.blobUri']]
  jobs:
  - job: 'JobC'
    steps:
    - powershell: |
        echo JobBUri: $(blobUri)    

Upvotes: 3

Views: 2409

Answers (2)

WaitingForGuacamole
WaitingForGuacamole

Reputation: 4301

The following illustrates that the answer from Kontekst is correct (I was going to edit the post to add this, but don't know if that's appropriate here?):

The following confirms and illustrates that this is the correct answer:

stages:
- stage: A
  jobs:
  - job: A1
    steps:
     - pwsh: Write-Host "##vso[task.setvariable variable=varFromA;isOutput=true]A"
       name: writevar

- stage: B
  dependsOn: A
  jobs:
  - job: B1
    variables: 
      varA: $[ stageDependencies.A.A1.outputs['writevar.varFromA'] ]
    steps:
     - pwsh: |
          Write-Host "##vso[task.setvariable variable=varFromB;isOutput=true]B"
          Write-Host "accesses $(VarA) from A"
       name: writevar

- stage: C
  dependsOn: 
  - A
  - B
  jobs:
  - job: C1
    variables: 
      varA: $[ stageDependencies.A.A1.outputs['writevar.varFromA'] ]
      varB: $[ stageDependencies.B.B1.outputs['writevar.varFromB'] ]
    steps:
     - pwsh: |
          Write-Host "##vso[task.setvariable variable=varFromC;isOutput=true]C"
          Write-Host "accesses $(varB) from B"
          Write-Host "accesses $(varA) from A"
       name: writevar

Upvotes: 2

Kontekst
Kontekst

Reputation: 1151

Try this and probably variable set in stage A will be available at stage C by stageDependencies.StageA

- stage: 'StageC'
  dependsOn:
  - StageA
  - StageB

Other workaround is to set again this variable as output variable in stage B and access it from C by stageDependencies.StageB(...)

From release notes

https://learn.microsoft.com/en-us/azure/devops/release-notes/2020/sprint-168-update#jobs-can-access-output-variables-from-previous-stages

By default, each stage in a pipeline depends on the one just before it in the YAML file. Therefore, each stage can use output variables from the prior stage. You can alter the dependency graph, which will also alter which output variables are available. For instance, if stage 3 needs a variable from stage 1, it will need to declare an explicit dependency on stage 1.

Upvotes: 5

Related Questions