Dynamic parallel stages in Jenkins

Jenkins Declarative Pipeline v1.2 added support for Parallel Stages, which is a great and easy way to, well, run multiple aspects of a job – in parallel.

In most cases this would suffice and you could author a simple parallel block as described in the Jenkins documentation (See blog post.

But what if you need to generate multiple parallel runs in a job – dynamically?
One way would be as follows.

Lets assume a have a package.json that defines different script executions, such as:

"scripts": {
"test1": "jenkins-mocha --recursive --reporter mocha-multi-reporters --reporter-options configFile=config/mocha-config.json test/test1.js",
"test2": "jenkins-mocha --recursive --reporter mocha-multi-reporters --reporter-options configFile=config/mocha-config.json test/test2.js"

In order to run these in parallel you could do this in a stage:

packageJson = readJSON file: ('package.json')
tests = packageJson.scripts
listOfTests = tests.keySet()

for (int i = 0 ; i < listOfTests.size() ; i++) {
test = listOfTests[i]
parallelTests[test] = {
sh "npm run $test"

parallel parallelTests

This would then generate a similar flow as depicted below (replace “Test on Linux/Windows” with “test1” and “test2”).

If one of the parallel executions will fail, this will then fail the job once all parallel runs have finished.