0

I am writing the following command in jenkinsfile which is running it in the powershell on the server.

\\check_pycodestyle.bat 2>&1 | tee pycodestyle.log; if ($LASTEXITCODE -ne 0) {Write-Output "it worked again"}

It works fine when I run it in my local powershell. But, when I tried to run it on server environment with a jenkinsfile (after modifying ${LASTEXITCODE} ) it is giving me following error.

 groovy.lang.MissingPropertyException: No such property: LASTEXITCODE for class: groovy.lang.Binding

Any idea why this happens?

  • 1
    Escape the `$` to prevent groovy from interpolating it before PowerShell is invoked: `\$LASTEXITCODE` – Mathias R. Jessen Apr 03 '23 at 13:20
  • @Mathias R. Jessen this does not work. I made this modification, the program exited with exit code 1 in jenkinsfile and in the local powershell window, this gives an error. – programmer_04_03 Apr 03 '23 at 13:53
  • As an aside: `$LASTEXITCODE -ne 0` indicates an _error_ condition; `-eq 0` indicates success. To explicitly report the last exit code to the caller, use `exit $LASTEXTCODE` as the last statement (note Mathias' comment re escaping for the sake of Groovy). – mklement0 Apr 03 '23 at 15:13
  • So, the statement should be, ```if (exit $LASTEXITCODE -eq 0)``` correct? – programmer_04_03 Apr 04 '23 at 06:26
  • No, you need `exit $LASTEXITCODE` in isolation as the _last_ statement. If you want to act on the last exit code before that, use something like `if ($LASTEXITCODE -eq 0) {"it worked again"}` – mklement0 Apr 04 '23 at 06:33
  • I tried this. Did not work. I am getting the same error. The problem still remains the same that it will run correctly on my local machine but, it will fail in the jenkinsbuild. – programmer_04_03 Apr 04 '23 at 07:39
  • Hello all, I had raised a different question in stack overflow and the answer to that question made the problem go away. The link to that question is attached here. And I am closing this discussion. https://stackoverflow.com/questions/75926788/jenkins-pipeline-failinig-based-on-executed-batch-file-exit-code – programmer_04_03 Apr 04 '23 at 08:37

0 Answers0