Skip to content

Instantly share code, notes, and snippets.

@richid
Last active February 24, 2023 15:07
Show Gist options
  • Save richid/d498346030275ae1790618039d8965c0 to your computer and use it in GitHub Desktop.
Save richid/d498346030275ae1790618039d8965c0 to your computer and use it in GitHub Desktop.
Jenksfile declarative pipeline with parallel dynamic stages
def applications = env.APPLICATIONS.split(",").findAll { it }.collect { it.trim() }
def environment = env.ENVIRONMENT
def version = env.VERSION
def jobs = [:]
if (applications.size() < 1) {
error("ERROR: APPLICATIONS must be a comma-delimited list of applications to build")
}
for (int i = 0; i < applications.size(); i++) {
def app = applications[i]
jobs["jobs-${app}"] = {
node {
stage("Build ${app}") {
build job: 'Application-Builder', parameters: [
string(name: 'APPLICATION', value: app),
string(name: 'ENVIRONMENT', value: environment),
string(name: 'VERSION', value: version)
]
}
}
}
}
pipeline {
agent none
stages {
stage('Build apps(s)') {
steps {
script {
parallel jobs
}
}
}
}
}
@ravibhure
Copy link

well written parallel dynamically load stages 👍

@karthikeayan
Copy link

copy pasted, thank you 👍

@bkoochin-clgx
Copy link

bkoochin-clgx commented Nov 10, 2021

Great example! Thanks kindly. 👍

@ajh789
Copy link

ajh789 commented Apr 30, 2022

Hi, in my verification the for loop doesn't work well with a closure.
See https://www.convalesco.org/notes/2020/05/26/parallel-stages-in-jenkins-scripted-pipelines.html
Better use "each" instead:

applications.each { app ->
    jobs["jobs-${app}"] = {
        node {
            stage("Build ${app}") {
                build job: 'Application-Builder', parameters: [
                    string(name: 'APPLICATION', value: app),
                    string(name: 'ENVIRONMENT', value: environment),
                    string(name: 'VERSION',     value: version)
                ]
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment