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

PowerShell: incorrect handling of $LASTEXITCODE can cause broken script to not abort pipeline

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Minor
    • Resolution: Fixed
    • Component/s: durable-task-plugin
    • Labels:
      None
    • Environment:
      Windows 10 x64
      JRE 1.8u144
      Jenkins 2.78
      Durable Task Plugin 1.14
      Jenkins installed as Standard Windows Service without a separate Webserver or Proxy
    • Similar Issues:

      Description

      Recently one of our PowerShell scripts failed but the pipeline didn't abort because Jenkins considered the PowerShell script to be succesful.

      You can reproduce the problem with the following example pipeline script:

      node {
        powershell '''
          $ErrorActionPreference = 'Stop'
          & cmd /c 'exit 0'
          Copy-Item 'doesnt' 'exist'
        '''
      }
      

       
      This script should fail because of ErrorActionPreference = 'Stop' and because Copy-Item will return an error because the file doesn't exist (resulting in $? = False).

      I assume that this happens because the PowerShell plugin first checks for $LASTEXITCODE, which was set to 0 by the "cmd" command. The problem is that $? isn't checked as well.

        Attachments

          Issue Links

            Activity

            Show
            gabloe Gabriel Loewen added a comment - Fixed in PR  https://github.com/jenkinsci/durable-task-plugin/pull/51
            Hide
            scm_issue_link SCM/JIRA link daemon added a comment -

            Code changed in jenkins
            User: Sam Van Oort
            Path:
            src/main/java/org/jenkinsci/plugins/durabletask/PowershellScript.java
            src/main/resources/org/jenkinsci/plugins/durabletask/powershellHelper.ps1
            src/test/java/org/jenkinsci/plugins/durabletask/PowershellScriptTest.java
            http://jenkins-ci.org/commit/durable-task-plugin/4a06f3cc8eb9053c0bf5597a06b69163c005c3b0
            Log:
            Merge pull request #51 from gabloe/master

            Fixed JENKINS-46876, JENKINS-46508, JENKINS-46496, JENKINS-48057, and JENKINS-47797

            Compare: https://github.com/jenkinsci/durable-task-plugin/compare/7c12b3a72cb4...4a06f3cc8eb9

            Show
            scm_issue_link SCM/JIRA link daemon added a comment - Code changed in jenkins User: Sam Van Oort Path: src/main/java/org/jenkinsci/plugins/durabletask/PowershellScript.java src/main/resources/org/jenkinsci/plugins/durabletask/powershellHelper.ps1 src/test/java/org/jenkinsci/plugins/durabletask/PowershellScriptTest.java http://jenkins-ci.org/commit/durable-task-plugin/4a06f3cc8eb9053c0bf5597a06b69163c005c3b0 Log: Merge pull request #51 from gabloe/master Fixed JENKINS-46876 , JENKINS-46508 , JENKINS-46496 , JENKINS-48057 , and JENKINS-47797 Compare: https://github.com/jenkinsci/durable-task-plugin/compare/7c12b3a72cb4...4a06f3cc8eb9
            Hide
            svanoort Sam Van Oort added a comment -

            Released with version 1.18

            Show
            svanoort Sam Van Oort added a comment - Released with version 1.18

              People

              • Assignee:
                gabloe Gabriel Loewen
                Reporter:
                mus65 m t
              • Votes:
                1 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: