在 Azure DevOps Pipelines 中跨阶段共享变量 [英] Share variables across stages in Azure DevOps Pipelines

查看:23
本文介绍了在 Azure DevOps Pipelines 中跨阶段共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想弄清楚如何在我的脚本中跨 ADO 管道共享自定义变量.以下是我的 2 个阶段的脚本.

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

我将 curProjVersion 设置为输出变量并尝试从不同的阶段访问它.我做得对吗?

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)

推荐答案

更新:

跨阶段共享变量功能已在 Sprint 168 现在.

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

原文:

在 Azure DevOps Pipelines 中跨阶段共享变量

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.

这是我们计划添加的功能,但直到现在还不支持.你可以关注这个Github issue,很多人有同样的需求你.你可以跟踪那个.

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.

到目前为止,我们只支持设置一个 多任务输出变量,但这只支持YAML.对于经典编辑器,没有任何计划在发布中添加此功能.

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.

这篇关于在 Azure DevOps Pipelines 中跨阶段共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆