Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-15236] Add option not to fail the build if some error …
…happens during the cleanup
  • Loading branch information
vjuranek committed Jan 23, 2013
1 parent 6c2a11f commit 8b56d17
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
19 changes: 15 additions & 4 deletions src/main/java/hudson/plugins/ws_cleanup/WsCleanup.java
Expand Up @@ -33,14 +33,16 @@ public class WsCleanup extends Notifier implements MatrixAggregatable {
private final List<Pattern> patterns;
private final boolean deleteDirs;
private final boolean skipWhenFailed;
private final boolean notFailBuild;
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, final boolean cleanupMatrixParent) {
public WsCleanup(List<Pattern> patterns, boolean deleteDirs, final boolean skipWhenFailed, final boolean notFailBuild, final boolean cleanupMatrixParent) {
this.patterns = patterns;
this.deleteDirs = deleteDirs;
this.skipWhenFailed = skipWhenFailed;
this.notFailBuild = notFailBuild;
this.cleanupMatrixParent = cleanupMatrixParent;
}

Expand All @@ -56,13 +58,17 @@ public boolean getSkipWhenFailed() {
return skipWhenFailed;
}

public boolean getNotFailBuild() {
return notFailBuild;
}

public boolean getCleanupMatrixParent() {
return cleanupMatrixParent;
}

@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
listener.getLogger().append("\nDeleting project workspace... ");
listener.getLogger().append("\nDeleting project workspace... \n");
FilePath workspace = build.getWorkspace();
try {
if (workspace == null || !workspace.exists())
Expand All @@ -77,16 +83,21 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
workspace.act(new Cleanup(patterns,deleteDirs));
}
listener.getLogger().append("done\n\n");
} catch (InterruptedException ex) {
} catch (Exception ex) {
Logger.getLogger(WsCleanup.class.getName()).log(Level.SEVERE, null, ex);
if(notFailBuild) {
listener.getLogger().append("Cannot delete workspace: " + ex.getCause() + "\n");
listener.getLogger().append("Option not to fail the build is turned on, so let's continue\n");
return true;
}
return false;
}
return true;
}

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

Expand Down
Expand Up @@ -17,21 +17,23 @@ public class WsCleanupMatrixAggregator extends MatrixAggregator {
private final List<Pattern> patterns;
private final boolean deleteDirs;
private final boolean skipWhenFailed;
private final boolean notFailBuild;

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

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

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

//TODO do we want to keep keep child workpsaces if run on the same machine? Make it optional?
/*
Expand Down Expand Up @@ -74,8 +76,13 @@ private boolean doWorkspaceCleanup() throws IOException, InterruptedException {
workspace.act(new Cleanup(patterns,deleteDirs));
}
listener.getLogger().append("done\n\n");
} catch (InterruptedException ex) {
} catch (Exception ex) {
Logger.getLogger(WsCleanupMatrixAggregator.class.getName()).log(Level.SEVERE, null, ex);
if(notFailBuild) {
listener.getLogger().append("Cannot delete workspace: " + ex.getCause() + "\n");
listener.getLogger().append("Option not to fail the build is turned on, so let's continue\n");
return true;
}
return false;
}
return true;
Expand Down
Expand Up @@ -29,6 +29,9 @@
<f:entry title="${%Skip when build failed}">
<f:checkbox field="skipWhenFailed" checked="${it.skipWhenFailed}" />
</f:entry>
<f:entry title="${%Don't fail the build if cleanup fails}" help="/plugin/ws-cleanup/help/notFailBuild.html">
<f:checkbox field="notFailBuild" checked="${it.notFailBuild}" />
</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}" />
Expand Down
4 changes: 4 additions & 0 deletions src/main/webapp/help/notFailBuild.html
@@ -0,0 +1,4 @@
<p>
During the workspace cleanup various issues can happen, e.g. file cannot be deleted because it's still open and locked by some other process.
By default, when such problem happens, build is marked as failed. If this option is checked, workspace cleanup never marks build as failed.
</p>

0 comments on commit 8b56d17

Please sign in to comment.