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

Pipeline fails to detect failed powershell step

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Major Major
    • core, pipeline
    • Jenkins version 2.222.1
      Pipeline plugin version 2.6
      Pipeline: Nodes and Processes plugin version 2.35

      Consider following scripted pipeline:

       node ('windows') {
          stage ("stage 1") {
              powershell '''
                  & C:\\Temp\\test.ps1
              '''
          }
          
          stage ("stage 2") {
              echo "stage 2"
          }
      }

      Contents of the test.ps1 is following:

      param ($param1)
      
      $ErrorActionPreference = "Stop"
      Set-StrictMode -Version Latest
      
      Write-Host $undefVar

      The script is specifically designed to fail on trying to access undefined variable. This scenario is working correctly, Jenkins detects that PowerShell script returned something other than 0 exit code and fails pipeline execution.

      However! If the script to be changed this way (only line 1 has changed):

      param ([Parameter(Mandatory=$false)] $param1)
      
      $ErrorActionPreference = "Stop"
      Set-StrictMode -Version Latest
      
      Write-Host $undefVar

      Then the script fails exactly the same way, but Jenkins fails to detect this failure and proceeds to execute stage 2.

      Invoking both versions of the PowerShell script manually shows that in both cases powershell process exit code is 1. I fail to understand how adding parameter attribute to PowerShell script affects Jenkins ability to detect script outcome.

          [JENKINS-61866] Pipeline fails to detect failed powershell step

          Sergey Dedik added a comment -

          The behavior is also reproducible on fresh Jenkins 2.222.3 installation with only Pipeline plugin (and dependencies) being installed.

          Sergey Dedik added a comment - The behavior is also reproducible on fresh Jenkins 2.222.3 installation with only Pipeline plugin (and dependencies) being installed.

          Liam Baker added a comment - - edited

          Confirm this error on Jenkins 2.375.3 pipeline plugin versions
          Pipeline: Job Version 1268.v6eb_e2ee1a_85a
          Pipeline: Multibranch Version 733.v109046189126

          Snippet we tested with

          def status = powershell(returnStatus: true, script: "Copy-Item -Path 'Path_does_not_exist' -Destination 'Path_that_never_existed' -verbose")
                      if (status == 0) {
                          echo "Copy failed but status set to 0"
                      } else {
                          echo status
                      }
          

          ```

          Liam Baker added a comment - - edited Confirm this error on Jenkins 2.375.3 pipeline plugin versions Pipeline: Job Version 1268.v6eb_e2ee1a_85a Pipeline: Multibranch Version 733.v109046189126 Snippet we tested with def status = powershell(returnStatus: true , script: "Copy-Item -Path 'Path_does_not_exist' -Destination 'Path_that_never_existed' -verbose" )             if (status == 0) {                 echo "Copy failed but status set to 0"             } else {                 echo status             } ```

          Sergey Dedik added a comment - - edited

          sm__liam_baker I believe you are experiencing different issue from what has been reported here.

          By default many PowerShell cmdlets throw non-terminating errors causing scripts to continue executing even when error has been encountered. More on them here: https://devblogs.microsoft.com/scripting/understanding-non-terminating-errors-in-powershell/

          Because by default $ErrorActionPreference is set to "Continue" your script is being executed to the end and returns exit code 0. This is by PowerShell's design. This has nothing to do with Jenkins and can be reproduced by saving PowerShell script to a file, launching it from cmd.exe and examining %ERRORLEVEL% variable.

          In general setting $ErrorActionPreference = "Stop" helps in most cases.

          However, in case of Copy-Item with -Verbose switch there is a known bug, which causes it to ignore ErrorActionPreference variable: https://stackoverflow.com/questions/64916352

          This should behave as expected:

          def status = powershell(returnStatus: true, script: "Copy-Item -Path 'Path_does_not_exist' -Destination 'Path_that_never_existed' -verbose -erroraction stop")
                      if (status == 0) {
                          echo "Copy failed but status set to 0"
                       } else {
                           echo status
                       }

          Sergey Dedik added a comment - - edited sm__liam_baker I believe you are experiencing different issue from what has been reported here. By default many PowerShell cmdlets throw non-terminating errors causing scripts to continue executing even when error has been encountered. More on them here: https://devblogs.microsoft.com/scripting/understanding-non-terminating-errors-in-powershell/ Because by default $ErrorActionPreference is set to "Continue" your script is being executed to the end and returns exit code 0. This is by PowerShell's design. This has nothing to do with Jenkins and can be reproduced by saving PowerShell script to a file, launching it from cmd.exe and examining %ERRORLEVEL% variable. In general setting $ErrorActionPreference = "Stop" helps in most cases. However, in case of Copy-Item with -Verbose switch there is a known bug, which causes it to ignore ErrorActionPreference variable: https://stackoverflow.com/questions/64916352 This should behave as expected: def status = powershell(returnStatus: true , script: "Copy-Item -Path 'Path_does_not_exist' -Destination 'Path_that_never_existed' -verbose -erroraction stop" )             if (status == 0) { echo "Copy failed but status set to 0"             } else {                 echo status             }

            Unassigned Unassigned
            vodalus Sergey Dedik
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: