Skip to content

Instantly share code, notes, and snippets.

@mramanathan
Last active April 10, 2024 16:28
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mramanathan/b1e7e92a3953d28b3b9f856f6bb18412 to your computer and use it in GitHub Desktop.
Save mramanathan/b1e7e92a3953d28b3b9f856f6bb18412 to your computer and use it in GitHub Desktop.
Jenkins Pipeline: How to run stage(s) in all nodes that match label string ?
import jenkins.model.*
collectBuildEnv = [:]
@NonCPS
def getNodes(String label) {
jenkins.model.Jenkins.instance.nodes.collect { thisAgent ->
if (thisAgent.labelString.contains("${label}")) {
// this works too
// if (thisAagent.labelString == "${label}") {
return thisAgent.name
}
}
}
def dumpBuildEnv(String agentName) {
node("${agentName}") {
stage("Env in ${agentName}") {
echo "running on agent, ${agentName}"
sh 'printenv'
}
}
}
def processTask() {
// Replace label-string with the label name that you may have
def nodeList = getNodes("label-string")
for(i=0; i<nodeList.size(); i++) {
def agentName = nodeList[i]
// skip the null entries in the nodeList
if (agentName != null) {
println "Prearing task for " + agentName
collectBuildEnv["node_" + agentName] = {
dumpBuildEnv(agentName)
}
}
}
}
pipeline {
// I prefer to have a dedicated node to execute admin tasks
agent {
label "admin-agent"
}
options {
timestamps()
}
stages {
stage('agents-tasks') {
steps {
script {
processTask()
parallel collectBuildEnv
}
}
}
}
}
@mramanathan
Copy link
Author

mramanathan commented Nov 20, 2018

Tested on Jenkins instance running v2.32.2, had to whitelist couple of methods, though.

@mramanathan
Copy link
Author

#NOTE#

Value for "label-string" (handled in line 27) is case sensitive.

@aviadcye
Copy link

aviadcye commented Dec 7, 2020

Worked for me.
Thanks :)

@mramanathan
Copy link
Author

@aviadcye
Nice to hear that. Thanks for the feedback.

👍

@Th0m45Er
Copy link

Worked as expected, nice!
Little typo in L10. thisAagent -> thisAgent

@nightskyguy
Copy link

nightskyguy commented May 18, 2023

You don't need to use any restricted functions if you use the nodesByLabel step in a scripted pipeline

def nodeList = nodesByLabel label: MyLabel, offline: false

Set offline: true if you want to also select agents that are offline.

@luizhpriotto
Copy link

        stage('Checkout') {
            agent { label "jenkinsnodes" }
            steps {
                script {
                 def nodes = nodesByLabel label: 'jenkinsnodes'
                 nodes = nodes.sort()

                    Map tasks = [:]
                    
                    for (int i = 0; i < nodes.size(); i++) {
                        def label = nodes[i]
                        def stageName = "Checkout ${nodes[i]}"
                        tasks[label] = {
                            node(label) {
                                stage(stageName) {
                                  checkout scm
                                }
                            }
                        }
                    }
                    
                    timeout(time: 3, unit: 'MINUTES') {
                        parallel(tasks)
                    }
                }
            }
        }

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