2

In a Jenkins script, I do :

def status = powershell(
    script: """
        # Display current directory
        Write-Host "Current directory: \$(Get-Location)"

        # Display Git version
        git --version

        # Display HEAD's SHA
        git rev-parse HEAD

        # Test git diff
        Write-Host "git diff --quiet ${some_SHA1} HEAD --"
        git diff --quiet ${some_SHA1} HEAD --
        \$code = \$LASTEXITCODE
        Write-Host "Git exited with code \$code"
        exit \$LASTEXITCODE
    """,
    returnStatus: true)

print "status = ${status}"

${some_SHA1} is a random SHA1. There is no associated commit. In this case, the Git command returns 128 as exit code, and some error message on the prompt.

On the system where Jenkins is hosted and executed, I tested the command in the current directory displayed by the script, and it does what is expected.

But the script, after the git diff command, display: Git exited with code 0 The value should be 128.

And of course, outside the Powershell script, status = 0.

I tested also with cmd() command, and I got the same result.

I don't understand this behavior. Would anyone have an explanation ?

EDIT 1: Add independent PowerShell script:

param($some_SHA1)

# Display current directory
Write-Host "Current directory: $(Get-Location)"

# Display Git version
git --version

# Display HEAD's SHA
git rev-parse HEAD

# Test git diff
Write-Host "git diff --quiet ${some_SHA1} HEAD --"
git diff --quiet ${some_SHA1} HEAD --
$code = $LASTEXITCODE
Write-Host "Git exited with code \$code"
exit $LASTEXITCODE

Works as expected when run from Powershell prompt:

  • the trace instruction in the Powershell code outputs 128
  • the code return ($LASTEXITCODE) called on the prompt outputs 128

Works as expected when run from cmd prompt:

  • the trace instruction in the Powershell code outputs 128
  • the code return ($?) called on the prompt outputs False

EDIT 2: I am wondering if Jenkins restrict the reading of the value on 7 bits...

9
  • Can you show us the powershell script if you actually run it locally? I do not use powershell myself so I have no idea if that syntax is actually correct or not (even though it looks correct..... but I am not sure). Commented Sep 5 at 16:30
  • it should work. create ps1 file on nginx worker that corresponds to your code and try to execute it with powersell. check the result and error code. Commented Sep 5 at 23:41
  • @eftshift0 See edited original post. Commented Sep 8 at 11:01
  • @daggett I ran the .ps1 added in the original post on the Jenkins server, and it ouutpus the expected value (128). Commented Sep 8 at 11:08
  • the ps1 code is different. try to print this triple-quoted string, copy the printed result to server and run it. Commented Sep 8 at 13:23

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.