Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-36002

Add $ErrorActionPreference = "Stop" to the top of each script before executing

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Major Major
    • powershell-plugin
    • None
    • Jenkins 1.628
      powershell-plugin 1.2

      Just as bash scripts are executed with set -e (as to terminate on any error), so should PowerShell scripts be executed with $ErrorActionPreference = "Stop" at the top (to the same effect).

      Not only is it more consistent (e.g. with bash), it is also usually a more desirable behavior in CI scenarios (when you allow the script to continue you can miss important silent errors).

      A user can always start his script with $ErrorActionPreference = "Continue" to override this, or it could be a checkbox, or the user could simply wrap potentially erronous statements in a try-catch block (probably the best solution).

          [JENKINS-36002] Add $ErrorActionPreference = "Stop" to the top of each script before executing

          Nate Stovall added a comment -

          Instead of a fixed default, perhaps a dropdownbox of choices, with the default choice being the PowerShell default could be in put above the command text entry window.

          Or, you can do what I do and put $ErrorActionPreference = "SilentlyContinue" as the first line of the script.

          Nate Stovall added a comment - Instead of a fixed default, perhaps a dropdownbox of choices, with the default choice being the PowerShell default could be in put above the command text entry window. Or, you can do what I do and put $ErrorActionPreference = "SilentlyContinue" as the first line of the script.

          Ohad Schneider added a comment - - edited

          natestovall of course adding it manually is a possibility, just as adding set -e to the top of each bash script would be. But the fact is, Jenkins effectively adds set -e to the top of each bash script without even asking you, so the most consistent thing to do would be to use the PS equivalent which is $ErrorActionPreference = "SilentlyContinue". A dropdown (with stop as the default) would be even better, of course...

          Ohad Schneider added a comment - - edited natestovall of course adding it manually is a possibility, just as adding set -e to the top of each bash script would be. But the fact is, Jenkins effectively adds set -e to the top of each bash script without even asking you, so the most consistent thing to do would be to use the PS equivalent which is $ErrorActionPreference = "SilentlyContinue" . A dropdown (with stop as the default) would be even better, of course...

          Filipe Roque added a comment -

          Filipe Roque added a comment - created pull request for this:  https://github.com/jenkinsci/powershell-plugin/pull/7

          This is a great idea, in most cases, you don't want scripts to go on when they encounter failures. I'd rather have false positive abortions than false negatives. 

          Raúl Salinas-Monteagudo added a comment - This is a great idea, in most cases, you don't want scripts to go on when they encounter failures. I'd rather have false positive abortions than false negatives. 

          Just had the same issue. Script expected some argument, which I didn't pass in. Jenkins still shows green.

          Michael Brunner added a comment - Just had the same issue. Script expected some argument, which I didn't pass in. Jenkins still shows green.

          Josh Turnbull added a comment -

          Could we have this implemented in the pipeline step rather than just the plugin? From what I understand the powershell plugin is not supported in pipelines and so we still can't get around this issue. Pipeline carries on when calling Import-PowerShellDataFile on an invalidly formatted file when it should absolutely stop. Having to write try-catch blocks every time we try something where errors aren't handled properly and can't set ErrorActionPreference within the pipeline itself

          Josh Turnbull added a comment - Could we have this implemented in the pipeline step rather than just the plugin? From what I understand the powershell plugin is not supported in pipelines and so we still can't get around this issue. Pipeline carries on when calling Import-PowerShellDataFile on an invalidly formatted file when it should absolutely stop. Having to write try-catch blocks every time we try something where errors aren't handled properly and can't set ErrorActionPreference within the pipeline itself

          Éric Louvard added a comment -

          With powershell plugin 1.4 the "stopOnError" feature has been added to the PowerShell class, but this feature is not availlable to the pipeline.
          powershell=true could only be set in the the Build-Step for Free Style projects.
          With the pipeline you already need to set

          powershell script: "\$ErrorActionPreference='Stop';'your script code'"
          

          before each scripts.
          Regards.

          Éric Louvard added a comment - With powershell plugin 1.4 the "stopOnError" feature has been added to the PowerShell class, but this feature is not availlable to the pipeline. powershell=true could only be set in the the Build-Step for Free Style projects. With the pipeline you already need to set powershell script: "\$ErrorActionPreference= 'Stop' ; 'your script code' " before each scripts. Regards.

            Unassigned Unassigned
            ohadschn Ohad Schneider
            Votes:
            5 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: