简体   繁体   中英

Gitlab: use CI to check commited Python files with pylint

How can I set up CI in a Gitlab project that runs pylint on each python file that gets committed? (Maybe CI is also not the best strategy but the first idea I could think of.

Maybe the answer is already somewhere but I could not found it.

(Later, I also want to check all files that are already in the repository and I would also like to use some linters against shell and R scripts.)

This is what you can do

.gitlab-ci.yml

stages:
  - Lint

Lint:
  stage: Lint
  allow_failure: true
  script:
  - chmod +x lint.sh
  - ./lint.sh

lint.sh

#! /bin/sh

pip install pycodestyle
current_branch="$CI_BUILD_REF_NAME" 
echo $current_branch
all_changed_files=$(git diff --name-only origin/master origin/$current_branch)
echo "Checking changes!"
for each_file in $all_changed_files
do
# Checks each newly added file change with pycodestyle
pycodestyle $each_file
error_count=$(pycodestyle $each_file --count | wc -l)
if [ $error_count -ge 1 ]; then
    exit 1
fi
if [ $error_count -eq 0 ]; then
    exit 0
fi
done
echo "Completed checking"

Something like this should work:

stages:
  - lint

pylint:
  image: "python:latest"
  stage: lint
  script:
    - pip install pylint
    - pylint src/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM