diff --git a/src/main/java/org/jenkinsci/plugins/workflow/steps/durable_task/PowershellScriptStep.java b/src/main/java/org/jenkinsci/plugins/workflow/steps/durable_task/PowershellScriptStep.java index d2ddb2d5..8f5834ca 100644 --- a/src/main/java/org/jenkinsci/plugins/workflow/steps/durable_task/PowershellScriptStep.java +++ b/src/main/java/org/jenkinsci/plugins/workflow/steps/durable_task/PowershellScriptStep.java @@ -29,6 +29,7 @@ import org.jenkinsci.plugins.durabletask.DurableTask; import org.jenkinsci.plugins.durabletask.PowershellScript; import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.DataBoundSetter; /** * Asynchronous batch script execution. @@ -36,11 +37,19 @@ public class PowershellScriptStep extends DurableTaskStep { private final String script; + private boolean stopOnError = false; + + @DataBoundSetter public void setStopOnError(boolean stopOnError) { + this.stopOnError = stopOnError; + } @DataBoundConstructor public PowershellScriptStep(String script) { if (script == null) { throw new IllegalArgumentException(); } + if(this.stopOnError){ + script = "$ErrorActionPreference=\"Stop\"" + script; + } this.script = script; } diff --git a/src/test/java/org/jenkinsci/plugins/workflow/steps/durable_task/PowerShellStepTest.java b/src/test/java/org/jenkinsci/plugins/workflow/steps/durable_task/PowerShellStepTest.java index 28165218..8893dbac 100644 --- a/src/test/java/org/jenkinsci/plugins/workflow/steps/durable_task/PowerShellStepTest.java +++ b/src/test/java/org/jenkinsci/plugins/workflow/steps/durable_task/PowerShellStepTest.java @@ -66,6 +66,22 @@ public class PowerShellStepTest { j.assertLogContains("bogus error", j.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0))); } + // Test that a powershell step that fails indeed causes the underlying build to fail with stopOnError: true + @Test public void testStopOnFailure() throws Exception { + WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "stopOnError"); + p.setDefinition(new CpsFlowDefinition("node {powershell( script: 'throw \"bogus error\"', stopOnError: true) powershell( script: 'Write-output \"We made it past the error\"') }", true)); + j.assertLogNotContains("We made it past the error", j.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0))); + j.assertLogContains("bogus error", j.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0))); + } + + // Test that a powershell step that fails does not cause the underlying build to fail with stopOnError: false + @Test public void testStopOnFailureFalse() throws Exception { + WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "stopOnErrorFalse"); + p.setDefinition(new CpsFlowDefinition("node {powershell( script: 'throw \"bogus error\"', stopOnError: false) powershell( script: 'Write-output \"We made it past the error\"') }", true)); + j.assertLogContains("We made it past the error", j.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0))); + j.assertLogContains("bogus error", j.assertBuildStatus(Result.FAILURE, p.scheduleBuild2(0))); + } + @Test public void testUnicode() throws Exception { WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "foobar"); p.setDefinition(new CpsFlowDefinition("node {def x = powershell(returnStdout: true, script: 'write-output \"Hëllö Wórld\"'); println x.replace(\"\ufeff\",\"\")}", true));