Skip to content

Instantly share code, notes, and snippets.

@jmarcos-cano
Last active December 7, 2019 19:23
Show Gist options
  • Save jmarcos-cano/2db9b2cb5169cfb20dda586d53fe60ff to your computer and use it in GitHub Desktop.
Save jmarcos-cano/2db9b2cb5169cfb20dda586d53fe60ff to your computer and use it in GitHub Desktop.
Jenkinsfile to run a command on each node
/*
Created by: Marcos Cano
Team: DevOps
Date: 7/7/17
*/
stage("Run cmd on each node") {
def node_names=nodeNames()
for (String item:node_names) {
println "************* RUNNING IN: ${item} *************"
try{
timeout(time: 100, unit: 'SECONDS'){
node(item) {
stdout = sh (
script: "uname -a",
returnStdout: true,
returnStderr: true
).trim()
println stdout
writeFile file: "${item}.txt", text: stdout
archiveArtifacts '*.txt'
}
}
}catch(e){
println "ERROR " +item " Timed Out"
println e
}
}
}
@NonCPS
def nodeNames() {
return jenkins.model.Jenkins.instance.nodes.collect { node ->
node.name
}
}
@gavvvr
Copy link

gavvvr commented Dec 7, 2019

Thank you!
Unfortunately I did not have enough access rights for Jenkins.instance.nodes, so I just created a static list of nodes.

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