2

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?

undefine
  • 956
  • 8
  • 20
  • You can set a global variable in a stage with readFile (cheap), or use stash/unstash to carry around the environment between nodes (expensive). – Andrew Domaszek Nov 07 '17 at 02:46

1 Answers1

4

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
jayhendren
  • 917
  • 4
  • 11