【问题标题】:Gitlab: use CI to check commited Python files with pylintGitlab:使用 CI 使用 pylint 检查提交的 Python 文件
【发布时间】:2018-11-17 08:54:14
【问题描述】:

如何在 Gitlab 项目中设置 CI,该项目在每个提交的 python 文件上运行 pylint? (也许 CI 也不是最好的策略,而是我能想到的第一个想法。

也许答案已经在某个地方,但我找不到。

(稍后,我还想检查存储库中已经存在的所有文件,并且我还想对 shell 和 R 脚本使用一些 linter。)

【问题讨论】:

    标签: python gitlab pylint


    【解决方案1】:

    这样的事情应该可以工作:

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

    【讨论】:

    • 您是否将- pylint src/ 称为特定命令?这将拉动No module named src/
    • @lcadc17 那么我想你没有src/ 目录
    • 是的,这就是我所说的。我的意思是,这不是 lint 检查提交文件的命令,如果你 pylint 某个文件夹,它将检查所有 *.py 文件,无论它们是否在最后一次提交中被更改
    【解决方案2】:

    这是你可以做的

    .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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多