Jenkins Multibranch Parallel Pipeline

Mehmet Ali Baykara
The Startup
Published in
2 min readAug 14, 2020

After a long break, I am back again. Let’s move forward with another Jenkins job type so-called multibranch pipeline. In this post, I will focus on parallel execution syntax for the CD chain via Jenkins.

Multibranch
Following tools I’ve used:
* Jenkins
* Docker
* CMake

You most probably work on a multi-branch project where you not directly developing under the master/main branch. To keep the main/production branch always stable we avoid pushing new code there. In my demo will use a C++ project where build, test, and all QA processes triggered by CMake.

Each parallel stage will run its docker container and we will make always it out of source build to keep source code clean. As you might know, you can run all Linux commands in the Jenkins pipeline. This will power us to work on a Linux machine. At some point, there are some differences based on syntax because Jenkinsfile has to be written in the Groovy language. Nonetheless the groovy allows you to use the same syntax as well by adding

#!/bin/bash
echo "hello world"
'''

The project structure looks as below:

├───.vscode
└───cpp-project
├───.vscode
├───CMakeLists.txt
├───cmake
├─── code-coverage.cmake
├─── sanitizer.cmake
├─── package-settings.cmake
├───src
├───Jenkinsfile
└───test

The Jenkinsfile looks:

//if you have multiple worker define by node('worker1')
node(){
checkout scm//Define where you docker image located
def String dockerImage = “docker
//Add docker argument
def String dockerArgs = “-v ${WORKSPACE}:/tmp/project -w /tmp/project”
//define your environment
def String devEnv = “CONAN_USER_HOME=/tmp/conan/ CONAN_NON_INTERACTIVE=1 CCACHE_DIR=/tmp/ccache/”
//here will run three stage parallel
parallel (
‘Release build’: { docker.image(dockerImage).inside(dockerArgs){
sh “mkdir -p /tmp/build-release”
echo "build release process here"
}},
‘Address Sanitizer’: {docker.image(dockerImage).inside(dockerArgs){
sh “mkdir -p /tmp/build-sanitizer “
echo "Do the sanitizer here"
}},
'Coverage-Stage' : docker.image(dockerImage).inside(dockerArgs){
sh “mkdir -p /tmp/coverage “
echo "Do the coverage here"
}})
//And when all stage successfully done then add new non parallel stage as Deployment
stage(‘Deploy Debian Package’) {
docker.image(dockerImage).inside(dockerArgs){
echo "Do the deployment process"
//if this master branch then deploy it
if(env.BRANCH_NAME == “master”) {
echo "This is master branch will be deployed"
}
else{
echo “Skipping upload, only allowed on master.”
}
}
}

Sure you can be extended as you like by adding new parallel jobs or sequential steps. I simply give syntax on Jenkinsfile.

Next post I will share the source code and set up the entire CI/CD chain.

--

--