Created
August 4, 2016 12:19
-
-
Save tszpinda/8cc35ef2369859522d62a4e2cc026554 to your computer and use it in GitHub Desktop.
Delete unused workspace folders from slaves
This file contains hidden or 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 hudson.FilePath | |
import jenkins.model.Jenkins | |
import hudson.model.Job | |
// | |
// cleans up workspace folders on slaves - removes deleted projects | |
// | |
def deleteUnusedWorkspace(FilePath root, String path) { | |
if(!root.list())return; | |
root.list().sort{child->child.name}.each { child -> | |
String fullName = path + child.name | |
def item = Jenkins.instance.getItemByFullName(fullName); | |
if (item == null) { | |
println "Deleting (no such job): '$fullName'" | |
child.deleteRecursive() | |
} else if (item.class.canonicalName == 'com.cloudbees.hudson.plugins.folder.Folder') { | |
deleteUnusedWorkspace(root.child(child.name), "$fullName/") | |
} else if (item instanceof Job && !item.isBuildable()) { | |
println "Deleting (job disabled): '$fullName'" | |
child.deleteRecursive() | |
} else { | |
println "Leaving: '$fullName'" | |
} | |
} | |
} | |
for (node in Jenkins.instance.nodes) { | |
println "Processing $node.displayName" | |
if(node.rootPath) { | |
def workspaceRoot = node.rootPath.child("workspace"); | |
deleteUnusedWorkspace(workspaceRoot, "") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment