I have a build flow pipeline (https://wiki.jenkins-ci.org/display/JENKINS/Build+Flow+Plugin) setup in Jenkins that spawns two or more children jobs each of which runs Junit tests.
def childjobs = []
// some logic to populate the children jobs array
// Run the jobs in parallel
parallel(childjobs)
I am trying to write a Groovy script in the context of the parent job using the Jenkins API to send a summary email from the parent job by collecting the summaries from the children.
How can I access the build information (success/failure, how many failures, duration, Junit results etc.) of the children jobs from the parent job? Conceptually something like this:
for (AbstractBuild<?,?> childjob in childjobs) {
// get build info from childjob
// get Junit results from childjob
}
It took me a while to play with the Build Flow and Jenkins API, and finally I got this:
import hudson.model.*
import groovy.json.*
// Assign a variable to the flow run
def flow = parallel(
{build("child-dummy", branch_name: "child1")},
{build("child-dummy", branch_name: "child2")}
)
println 'Main flow ' + flow.getClass().toString() + ', size: ' + flow.size()
flow.each { it -> // type = com.cloudbees.plugins.flow.FlowState
def jobInvocation = it.getLastBuild() // type = com.cloudbees.plugins.flow.JobInvocation
println 'Build number #' + jobInvocation.getNumber()
println 'Build URL ' + jobInvocation.getBuildUrl()
}
To obtain details of the run, such as artifacts etc. from the child jobs, use jobInvocation.getBuild()
method that returns a Run
instance.
Getting the Junit results should be easy by parsing the JSON result file as specified in How to get the number of tests run in Jenkins with JUnit XML format in the post job script?.