Python Script Not Showing Output in CI/CD
Posted on
I'm running a Python script in my Gitlab CI/CD pipeline, and it basically looks like this:
if __name__ == "__main__":
    errors = do_thing("file.yml")
    if errors:
        print("The following errors were found:")
        for error in errors:
            print(f"- {error}")
        os._exit(1)
    else:
        print("File is looking good!")
        os._exit(0)
And the gitlab-ci.yml:
stages:
  - build
validate file:
  stage: build
  image: python:3.11
  script:
    - python3 ./scripts/check.py
If there are any errors they won't get shown before the pipeline fails.
This can be fixed by setting the environment variable PYTHONUNBUFFERED to true or using the -u flag:
stages:
  - build
validate file:
  stage: build
  image: python:3.11
  script:
    - python3 -u ./scripts/check.py
The explanation is that it "forces the stdout and stderr streams to be unbuffered".