'How to run multiple agents (given on runtime) simultaneously with Jenkins Declarative Pipeline?
I'm running a Jenkins Job on multiple platforms selected manually at runtime. Having multi-select Active Choices Parameter named "Platforms" for the relevant nodes.
I'm using the following code in Jenkins scripted pipeline:
def labels=Platforms.toString().split(",").collect{"\'" + it + "\'"}
def builders=[:]
for (label in labels) {
builders[label] = {
node(label) {
stage ('Stage 1') {
sh 'hostname'
}
}
}
}
parallel builders
How can I accomplish the same thing in Declarative Pipeline? The agents are selected randomly by the user at runtime.
Thanks.
Solution 1:[1]
Ok, I found my answer here: Running same Jenkins job on multiple agents in parallel in declarative pipeline Thanks to user 'np2807'. My code is now looking like this:
def labels=Platforms.toString().split(",").collect{"\'" + it + "\'"}
def generateStage(nodeLabel) {
return {
stage("Runs on ${nodeLabel}") {
node(nodeLabel) {
stage("Stage 1") {
script {
echo "Running on ${nodeLabel}"
sh 'hostname'
}
}
}
}
}
}
def parallelStagesMap = labels.collectEntries {
["${it}" : generateStage(it)]
}
pipeline {
agent none
stages {
stage('non-parallel stage') {
steps {
echo 'This stage will be executed first.'
}
}
stage('parallel stage') {
steps {
script {
parallel parallelStagesMap
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sofia Zelentsov |
