如何访问我从上次成功构建中定义的环境变量

如何访问我从上次成功构建中定义的环境变量

本文介绍了如何访问我从上次成功构建中定义的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从另一个作业'B'访问我在作业'A'中定义的环境变量

I am trying to access an environment variable that I defined in the job 'A' from another job 'B'

作业'A'通过-定义它

job 'A' defines it by -

evn.upload_loaction = "loc"

在作业B中,我试图访问作业A的最后一次成功构建并获取该变量-

In job B I am trying to access the last successful build of JOb A and get that variable -

        def item = Jenkins.instance.getItem("deploy-artifact-pipeline")
        def dev_deployed_build=item.getLastSuccessfulBuild()
        def envVars= dev_deployed_build.getEnvVars()
        echo envVars['upload_loaction']   // prints null
        echo envVars['BUILD_NUMBER'] // prints 21

无法识别我的自定义变量,但可以使用诸如build_number之类的通用变量.

My custom variable is not recognized but generic ones like build_number is available.

当我将作业A触发为下游作业时,可以使用-

When I trigger Job A as downstream job then I can access using -

def jLz = build (job: 'deploy-artifact-pipeline')
echo jLz.buildVariables.PROCESSOR_UPLOAD_LOCATION // prints loc

有人可以帮我吗?还是有更好的方法来存储和访问先前版本中的变量?

Can someone help me with this? Or is there a better way to store and access that variable from a previous build ?

推荐答案

EnvInject插件和Pipeline作业不能并行进行,如果需要存储一些环境变量,我通常更喜欢将该作业作为自由样式的作业,其中您可以发送env变量以将其存储在构建的末尾,并且可以使用"injectedEnvVars/api/json"来访问特定的构建

EnvInject plugin and Pipeline job doesn't go hand in hand, if some environment variable needs to be stored I usually prefer that job to be a free style job where you can send your env variable to be stored at the end of the build and that can be accessed for the specific build with "injectedEnvVars/api/json"

这篇关于如何访问我从上次成功构建中定义的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:29