0

Assume I have the following downstream job:

// DOWNSTREAM JOB
DYNAMIC_VAR = ""

parallel(
  {
    DYNAMIC_VAR = new Date()    // Some other value determined 
                                // at runtime by this job
  },
  { 
    // Some other stuff...
  }
)

As part of my upstream job (see example below) I want to be able to call the downstream job, and access the variable that was set during the downstream job.

// UPSTREAM JOB 
my_build = build("my-custom-job")

// Would like to beable to do something like
// out.println my_build.build.get_var('DYNAMIC_VAR')
//  or 
// out.println my_build.build.DYNAMIC_VAR

Looking through the output it seems that the variable is not returned, and hence is not accessible. I suspect this is because the variable in question (DYNAMIC_VAR) is only available during the scope of the downstream job, and hence once the job finishes the variable is removed.

My two questions I wanted to ask were:

  1. Is it correct that the variables are removed upon job completion?
  2. Does anyone have an idea how this could (if it can) be achieved (additional plugins are fine if required)?
clangers
  • 566
  • 1
  • 6
  • 13

2 Answers2

0

1) Would outputting the variable=value pair to some file be an acceptable solution for you?

2) I haven't used groovy in Jenkins much, but all the job's environment variables are stored under:

${JENKINS_HOME}/jobs/${JOB_NAME}/builds/${BUILD_NUMBER}/injectedEnvWars.txt

This may or may not require EnvInject plugin.

Zloj
  • 2,235
  • 2
  • 18
  • 28
0

According to the comments here: https://issues.jenkins-ci.org/browse/JENKINS-18784

You can do the following:

// – In job First, I am setting the environment variable testKey    
b = build( "First" ) 

// Then, using it in workflow: 
out.println b.build.properties.environment['testKey']
// Or 
b.build.properties.environment.testKey
Clintm
  • 4,505
  • 3
  • 41
  • 54