I am unable to read my custom environment variables from a specific 
projects previous builds.

stage("Test") {
    node("master) {
        env.TEST = "Hello World"
    }
}

stage("Previous") {    
    final def lastBuild = currentBuild.rawBuild.getPreviousBuild()
    final def upstreamEnv = lastBuild.getEnvironment(manager.listener)
    final def test = upstreamEnv['TEST']
    println "TEST="+test
}
TEST=null

1) Is this a bug?
2) Am I doing something wrong getting the environment variable from the 
previous build?
   Perhaps I must use a different listener than manager.listener, but I am 
not sure how to get one.
3) Is it as intended, in other word it is not implemented, storing custom 
build environment variables to the build?

The following work
    def previousBuild = currentBuild.previousBuild
    def upstreamEnv = previousBuild.getBuildVariables()
    final def test = upstreamEnv['TEST']


However I was using currentBuild here as an example. I am actually getting 
the previous build of a different project than the currentBuild project.
A WorkflowRun does not have a getBuildVariables() which RunWrapper does.

def getPreviousReleaseBuild(jenkinsProjectName) {
    def releaseBuild = null

    final def jenkinsInstance = jenkins.model.Jenkins.getInstance()
    final def jenkinsProject = jenkinsInstance.getItemByFullName(
jenkinsProjectName)
    if (jenkinsProject != null) {
        final def job = jenkinsProject.getAllJobs().first()
        final def lastBuild = job.getLastBuild()
        final def upstreamEnv = lastBuild.getEnvironment(manager.listener)
        final def buildRelease = upstreamEnv['BUILD_RELEASE']
        if (buildRelease) {
            releaseBuild = lastBuild
        }
    }

    return releaseBuild
}


I have also tried 
   lastBuild.getEnvVars()
   lastBuild.getCharacteristicEnvVars()
But neither of these contains the build environment variable I created in 
the pipeline script.


I could set up BUILD_RELEASE as a build environment property that I then 
can access with lastBuild.getEnvVars()
    properties([
        parameters([
            booleanParam(defaultValue: false, description: 'Build a release'
, name: 'BUILD_RELEASE')
        ])
    ])
However I do not want this build environment variable exposed to users when 
they "Build with Parameters".

-- 
You received this message because you are subscribed to the Google Groups 
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jenkinsci-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jenkinsci-users/fe1e6091-be4d-45da-9bbc-7f2f97655a52%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to