Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch canceled step and mark build has failure #219

Merged
merged 2 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}

}
}