Most of our builds are configured to be incremental builds because clean builds take ages. Occasionally the workspaces get corrupted e.g. orphaned intermediate files when the makefiles change or even the local git / repo instances gets corrupted.
I'd like to do something simple e.g.
a) Delete the workspaces for a defined set of jobs on every node.
b) Allow (a) to be triggered manually e.g. an admin can hit the 'reset' button
The best I have so far is a Groovy post build step that wipes out the workspace after the first build that day but it's a pain to configure that in every job.
cheers,
Tim
import hudson.node_monitors.*
import hudson.slaves.*
hudson = Hudson.instance
for (slave in hudson.slaves) {
try {
wsRoot = slave.getWorkspaceRoot()
space = DiskSpaceMonitor.DESCRIPTOR.get(slave.computer)
if (!wsRoot || !space) return
for (dir in wsRoot.list()) {
try {
item = hudson.getItem(dir.name)
if (item instanceof AbstractProject) { processProject(slave, item, dir) }
} catch (Exception e) {
println " workspace: ${dir.name}, has no Hudson object, will delete"
processDeadDir(slave, dir)
}
}
} catch (InterruptedException ie) {
throw ie
} catch (Exception e) {
println " ERR in slave processing: ${e}"
}
def processProject(slave, proj, dir) {
try {
printWS(slave, proj, dir)
if (proj.getLastBuiltOn() != slave) {
age = new Date() - new Date(dir.lastModified())
if (age > 4) {
println " => deleting: ${dir} on ${slave.name} "
tryDelete(slave, proj, dir)
}
}
} catch (InterruptedException ie) {
throw ie
} catch (Exception e) {
println " ERR in processProject: ${e}"
}
}
def processDeadDir(slave, dir) {
try {
println " => deleting: ${dir} on ${slave.name} "
dir.deleteRecursive()
} catch (InterruptedException ie) {
throw ie
} catch (Exception e) {
println " ERR in processDeadDir: ${e}"
}
}
def printWS(slave, proj, dir) {
try {
age = new Date() - new Date(dir.lastModified())
lastBuiltOn = proj.getLastBuiltOn()
same = (lastBuiltOn == slave)
where = lastBuiltOn instanceof DumbSlave ? lastBuiltOn.name : lastBuiltOn
println " workspace: ${dir.name}, age: ${age} days, last built on: ${where}, ${same ? 'keep' : 'could delete'}"
} catch (Exception e) {
println " ERR in print: ${e}"
}
}
def tryDelete(slave, proj, dir) {
if (proj.scm.processWorkspaceBeforeDeletion(proj, dir, slave)) {
dir.deleteRecursive()
> This is fairly easy to do with a system Groovy script; here's one adapted from a script we have that deletes all workspaces on build slaves that are not the active workspace and are more than 4 days old (not guaranteed to work as-is since I've taken out a lot of the conditional logic):
This would be pretty great to have as a plugin, in case anybody wants a weekend
project :)
- R. Tyler Croy
--------------------------------------
Code: http://github.com/rtyler
Chatter: http://identi.ca/agentdero
http://twitter.com/agentdero
-P
>> dblock.org <http://www.dblock.org> -
>> @dblockdotorg<http://twitter.com/#!/dblockdotorg>
>>
--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/f911351a-6e95-4cb9-bcc9-5ccb7efe2418%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
import hudson.model.*import hudson.tasks.*import hudson.plugins.emailext.*import hudson.triggers.*import hudson.maven.*import hudson.model.FreeStyleProject;import hudson.model.JDK;import hudson.maven.MavenModuleSet;import hudson.tasks.Maven;import hudson.maven.reporters.*import hudson.plugins.ws_cleanup.*;import java.util.*;def patterns = new ArrayList()patterns.add(new Pattern("**/target", Pattern.PatternType.INCLUDE))for(AbstractProject item in Jenkins.getInstance().getItems()) {def workspaceCleanup = new WsCleanup(patterns, true, true, true, false);def result = falseif(item instanceof MavenModuleSet) {result = true;}else if(item instanceof FreeStyleProject) {// parcourt de tous les builders pour savoir s'il y a au moins un Mavenfor (builder in item.builders){if(builder instanceof Maven) {result = truebreak}}}if(result) {item.getPublishersList().add(workspaceCleanup);item.save();println "Ajout de workspaceCleanup sur " + item.name}}