Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share variables across stages in Azure DevOps Pipelines

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) 
like image 696
zooes Avatar asked Aug 13 '19 21:08

zooes


People also ask

How do you use stages in Azure DevOps pipeline?

In Azure DevOps Server 2019, pools can only be specified at job level. This version of TFS doesn't support YAML pipelines. To add a stage to your release pipeline, select the release pipeline in Releases page, select the action to Edit it, and then select the Pipeline tab.


2 Answers

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}']  

Original:

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.

like image 105
Mengdi Liang Avatar answered Sep 28 '22 01:09

Mengdi Liang


What is important to mention stageDependencies is not available in condition at stage level. It is aviable in jobs, but not in stage directly (at least at the moment).

stages: - stage: A   jobs:   - job: JA     steps:     - script: |         echo "This is job Foo."         echo "##vso[task.setvariable variable=doThing;isOutput=true]Yes" #The variable doThing is set to true       name: DetermineResult     - script: echo $(DetermineResult.doThing)       name: echovar   - job: JA_2     dependsOn: JA     condition: eq(dependencies.JA.outputs['DetermineResult.doThing'], 'Yes')     steps:     - script: |         echo "This is job Bar."  #stage B runs if DetermineResult task set doThing variable n stage A - stage: B   dependsOn: A   jobs:   - job: JB     condition: eq(stageDependencies.A.JA.outputs['DetermineResult.doThing'], 'Yes') #map doThing and check if true     variables:       varFromStageA: $[ stageDependencies.A.JA.outputs['DetermineResult.doThing'] ]     steps:     - bash: echo "Hello world stage B first job"     - script: echo $(varFromStageA) 
like image 43
Krzysztof Madej Avatar answered Sep 28 '22 00:09

Krzysztof Madej