cleanup custom not used workspaces on all jenkins slaves
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
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