Working off of the existing answers and the Jenkins example documentation I came up with the following nested parallel solution. The Stage View (2.19) plugin and Blue Ocean (1.24.5) do not display them as you would hope, so I used the Groovy Postbuild (2.5) plugin (whitelisted) to show a result summary on the build page of the Classic View (the Badge plugin may work as well).
Current environment is using Jenkins LTS 2.277.2, a main "trigger" job, and 4 downstream jobs that build the pieces of my application, all of which are Multibranch pipelines.
I added a method to trigger my downstream Multibranch Pipeline jobs, since they all do the same thing. This is at the end of my Jenkinsfile.
// In case you want to work on the results of the downstream jobs
buildResults = []
// Common code to trigger a downstream job
// Outputs a URL that you can follow in Blue Ocean and Classic View
def performTriggerJob(String jobID) {
// We need to wrap what we return in a Groovy closure, or else it's invoked
// when this method is called, not when we pass it to parallel.
// To do this, you need to wrap the code below in { }, and either return
// that explicitly, or use { -> } syntax.
return {
GIT_BRANCH_FIXUP = env.BRANCH_NAME.replace("/","%2F")
// propagate: false so we can work after it returns. Try/catch will also work
res = build( job: "../${jobID}/${GIT_BRANCH_FIXUP}", propagate: false)
echo "${jobID}: ${res.getResult()} - ${res.buildVariables.get('RUN_DISPLAY_URL')}"
// Add to buildResults
buildResults << res
text = "Downstream Run Result: <a href=\"${res.buildVariables.get('RUN_DISPLAY_URL')}\">${jobID} - ${res.getResult()}</a>"
if (res.getResult().equals("SUCCESS")) {
manager.createSummary("green.gif").appendText(text)
} else {
manager.createSummary("red.png").appendText(text)
error 'JOB FAILED' // this fails the stage
}
}
}
Then in the main pipeline of the main Triggering job, I use script
blocks to start the nested parallel jobs.
pipeline {
agent any
stages {
stage('Build Nested parallel') {
steps {
script {
// Runs all these builds in parallel, the 2 nested Jobs
// have a requirement on the cmake build, but can also run in
// parallel after that finishes. Declarative cannot nest, but
// Scripted can.
// This is a map of 'name': action
// performTriggerJob returns a Closure which is why this is just a method "call"
// as Groovy will invoke .call() on parallel 'actions' automatically
def builds = [
"subJob1": performTriggerJob('subJob1'),
"subJob2": performTriggerJob('subJob2'),
"subJob3": { script {
// Since we have more work to do than just trigger a job,
// we have to .call() this Closure directly in the script block to get it to run
performTriggerJob('subJob3').call()
// You can also pass a map directly into the parallel
parallel (
'nestedJob1': performTriggerJob('nestedJob1'),
'nestedJob2': performTriggerJob('nestedJob2')
)
} }
]
parallel builds
}
}
}
// Other stages
}
}
Since performTriggerJob
adds the results of the jobs to the variable buildResults
we can do something with all the results once everything is done. I copy all artifacts from the last successful build and then zip them up so they're available on the trigger job.
buildResults
is a list of (what I believe are) RunWrapper objects, so the methods in this javadoc are what you can call on this object.
// Other stages
stage("Post steps") {
steps {
script {
for( finishedBuildObj in buildResults ) {
// Obj to use. Could be this build or the last successful build
buildObj = finishedBuildObj
projectName = buildObj.getFullProjectName()
if (buildObj.getResult() != "SUCCESS") {
buildObj = buildObj.getPreviousSuccessfulBuild()
}
copyArtifacts(
projectName: projectName,
// Allow this build to continue even if no build is found matching the "Which build" condition,
// the build's workspace does not exist or is inaccessible, or no artifacts are found matching the specified pattern.
// By default this build step fails the build if no artifacts are copied.
optional: true,
// Ignore the directory structure of the artifacts in the source project and copy all matching artifacts directly into the specified target directory.
// By default the artifacts are copied in the same directory structure as the source project.
flatten: true,
// Target base directory for copy, or leave blank to use the workspace.
// Directory (and parent directories, if any) will be created if needed.
//May contain references to build parameters like $PARAM.
target: 'allArtifacts',
fingerprintArtifacts: true,
// How to select the build to copy artifacts from, such as latest successful or stable build, or latest "keep forever" build. Other plugins may provide additional selections.
// The build number of the selected build will be recorded in the environment for later build steps to reference. For details, see the help of "Result variable suffix" in "Advanced" section.
selector: specific(buildObj.getNumber().toString())
)
}
}}}
parallel
commands, but the flow didn't look right in BlueOcean.