diff --git a/src/main/java/org/jenkinsci/plugins/postbuildscript/processor/Processor.java b/src/main/java/org/jenkinsci/plugins/postbuildscript/processor/Processor.java index 1754567..97f485b 100644 --- a/src/main/java/org/jenkinsci/plugins/postbuildscript/processor/Processor.java +++ b/src/main/java/org/jenkinsci/plugins/postbuildscript/processor/Processor.java @@ -1,6 +1,7 @@ package org.jenkinsci.plugins.postbuildscript.processor; import com.google.common.base.Strings; +import hudson.AbortException; import hudson.EnvVars; import hudson.FilePath; import hudson.Launcher; @@ -172,11 +173,16 @@ private boolean processBuildSteps(boolean endOfMatrixBuild) throws PostBuildScri } for (BuildStep buildStep : postBuildStep.getBuildSteps()) { - if (!buildStep.perform(build, launcher, listener)) { - everyStepSuccessful = false; - if (postBuildStep.isStopOnFailure()) { - return false; - } + boolean buildSucceed; + try { + buildSucceed = buildStep.perform(build, launcher, listener); + } catch (AbortException e) { + buildSucceed = false; + } + everyStepSuccessful &= buildSucceed; + + if (!buildSucceed && postBuildStep.isStopOnFailure()) { + return false; } } } diff --git a/src/test/java/org/jenkinsci/plugins/postbuildscript/PostBuildScriptIT.java b/src/test/java/org/jenkinsci/plugins/postbuildscript/PostBuildScriptIT.java index 334f239..f9b0289 100644 --- a/src/test/java/org/jenkinsci/plugins/postbuildscript/PostBuildScriptIT.java +++ b/src/test/java/org/jenkinsci/plugins/postbuildscript/PostBuildScriptIT.java @@ -1,9 +1,9 @@ package org.jenkinsci.plugins.postbuildscript; +import hudson.AbortException; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.MatcherAssert.assertThat; - import hudson.Functions; import hudson.Launcher; import hudson.model.AbstractBuild; @@ -46,6 +46,7 @@ public class PostBuildScriptIT { private final Collection buildSteps = new ArrayList<>(); private TestBuildStep firstBuildStep; private TestBuildStep secondBuildStep; + private TestAbortingBuildStep abortingBuildStep; @Test public void executesShellScriptFile(JenkinsRule jenkinsRule) throws Exception { @@ -111,6 +112,7 @@ public void executesPostBuildStep(JenkinsRule jenkinsRule) throws Exception { @Test public void executesPostBuildStepRegardlessOfFailures(JenkinsRule jenkinsRule) throws Exception { + givenAbortingBuildStep(); givenFailingFirstBuildStep(); givenSecondBuildStep(); givenPostBuildStep(false); @@ -136,6 +138,19 @@ public void stopOnBuildStepFailure(JenkinsRule jenkinsRule) throws Exception { Assertions.assertEquals(0, secondBuildStep.getInvocations()); } + @Test + public void handlesAbortException(JenkinsRule jenkinsRule) throws Exception { + givenAbortingBuildStep(); + givenSecondBuildStep(); + givenPostBuildStep(true); + + whenBuilt(jenkinsRule); + + thenNoProblemOccured(jenkinsRule); + thenFailedBuild(); + Assertions.assertEquals(0, secondBuildStep.getInvocations()); + } + private void givenSuccessfulFirstBuildStep() { firstBuildStep = new TestBuildStep(true); buildSteps.add(firstBuildStep); @@ -151,6 +166,11 @@ private void givenSecondBuildStep() { buildSteps.add(secondBuildStep); } + private void givenAbortingBuildStep() { + abortingBuildStep = new TestAbortingBuildStep(); + buildSteps.add( abortingBuildStep ); + } + private void givenPostBuildStep(boolean stopOnFailure) { PostBuildStep step = new PostBuildStep(SUCCESS_RESULTS, buildSteps, stopOnFailure); Collection steps = Collections.singleton(step); @@ -194,6 +214,10 @@ private void thenFailedBuild() { assertThat(build.getResult(), is(Result.FAILURE)); } + private void thenNoProblemOccured(JenkinsRule jenkinsRule) throws IOException { + jenkinsRule.assertLogNotContains(Messages.PostBuildScript_ProblemOccured(), build); + } + private static class TestBuildStep extends TestBuilder { private final boolean result; private volatile int invocations; @@ -212,4 +236,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen return result; } } + + private static class TestAbortingBuildStep extends TestBuilder { + + @Override + public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws AbortException { + throw new AbortException(); + } + + } }