Operations 5 min read

Accelerating Jenkins Pipeline Execution with Parallel and collectEntries

This article explains how to use the parallel step and the Groovy collectEntries method in Jenkins Pipelines to run multiple jobs concurrently, reduce build time, and transform collections into maps for flexible stage generation, providing complete code examples and practical guidance.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Accelerating Jenkins Pipeline Execution with Parallel and collectEntries

In Jenkins Pipeline, the parallel statement allows multiple steps to run concurrently on different agent nodes, shortening build time and improving efficiency.

Parallel Usage

Define a jobs map inside a script block to store tasks that should run in parallel.

def jobs = [:]

Add each parallel task with a unique name, stage, and steps.

jobs["job1"] = {
    stage("job1") {
        steps {
            script {
                sh "echo 123"
            }
        }
    }
}

jobs["job2"] = {
    stage("job2") {
        steps {
            script {
                sh "echo 123"
            }
        }
    }
}

Execute the tasks with the parallel statement.

parallel jobs

A complete pipeline example combines the definition and execution of parallel jobs.

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                script {
                    def jobs = [:]
                    jobs["job1"] = {
                        stage("job1") {
                            sh "echo 123"
                        }
                    }
                    jobs["job2"] = {
                        stage("job2") {
                            sh "echo 123"
                        }
                    }
                    parallel jobs
                }
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying'
            }
        }
    }
}

collectEntries Usage

The Groovy collectEntries method converts a collection of key‑value pairs into a new Map , enabling flexible transformation of data within a pipeline.

pipeline {
    agent any
    stages {
        stage('Collect entries') {
            steps {
                script {
                    def projects = [
                        'Project 1': 5,
                        'Project 2': 3,
                        'Project 3': 1,
                        'Project 4': 2
                    ]
                    def priorityMap = projects.collectEntries { [it.key, it.value * 10] }
                    echo "${priorityMap}"
                }
            }
        }
    }
}

This example creates a map of projects, multiplies each value by 10, and outputs the new map.

Generating Parallel Jobs with collectEntries

// Example function returning an array of job names
def getjobsToDeploy() {
    def jobList = []
    for (int i = 0; i < 5; i++) {
        jobList << "buildjob" + i.toString()
    }
    return jobList
}

// Convert array of jobs to a map of stages
def getDeployStages() {
    return getjobsToDeploy().collectEntries { job ->
        [ (job): {
            stage("deploy ${job}") {
                sh "ls "
            }
        }]
    }
}

def getTestStages() {
    return getjobsToDeploy().collectEntries { job ->
        [ (job): {
            stage("test ${job}") {
                sh "ls "
            }
        }]
    }
}

pipeline {
    agent any
    stages {
        // other stages...
        stage('Deploy') {
            steps {
                script {
                    parallel getDeployStages()
                }
            }
        }
        stage('Test') {
            steps {
                script {
                    parallel getTestStages()
                }
            }
        }
    }
}

The parallel statement accepts maps generated by collectEntries , allowing dynamic creation of parallel stages for deployment and testing.

CI/CDDevOpsPipelineGroovyJenkinsParallel
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.