Skip to content

Commit

Permalink
Merge pull request #219 from hashar/handle-cancelled-steps
Browse files Browse the repository at this point in the history
Catch canceled step and mark build has failure
  • Loading branch information
dheid authored Oct 12, 2024
2 parents f6d65b2 + 84d1bd1 commit 5772c31
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -46,6 +46,7 @@ public class PostBuildScriptIT {
private final Collection<BuildStep> buildSteps = new ArrayList<>();
private TestBuildStep firstBuildStep;
private TestBuildStep secondBuildStep;
private TestAbortingBuildStep abortingBuildStep;

@Test
public void executesShellScriptFile(JenkinsRule jenkinsRule) throws Exception {
Expand Down Expand Up @@ -111,6 +112,7 @@ public void executesPostBuildStep(JenkinsRule jenkinsRule) throws Exception {
@Test
public void executesPostBuildStepRegardlessOfFailures(JenkinsRule jenkinsRule) throws Exception {

givenAbortingBuildStep();
givenFailingFirstBuildStep();
givenSecondBuildStep();
givenPostBuildStep(false);
Expand All @@ -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);
Expand All @@ -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<PostBuildStep> steps = Collections.singleton(step);
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}

}
}

0 comments on commit 5772c31

Please sign in to comment.