Skip to content

Parallelism

Andrew Bayer edited this page Jan 10, 2017 · 7 revisions

How to use parallel and why (e.g. for running the same tests over multiple browser platforms)

You can use parallelism inside pipelines for a few reasons:

  • Split up a test suite on a powerful machine
  • Run tests across different operating systems or environments
  • Distribute work across a cluster

The aim of all this is to shorten the length of the pipeline run.

Split up a test suite

In this case, we will split our tests into two:

pipeline {
    agent { label '' }
    stages {
        stage("test") {
            steps {
                parallel (
                    "Firefox" : {
                        sh "echo testing FFX"
                        sh "echo more steps"
                    },
                    "Chrome" : {
                        sh "echo testing Chrome"
                        sh "echo more steps"
                    }
                }
            )
        }
    }    
}

This makes use of one single agent, and spins off 2 runs of the steps inside each parallel branch, as separate processes.

The steps:

                    sh "echo testing FFX"
                    sh "echo more steps"

Are run at the same time as:

                    sh "echo testing Chrome"
                    sh "echo more steps"

Testing across operating systems

Let's run steps on different platforms at the same time:

pipeline {
    agent none
    stages {
        stage("distribute") {
            steps {
                parallel (
                    "windows" : {
                        node('windows') {
                            bat "print from windows"    
                        }
                    },
                    "mac" : {
                        node('osx') {
                            sh "echo from mac"    
                        }
                    },
                    "linux" : {
                        node('linux') {
                            sh "echo from linux"    
                        }
                    }
                }                
            )
        }
    }    
}

The key difference here is that there is image none and then in each parallel section, we use node to go and fetch an agent that suits, and run steps on it remotely.

Distributing work

You don't have to use different node labels. You can use the same one as many times as you like and Jenkins will try to schedule the work on free agents that are suitable:

                "Browser Testing" : {
                    node('linux') {
                        sh "echo browser testing"    
                    }
                },
                "Unit Testing" : {
                    node('linux') {
                        sh "echo unit testing"    
                    }
                }

There is a lot more you can do with parallelism, but this is a start.

Note that parallelism happens inside a running pipeline - you can also have multiple instances of a pipeline running concurrently, even if you don't use parallel.