Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save prasathsivasubramaniyan/bab47e403ff992bb7e0dc4aa265c2139 to your computer and use it in GitHub Desktop.
cleanup custom not used workspaces on all jenkins slaves
import jenkins.*
import jenkins.model.*
import hudson.*
import hudson.model.*
def DRY_RUN = false
def DIRS_FILTER = ['.hg', 'production','logs','apache', 'projects', 'qlickview']
def JOBS_WS_ROOT = 'path-to-your-workspaces-root-dir'
def JOBS_IRRELEVANT_SYMBOLS = "[_,-]|\\s"
def allJobs = Jenkins.instance.items.findAll { it instanceof hudson.model.Job }.collect { it.name };
println "All jobs:" + allJobs;
for (slave in Jenkins.instance.slaves)
{
if(slave.computer.online) {
println "Cleaning old workspaces in " + slave.name+"("+slave.computer.hostName+")"
def wsRoot = new FilePath(slave.channel, JOBS_WS_ROOT)
println "starting from:" + wsRoot
def subdirs = wsRoot.listDirectories();
//println "subdirs :" + subdirs
if( subdirs.size() == 0 ) {
println(" (workspace is empty)");
continue;
}
for(d in subdirs) {
// Remove any suffixes from the dir name
def dirName = d.name.split("@")[0];
if (DIRS_FILTER.contains(dirName)) {
continue
}
//println "checking matching job for : " + dirName
// Find matching job
def jobMatch = allJobs.find { it.replaceAll(JOBS_IRRELEVANT_SYMBOLS,"") == dirName.replaceAll(JOBS_IRRELEVANT_SYMBOLS,"") };
if ( jobMatch != null ) {
//println(" KEEP: $d --> job:$jobMatch");
}
else {
if( DRY_RUN == true ) {
println(" DELETE: $d (dryRun)");
}
else {
println(" DELETE: $d");
d.deleteRecursive();
}
}
}//end of for over subdirs
}//end of if online
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment