Configure advanced Scripted Pipeline

Configure Jenkins Scripted and Declarative Pipelines to run stages in parallel across multiple agents using the parallel step to speed up test execution. Target labeled nodes (for example, Linux and Windows) to execute builds and tests concurrently.

1 minute read

Scripted Pipeline is a domain-specific language based on Groovy, most Groovy syntax can be used in Scripted Pipeline without modification.

Execute in parallel

The example below runs tests across two different platforms in a linear series. In practice, if the make check execution takes 30 minutes to complete, the "Test" stage would now take 60 minutes to complete!

Fortunately, Pipeline has built-in functionality for executing portions of Scripted Pipeline in parallel, implemented in the aptly named parallel step.

Declarative syntax
Scripted syntax
pipeline { agent none stages { stage('Build') { /* .. snip ..*/ } stage('Test') { parallel { stage('linux') { agent { label 'windows' } steps { unstash 'app' sh 'make check' junit '**/target/*.xml' } } stage('windows') { agent { label 'windows' } steps { /* ... snip .. */ } } } } } }
▼
stage('Build') { /* .. snip .. */ } stage('Test') { parallel linux: { node('linux') { checkout scm try { unstash 'app' sh 'make check' } finally { junit '**/target/*.xml' } } }, windows: { node('windows') { /* .. snip .. */ } } }
▼

Instead of executing the tests on the "linux" and "windows" labelled nodes in series, they will now execute in parallel assuming the requisite capacity exists in the Jenkins environment.