Skip to content

Commit

Permalink
[FIXED JENKINS-14128] Add option to delete matrix parent workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
vjuranek committed Jan 23, 2013
1 parent 6252bb9 commit 6c2a11f
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/main/java/hudson/plugins/ws_cleanup/WsCleanup.java
Expand Up @@ -3,9 +3,12 @@
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.matrix.MatrixAggregatable;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixBuild;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
Expand All @@ -16,27 +19,29 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import net.sf.json.JSONObject;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import net.sf.json.JSONObject;

/**
*
* @author dvrzalik
*/
public class WsCleanup extends Notifier {
public class WsCleanup extends Notifier implements MatrixAggregatable {

private final List<Pattern> patterns;
private final boolean deleteDirs;
private final boolean skipWhenFailed;
private final boolean cleanupMatrixParent;

@DataBoundConstructor
// FIXME can't get repeteable to work with a List<String>
public WsCleanup(List<Pattern> patterns, boolean deleteDirs, final boolean skipWhenFailed) {
public WsCleanup(List<Pattern> patterns, boolean deleteDirs, final boolean skipWhenFailed, final boolean cleanupMatrixParent) {
this.patterns = patterns;
this.deleteDirs = deleteDirs;
this.skipWhenFailed = skipWhenFailed;
this.cleanupMatrixParent = cleanupMatrixParent;
}

public List<Pattern> getPatterns(){
Expand All @@ -47,12 +52,14 @@ public boolean getDeleteDirs(){
return deleteDirs;
}



public boolean getSkipWhenFailed() {
return skipWhenFailed;
}

public boolean getCleanupMatrixParent() {
return cleanupMatrixParent;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
listener.getLogger().append("\nDeleting project workspace... ");
Expand All @@ -77,6 +84,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
return true;
}

public MatrixAggregator createAggregator(MatrixBuild build, Launcher launcher, BuildListener listener) {
if(cleanupMatrixParent)
return new WsCleanupMatrixAggregator(build, launcher, listener, patterns, deleteDirs, skipWhenFailed);
return null;
}

public BuildStepMonitor getRequiredMonitorService(){
return BuildStepMonitor.STEP;
}
Expand Down
@@ -0,0 +1,84 @@
package hudson.plugins.ws_cleanup;

import hudson.FilePath;
import hudson.Launcher;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixBuild;
import hudson.model.BuildListener;
import hudson.model.Result;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class WsCleanupMatrixAggregator extends MatrixAggregator {

private final List<Pattern> patterns;
private final boolean deleteDirs;
private final boolean skipWhenFailed;

public WsCleanupMatrixAggregator(MatrixBuild build, Launcher launcher, BuildListener listener, List<Pattern> patterns,
boolean deleteDirs, boolean skipWhenFailed) {
super(build, launcher, listener);
this.patterns = patterns;
this.deleteDirs = deleteDirs;
this.skipWhenFailed = skipWhenFailed;
}

public boolean endBuild() throws InterruptedException, IOException {
return doWorkspaceCleanup();
}

private boolean doWorkspaceCleanup() throws IOException, InterruptedException {
listener.getLogger().append("\nDeleting matrix project workspace... ");

//TODO do we want to keep keep child workpsaces if run on the same machine? Make it optional?
/*
VirtualChannel vch = build.getWorkspace().getChannel();
String parentPath = build.getWorkspace().absolutize().toString();
Set<String> filter = new HashSet<String>();
for(MatrixRun run : build.getRuns()) {
if(vch != run.getWorkspace().getChannel())
continue;
String childPath = run.getWorkspace().absolutize().toString();
if(childPath.indexOf(parentPath) == 0) {
if(!parentPath.endsWith(File.separator))
parentPath = parentPath + File.separator;
String relativePath = childPath.substring(parentPath.length());
int firstDirIndex = relativePath.indexOf(File.separator);
String childDir = relativePath.substring(0,firstDirIndex);
//TODO add ./childDir ?
filter.add(childDir);
}
}
if(patterns == null) {
patterns = new LinkedList<Pattern>();
}
for(String excludeDir : filter)
patterns.add(new Pattern(excludeDir, PatternType.EXCLUDE));
*/

FilePath workspace = build.getWorkspace();
try {
if (workspace == null || !workspace.exists())
return true;
if ( this.skipWhenFailed && build.getResult().isWorseOrEqualTo(Result.FAILURE)) {
listener.getLogger().append("skipped for failed build\n\n");
return true;
}
if (patterns == null || patterns.isEmpty()) {
workspace.deleteRecursive();
} else {
workspace.act(new Cleanup(patterns,deleteDirs));
}
listener.getLogger().append("done\n\n");
} catch (InterruptedException ex) {
Logger.getLogger(WsCleanupMatrixAggregator.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
return true;
}

}
Expand Up @@ -29,5 +29,10 @@
<f:entry title="${%Skip when build failed}">
<f:checkbox field="skipWhenFailed" checked="${it.skipWhenFailed}" />
</f:entry>
<!--TODO show only for matrix jobs j:when test="" -->
<f:entry title="${%Cleanup workspace of matrix parent}" help="/plugin/ws-cleanup/help/cleanupMatrixParent.html">
<f:checkbox field="cleanupMatrixParent" checked="${it.cleanupMatrixParent}" />
</f:entry>
<!-- /j:when -->
</f:advanced>
</j:jelly>
4 changes: 4 additions & 0 deletions src/main/webapp/help/cleanupMatrixParent.html
@@ -0,0 +1,4 @@
<p>
When this option is enabled, also matrix parent workspace will be cleaned up. Matrix parent job does checkout into its workspace.
However, if it run on the same node as its sub-jobs, the workspaces of sub-jobs are sub-directories of matrix parent workspace. Therefore in these case, when this option is enabled, workspaces of matrix sub-jobs will be affected, unless appropriate filters are set.
</p>

0 comments on commit 6c2a11f

Please sign in to comment.