How to easily get variables from a downstream Jenkins job

Kobi Rosenstein
1 min readNov 7, 2024

--

I was absolutely stunned by how hard this seems.

But the answer isn’t difficult, just hard to find.
First, let’s define the problem.

Say you have many jobs, each which performs one discrete action.
To tie the various actions together, you create a “parent” job, which triggers all the “action” jobs, which we’ll call children.

Say one of the child jobs sets a variable which we need to later use in a different child job, for instance, if a job creates a dynamically named object such as a VM, and a later job connects to the new VM and performs some Ansible task on it.

Because child jobs will each create and clean their own workspace, we can use environment variables in the child, which we can then catch with the parent.

For example, let’s say the child job creates a variable called newServerIP, we can do (in any part of the child job):

env.newServerIP = newServerIP

To then retrieve the value, the simplest way I have found is as follows:

stage('Run child job') {
steps {
childJob = build job: 'Folder/job_name',
parameters: [
string(name: 'NAME', value: 'Kobi'),
]
newServerIP = childJob?.buildVariables?.newServerIP
println(newServerIP)

Naturally, we can then do whatever we want with this variable, including sending it down to sibling jobs.

--

--

Kobi Rosenstein
Kobi Rosenstein

Written by Kobi Rosenstein

Devops engineeer. This blog chronicles my “gotcha” moments — Each post contains an answer I would have like to have found when trawling google.

No responses yet