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

Support stopOnError for powershell scripts #198

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mbaitelman
Copy link

Add support for stopOnError to handle PS errors

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests - that demonstrates feature works or fixes the issue

@mbaitelman
Copy link
Author

I was having trouble with this param until I figured out that this plugin isn't https://github.com/jenkinsci/powershell-plugin which is mentioned everywhere else.

@mbaitelman
Copy link
Author

Id rather this not be a blocker but if this is accepted the docs will need to be updated as well.
https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#code-powershell-code-powershell-script

// Test that a powershell step that fails indeed causes the underlying build to fail with stopOnFailure: true
@Test public void testStopOnFailure() throws Exception {
WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "stopfailure");
p.setDefinition(new CpsFlowDefinition("node {powershell( script: 'throw \"bogus error\"', stopOnFailure: true)}", true));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make the job do more after the error step (like print a line) and verify that the subsequent actions do not appear

@car-roll
Copy link
Collaborator

Looks good! I really do think though we need to add some documentation. @dwnusbaum If we are updating the documentation to include this label for just the Powershell step, would we need to add a jelly file here: https://github.com/jenkinsci/workflow-durable-task-step-plugin/tree/master/src/main/resources/org/jenkinsci/plugins/workflow/steps/durable_task/PowershellScriptStep ?

Comment on lines +50 to +52
if(this.stopOnError){
script = "$ErrorActionPreference=\"Stop\"" + script;
}
Copy link
Member

@dwnusbaum dwnusbaum Jan 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this do anything? The constructor will run before setStopOnError could be called, so I think this branch is never entered. That said, if your new tests pass, then maybe the existing behavior is already fine and this PR is not needed, or maybe your tests don't quite cover the right scenario.

Also more generally, I don't think you should modify the script like this here. I think the right way to do this would be to modify powershellHelper.ps1 (or do something like this) over in durable-task plugin and expose a new @DataBoundSetter on PowershellScript, and then consume that from this plugin.

(I don't know enough about Powershell to comment on the behavior of ErrorActionPreference itself).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points. If I had to choose, I'd go with the second option
powershellHelper.ps1 does not exist in the binary version of durabletask, so you would lose support there.

@dwnusbaum
Copy link
Member

@car-roll yeah, if you add new parameters, they need to be added to config-details.jelly in that directory, and a help-parameterName.html file should be added there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants