Operations 5 min read

Using Jenkins Pipeline Parallel Syntax for Parallel Tasks and Stages in Microservice Deployments

This article explains how to manage multiple microservice projects with GitLab groups and Jenkins, demonstrating the use of Jenkins pipeline's parallel syntax for both parallel tasks and parallel stages, and provides a master job script that triggers builds concurrently to speed up deployments.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Using Jenkins Pipeline Parallel Syntax for Parallel Tasks and Stages in Microservice Deployments

In a microservice project with many service modules, managing them becomes cumbersome; each module is a separate GitLab project linked to a Jenkins job, and manual selection for releases is inefficient.

A dedicated Jenkins project can be created to centralize management: it binds to a GitLab group, retrieves project information via the GitLab API, allows users to select services to release, and then triggers each selected service's pipeline in parallel.

Example of parallel tasks using the parallel syntax:

def tasks = [:]
// Define parallel task names and actions
tasks["build01"] = { sh "ls" }
tasks["build02"] = { sh "ls" }
// Run in parallel
parallel tasks

Parallel stages can also be defined to run tests on different platforms simultaneously, reducing overall test time. The failFast parameter can be used to control whether other tasks should abort when one fails.

pipeline {
    agent none
    stages {
        stage('Run Tests') {
            parallel {
                stage('Test On Windows') {
                    agent { label "windows" }
                    steps { bat "run-tests.bat" }
                    post { always { junit "**/TEST-*.xml" } }
                }
                stage('Test On Linux') {
                    agent { label "linux" }
                    steps { sh "run-tests.sh" }
                    post { always { junit "**/TEST-*.xml" } }
                }
            }
        }
    }
}

The master job implementation stores the names of projects to be released in a list, builds a map of parallel tasks, and uses the build step to trigger each service's pipeline. Errors are captured in a buildStatus map and reported in the build description.

def jobs = ["test2","demo-test-service"]
def parallelMap = [:]
def buildStatus = [:]

jobs.each { 
    println(it)
    parallelMap[it] = {
        try {
            build job: "${it}", parameters: [string(name: 'branchName', value: 'Dev')]
        } catch(e) {
            println("${it}  " + e)
            buildStatus[it] = e
        }
    }
}
parallel parallelMap

// Evaluate status
for (i in buildStatus.keySet()) {
    currentBuild.description = "构建信息"
    currentBuild.description += ("\n项目名称->" + i + "错误信息:->" + buildStatus[i])
}

By using Jenkins pipeline's parallel capabilities, each module can be built and deployed concurrently, accelerating release cycles and reducing repetitive manual operations.

CI/CDmicroservicesAutomationPipelineJenkinsParallel
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.