From 0efc8d5349b2e73b479e4d86c84244c8c3a0568b Mon Sep 17 00:00:00 2001 From: Antoine Musso Date: Thu, 26 Sep 2024 17:24:43 +0200 Subject: [PATCH] Catch canceled step and mark build has failure When using the Parameterized plugin, the triggered job can be canceled. That causes an `AbortException` to be thrown. Since it is not handled, PostBuildScript let it through and the build is marked in failure even when one explicitly asks to ignore any error. Catch `AbortException` and mark the build has being in failure in such a case instead of bubbling up the exception. Fixes: https://phabricator.wikimedia.org/T352319 --- .../postbuildscript/processor/Processor.java | 16 ++++++--- .../postbuildscript/PostBuildScriptIT.java | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) 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 f341bb4..bc9c547 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; @@ -177,11 +178,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 e961999..6ec987a 100644 --- a/src/test/java/org/jenkinsci/plugins/postbuildscript/PostBuildScriptIT.java +++ b/src/test/java/org/jenkinsci/plugins/postbuildscript/PostBuildScriptIT.java @@ -1,5 +1,6 @@ package org.jenkinsci.plugins.postbuildscript; +import hudson.AbortException; import hudson.Functions; import hudson.Launcher; import hudson.model.*; @@ -43,6 +44,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 { @@ -122,6 +124,7 @@ public void executesPostBuildStep(JenkinsRule jenkinsRule) throws Exception { @Test public void executesPostBuildStepRegardlessOfFailures(JenkinsRule jenkinsRule) throws Exception { + givenAbortingBuildStep(); givenFailingFirstBuildStep(); givenSecondBuildStep(); givenPostBuildStep(false); @@ -149,6 +152,19 @@ public void stopOnBuildStepFailure(JenkinsRule jenkinsRule) throws Exception { } + @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); @@ -164,6 +180,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); @@ -209,6 +230,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; @@ -227,4 +252,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(); + } + + } }