Forked from ceilfors/cleanupUnusedWorkspaceInSlaves.groovy
Last active
November 30, 2020 05:46
-
-
Save yafevin/1852739069dfeca72fad00dfa6244a69 to your computer and use it in GitHub Desktop.
cleanup unused workspace in 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 com.cloudbees.hudson.plugins.folder.Folder | |
import hudson.FilePath | |
import jenkins.model.Jenkins | |
def boolean isFolder(String name) { | |
def item = Jenkins.instance.getItemByFullName(name) | |
return item instanceof Folder | |
} | |
def deleteUnusedWorkspace(FilePath root, String path) { | |
root.list().each { child -> | |
String fullName = path + child.name | |
if (isFolder(fullName)) { | |
deleteUnusedWorkspace(root.child(child.name), "$fullName/") | |
} else { | |
if (Jenkins.instance.getItemByFullName(fullName) == null) { | |
println "Deleting: $fullName " | |
child.deleteRecursive() | |
} | |
} | |
} | |
} | |
for (node in Jenkins.instance.nodes) { | |
println "Processing $node.displayName" | |
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