Last active
February 3, 2025 14:50
-
-
Save rb2k/8372402 to your computer and use it in GitHub Desktop.
A jenkins script to clean up workspaces on 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
// Check if a slave has < 10 GB of free space, wipe out workspaces if it does | |
import hudson.model.*; | |
import hudson.util.*; | |
import jenkins.model.*; | |
import hudson.FilePath.FileCallable; | |
import hudson.slaves.OfflineCause; | |
import hudson.node_monitors.*; | |
for (node in Jenkins.instance.nodes) { | |
computer = node.toComputer() | |
if (computer.getChannel() == null) continue | |
rootPath = node.getRootPath() | |
size = DiskSpaceMonitor.DESCRIPTOR.get(computer).size | |
roundedSize = size / (1024 * 1024 * 1024) as int | |
println("node: " + node.getDisplayName() + ", free space: " + roundedSize + "GB") | |
if (roundedSize < 10) { | |
computer.setTemporarilyOffline(true, new hudson.slaves.OfflineCause.ByCLI("disk cleanup")) | |
for (item in Jenkins.instance.items) { | |
jobName = item.getFullDisplayName() | |
if (item.isBuilding()) { | |
println(".. job " + jobName + " is currently running, skipped") | |
continue | |
} | |
println(".. wiping out workspaces of job " + jobName) | |
workspacePath = node.getWorkspaceFor(item) | |
if (workspacePath == null) { | |
println(".... could not get workspace path") | |
continue | |
} | |
println(".... workspace = " + workspacePath) | |
customWorkspace = item.getCustomWorkspace() | |
if (customWorkspace != null) { | |
workspacePath = node.getRootPath().child(customWorkspace) | |
println(".... custom workspace = " + workspacePath) | |
} | |
pathAsString = workspacePath.getRemote() | |
if (workspacePath.exists()) { | |
workspacePath.deleteRecursive() | |
println(".... deleted from location " + pathAsString) | |
} else { | |
println(".... nothing to delete at " + pathAsString) | |
} | |
} | |
computer.setTemporarilyOffline(false, null) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made this version if you have 'folders' plugins installed, but does not have the size check, I did this for a server migration that included updates to svn and wanted to delete all workspaces. it handles the recursive nature of folder orginization.
// Check if a slave has < 10 GB of free space, wipe out workspaces if it does
import hudson.model.;
import hudson.util.;
import jenkins.model.;
import hudson.FilePath.FileCallable;
import hudson.slaves.OfflineCause;
import hudson.node_monitors.;
def deleteWorkspace (items, node) {
for (item in items) {
if (item.class.canonicalName != null
&& item.class.canonicalName != "com.cloudbees.hudson.plugins.folder.Folder"
&& item.class.canonicalName != "org.jenkinsci.plugins.workflow.job.WorkflowJob"
&& item.class.canonicalName != "com.github.mjdetullio.jenkins.plugins.multibranch.MavenMultiBranchProject"
&& item.class.canonicalName != "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject"
&& item.class.canonicalName != "hudson.model.ExternalJob") {
println("Item of type "+item.class.canonicalName+" found")
if(!item.isBuilding()) {
println("Wiping out workspace of job "+item.name)
workspacePath = node.getWorkspaceFor(item)
if (workspacePath == null) {
println(".... could not get workspace path")
continue
}
println(".... workspace = " + workspacePath)
customWorkspace = item.getCustomWorkspace()
if (customWorkspace != null) {
workspacePath = node.getRootPath().child(customWorkspace)
println(".... custom workspace = " + workspacePath)
}
pathAsString = workspacePath.getRemote()
if (workspacePath.exists()) {
workspacePath.deleteRecursive()
println(".... deleted from location " + pathAsString)
} else {
println(".... nothing to delete at " + pathAsString)
}
} else {
println("Skipping job "+item.name+", currently building")
}
} else if (item.class.canonicalName == "com.cloudbees.hudson.plugins.folder.Folder") {
println("Item is folder with name "+item.name)
deleteWorkspace(((com.cloudbees.hudson.plugins.folder.Folder) item).getItems(), node)
} else {
println("Item of type "+item.class.canonicalName + " cannot have its workspace cleaned")
}
}
}
for (node in Jenkins.instance.nodes) {
computer = node.toComputer()
if (computer.getChannel() == null) continue
rootPath = node.getRootPath()
size = DiskSpaceMonitor.DESCRIPTOR.get(computer).size
roundedSize = size / (1024 * 1024 * 1024) as int
println("node: " + node.getDisplayName() + ", free space: " + roundedSize + "GB")
}