Last active
December 7, 2019 19:23
-
-
Save jmarcos-cano/2db9b2cb5169cfb20dda586d53fe60ff to your computer and use it in GitHub Desktop.
Jenkinsfile to run a command on each node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you!
Unfortunately I did not have enough access rights for
Jenkins.instance.nodes
, so I just created a static list of nodes.