1

我正在尝试使用 CircleCI 运行一个预提交挂钩,该挂钩为 Python 2.7 和 3.7 运行 pylint。

.circleci/config.yml为 Python 2 和 Python 3 运行预提交:

jobs:
  lint-py2:
    docker:
      - image: python:2.7.14
    steps:
      {snip}
      - run: pre-commit run --all-files
      {snip}

  lint-py3:
    docker:
      - image: python:3.7.3
    steps:
      {snip}
      - run: pre-commit run --all-files
      {snip}

除其他外,预提交运行 pylint:

-   repo: https://github.com/pre-commit/mirrors-pylint
    rev: v2.3.1  # Which version here?
    hooks:
    -   id: pylint

这里的问题是没有与 Python 2.7 和 3.7 兼容的 pylint 版本:Python 2.7 需要 pylint 1.x,而 Python 3.7 需要 pylint 2.x。

如何让 Circle CI 使用不同版本的 pylint 运行两个 linting 作业?

我正在考虑几种选择:

  • 在预提交配置中添加 pylint 两次(使用不同的别名)并在作业定义中 禁用其中一个或另一个
    • 似乎 pre-commit在查看变量之前SKIP尝试安装依赖项,因此 Python 2.7 运行无论如何都会尝试安装 pylint 2,并且出现错误ERROR: Could not find a version that satisfies the requirement pylint==2.3.1 (from pre-commit-dummy-package==0.0.0)
  • 使用具有两个 Python 版本的 Docker 映像,并在钩子级别 设置 Python 版本
    • 这需要构建我自己的 Docker 镜像
  • 在其中一项 linting 工作中跳过 pylint
  • 放弃 2.7 或 3.7 支持
4

1 回答 1

3

最简单的选择可能是同时安装 python2 和 python3 虽然可以使用多个配置文件来完成你想要的:

--config另一种选择是在 CI 期间通过使用该选项仅运行其中一个

有了这个,您将拥有默认值.pre-commit-config.yaml和特殊值,.pre-commit-config-py27.yaml其中包括 python2.7 pylint 而不是 python3 pylint

在 CI 中,您将调用pre-commit run --config .pre-commit-config-py27.yaml --all-files --show-diff-on-failurepython2.7 和pre-commit run --all-files --show-diff-on-failure非 py27 的正常运行

于 2019-09-02T23:59:46.897 回答