Jenkins – return something from child job

automationJenkinspipelining

There is a jenkins pipeline job ("parent"). From it – on one stage there is called another pipeline job ("child" – using build job command).

Is there any way to return something (for example short text) from child to parent job without using external services like artificatory, and don't assuming that parent and child jobs are on the same machine?

Best Answer

One way you can do this is with Jenkins' built-in artifacts. I like to use JSON for this purpose since Pipeline has built-in readJSON and writeJSON methods.

For instance, here's what the configuration from the parent job might look like:

build job: "myproject", wait: true

step([
  $class: 'CopyArtifact',
  filter: 'mydata.json',
  projectName: "myproject",
])

if (fileExists("mydata.json")) {
  mydata = readJSON file: "mydata.json"
  myvalue = mydata.mykey
}

And then your child job would need to write the mydata.json file to the artifact store somewhere in its Pipeline job config, e.g.:

mydata = [mykey: 'myvalue']
writeJSON file: 'mydata.json', json: mydata

archiveArtifacts artifacts: 'mydata.json', onlyIfSuccessful: true