Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jespada
Created April 24, 2018 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jespada/059d44639bd9ab42cccf6c6b9075339f to your computer and use it in GitHub Desktop.
Save jespada/059d44639bd9ab42cccf6c6b9075339f to your computer and use it in GitHub Desktop.
jenkins dsl cleanup job slaves docker
// got it from https://gist.github.com/tomnomnom/53998ec618a17d61153911f9b5ade0ed
// The ArrayList of slaves is not serializable, so fetching them should be marked as @NonCPS so that
// no attempt is made to serialize and save the local state of the function. See here for details:
// https://github.com/jenkinsci/pipeline-plugin/blob/master/TUTORIAL.md#serializing-local-variables
@NonCPS
def getSlaves() {
def slaves = []
jenkins.model.Jenkins.instance.slaves.each {
if (it.name.startsWith('jenkinsslave-')) {
slaves << it.name
}
}
print(slaves)
return slaves
}
// Run a command on each slave in parallel
def jobs = [:]
getSlaves().each {
// Use a local variable to avoid closing over a reference to 'it',
// the value of which changes on each iteration
def slave = it
// Create a closure for each slave and put it in the map of jobs
jobs[slave] = {
node(slave) {
// clean up docker containers and docker images
sh 'docker rm -f $(docker ps -a -q)'
sh 'docker rmi -f $(docker images -a -q)'
}
}
}
// Run the closures in parallel
parallel jobs
//triggers {
// cron('@midnight')
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment