Loading...

Parallel steps with Jenkinsfile

:heavy_exclamation_mark: This post is older than a year. Consider some information might not be accurate anymore. :heavy_exclamation_mark:

Pipeline offers a straight-forward syntax for branching your pipeline into parallel steps. Following example demonstrate how to use the parallel steps with Jenkins. The example is based on my use case, Jenkins generates a rpm artifact with custom software. That software is deployed on multiple servers (aws data centers).

node {
    stage("Build") { // build rpm }
    stage("Deploy") {
        def servers = ["aws-frankfurt", "aws-london", "aws-dublin"]
        def stepsForParallel = [:]
        for (int i = 0; i < servers.size(); i++) {
            def s = servers.get(i)
            def deployStep = "${s}"
            stepsForParallel[deployStep] = transformIntoStep(s)
        }
        parallel stepsForParallel
    }
}
def transformIntoStep(String serverName) {
    return {
        node {
            // install rpm
            sh "ssh -q -t vinh@${serverName} \"sudo yum install ${rpmfile} -y\""
        }
    }
}

The LinkedList servers contains all the host names. We put into the map stepsForParallel for each servername a new jenkins node with the rpm installation directive. Each node is executed for itself. Don’t use stages for a step! All steps are in the stage deploy but are separated by the step identifier.

Parallel Steps with Jenkins

Please remember the terms for blog comments.