Search code examples
python-3.xgitlab-ciflake8

Gitlab CI exit code 1 using with flake8


I don't understand why Job finished with exit code 1 in gitlab CI. If I don't use flake8 (f.ex run script line echo "hello world" > $FOLDER_NAME/my_test.txt) all good. But I see that flake8 found errors in directory:

...
$ mkdir -p $FOLDER_NAME
$ flake8 --max-line-length=120 --ignore=W605,W504 --tee --output-file=$FOLDER_NAME/$LOG_NAME $CHECKING_FOLDER
./framework/tests/test_5_helper.py:30:30: W292 no newline at end of file
./framework/tests/test_1_start.py:2:1: F401 'pprint.pprint' imported but unused
Cleaning up file based variables
00:00
ERROR: Job failed: exit code 1

yml-file:

stages:
  - check


pep8_check:
    stage: check
    image: python:3.8-alpine
    variables:
      FOLDER_NAME: 'logs'
      LOG_NAME: 'linter.log'
      CHECKING_FOLDER: './framework/tests'
    when: always
    before_script:
      - python -m pip install --upgrade pip
      - pip install flake8
      - export
      - mkdir -p $FOLDER_NAME
    script:
        - flake8 --max-line-length=120 --ignore=W605,W504 --tee --output-file=$FOLDER_NAME/$LOG_NAME $CHECKING_FOLDER
    artifacts:
      expire_in: 7 days
      paths:
        - $FOLDER_NAME/

Solution

  • Flake8 finds 2 errors so exits with 1, this makes the GitLab pipeline fail.

    You have a few options:

    • if you want GitLab to ignore any error flake8 may find, then you can just add the parameter --exit-zero, this will make flake8 exit with 0 which makes the GitLab pipeline successful
    • if you want to ignore those specific errors from your output:
    ./framework/tests/test_5_helper.py:30:30: W292 no newline at end of file
    ./framework/tests/test_1_start.py:2:1: F401 'pprint.pprint' imported but unused
    

    then you just have to add those to the ignore list like you did for others:

    change this --ignore=W605,W504 to this --ignore=W605,W504,W292,F401

    • you can also go and "fix/amend/change" your code so flake8 stops flagging those lines when parsing your source code

    In any case reading the help with flake8 --help may give some more ideas on how to tackle these corner cases depending on what you want to achieve.

    Also see here the lists of error/warning/violation codes E***, W***, F***: