Using Jenkins Pipeline Parallel Syntax for Parallel Tasks and Stages in Microservice Deployment
This guide explains how to streamline the deployment of multiple microservice modules by creating a Jenkins project that integrates with GitLab, lets users select services, and uses the pipeline parallel syntax to run tasks and stages concurrently, reducing manual effort and speeding up releases.
In a microservice project with many service modules, managing releases manually becomes cumbersome, so a Jenkins project is created to centrally manage GitLab groups, bind each project to a Jenkins job, and allow users to select services for deployment.
The solution fetches GitLab project information via the API, lets users choose target services, and triggers each service's pipeline in parallel using Jenkins' parallel syntax.
Parallel Tasks Example : Define a map of task names to shell commands and execute them concurrently.
def tasks = [:]
// Define parallel task names and actions
tasks["build01"] = { sh "ls" }
tasks["build02"] = { sh "ls" }
// Run in parallel
parallel tasksParallel Stages Example : Use parallel stages to run automated tests on different platforms simultaneously, optionally configuring failFast to stop all 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" } }
}
}
}
}
}Master Job Implementation : Store target project names in a static list, create a map for parallel jobs, trigger each job with parameters, capture any errors, and summarize the build status.
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
// Summarize status
for (i in buildStatus.keySet()) {
currentBuild.description = "构建信息"
currentBuild.description += ("\n项目名称->" + i + "错误信息:->" + buildStatus[i])
}Each module typically has its own pipeline project; by using a centralized control job with the parallel syntax, deployments can be triggered concurrently, accelerating release speed and reducing repetitive manual operations.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.