Share variables across stages in Azure DevOps Pipelines

被刻印的时光 ゝ 提交于 2020-01-24 19:45:05

问题


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)

回答1:


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.




回答2:


You can define a global variable and use a Powershell to assign the value of the stage variable to the global variable.

Write-Output ("##vso[task.setvariable variable=globalVar;]$stageVar")

Global variables can either be defined in the yaml itself or in variable groups. Initialise the var with an empty value.

E.g. yaml

  variables:
    globalVar: ''



回答3:


You can define the variables just after you define the trigger and before you define the stages

trigger:
- master
  variables:
    VarA: aaaaa
    VarB: bbbbb
stages:
- stage: Build  
  jobs:
  - job: Build
    pool:
      vmImage: 'vs2017-win2016'


来源:https://stackoverflow.com/questions/57485621/share-variables-across-stages-in-azure-devops-pipelines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!