1

I have a python script that exits with an error code inside a Github action, but the action still shows a success. I'd like for it to show an error when a non-zero exit code is thrown. Is there anything that should be changed in the script or workflow?

Python script:

import sys
import os
error_code = 256
print(f'error_code: {error_code}')
os._exit(error_code)
# have also tried sys.exit(error_code)

Github Workflow:

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    
    - name: Setup Python
      uses: actions/setup-python@v3
      with:
        python-version: '3.10'

    - name: test
      run: python3 ./test_error_exit.py

1 Answer 1

1

On linux based systems, process exit codes are the 8 least significant bits of the number returned.

This means returning 0-255 is valid, and after that it wraps around. (256 becoming 0, 257 becoming 1, etc.)

Please try again with an exit code like 5.

import sys
sys.exit(5)
Sign up to request clarification or add additional context in comments.

4 Comments

It does work if I do like you propose. I was getting 256 back from os.system('gcloud ...') and passing that to sys.exit(). Now I will just call sys.exit(1).
Do you have a source for "there is no such exit code"? I don't think this really answers the question - for me, Python returns errorcode 9009, but Github Actions also thinks this was a successful run even though the documentation states anything non-zero is recognized as error... docs.github.com/en/actions/creating-actions/…
On linux systems, and iirc POSIX systems in general, the exit code is the least significant 8 bits of the returned status. linux.die.net/man/2/waitpid Look for "WEXITSTATUS"
Since only the last 8 bits are considered the status, anything over 255 will get wrapped, meaning 256 will be exit code 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.