Storing custom build environment variables to build

180 views
Skip to first unread message

Sverre Moe

unread,
May 3, 2018, 6:49:47 AM5/3/18
to Jenkins Users
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.
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".

Sverre Moe

unread,
Jun 20, 2018, 5:56:38 AM6/20/18
to Jenkins Users
If anyone could assist I would appreciate it very much.

I need to be able to read the build properties of previous run builds of a different project.

I am checking all downstream dependencies of a project. I then need to find the last build that built a release.
Reply all
Reply to author
Forward
0 new messages