Share variables across stages in Azure DevOps Pipelines

zooes picture zooes · Aug 13, 2019 · Viewed 10.4k times · Source

I am trying to figure out how to share custom variables across ADO pipelines in my script. Below is my script with 2 stages.

I am setting the curProjVersion as an output variable and trying to access it from a different stage. Am I doing it right?

stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: VersionCheck
    pool:
      vmImage: 'ubuntu-latest'
    displayName: Version Check
    continueOnError: false
    steps:

      - script: |
          echo "##vso[task.setvariable variable=curProjVersion;isOutput=true]1.4.5"
        name: setCurProjVersion
        displayName: "Collect Application Version ID"

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  variables:
    curProjVersion1: $[ dependencies.Build.VersionCheck.outputs['setCurProjVersion.curProjVersion'] ]
  jobs:
  - job: 
    steps: 
      - script: |
          echo $(curProjVersion1)

Answer

Merlin Liang - MSFT picture Merlin Liang - MSFT · Aug 14, 2019

Share variables across stages in Azure DevOps Pipelines

I'm afraid to say that it does not supported to share the variable which defined in one stage and pass it into another stage.

This is the feature we are plan to add, but until now, it does not supported. You can follow this Github issue, many people has the same demand with you. You can follow track that.

Until now, we only support set a multi-job output variable, but this only support YAML. For Classic Editor, there's no any plan to add this feature in release.

For work around, you can predefined the variables before the stages. But one important thing is if you change its value in one stage. The new value could not be passed to the next stage. The lifetime of variable with new value only exists in stage.

Updated:

Share variables across stages feature has been released in Sprint 168 now.

Please use below format to access output variables from previous stage:

stageDependencies.{stageName}.{jobName}.outputs['{stepName}.{variableName}']