【问题标题】:How to pre-install pre commit into hooks into docker如何将预提交预安装到 docker 的钩子中
【发布时间】:2021-08-12 09:28:03
【问题描述】:

据我了解文档,每当我将这些行添加到配置中时:

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.1.0
    hooks:
    -   id: trailing-whitespace

它预先提交从这个 repo 下载钩子代码并执行它。是否可以以某种方式将所有挂钩预安装到 Docker 映像中。那么当我拨打pre-commit run时没有使用网络?

我找到了文档的this 部分,该部分描述了预提交如何缓存所有存储库。它们存储在~/.cache/pre-commit 中,可以通过更新PRE_COMMIT_HOME 环境变量来配置。

但是,缓存仅在我执行pre-commit run 时才有效。但我想预先安装所有不运行检查的东西。有可能吗?

【问题讨论】:

    标签: pre-commit.com


    【解决方案1】:

    您正在寻找pre-commit install-hooks 命令

    至少你需要这样的东西来缓存预提交环境:

    COPY .pre-commit-config.yaml .
    RUN git init . && pre-commit install-hooks
    

    免责声明:我创建了预提交

    【讨论】:

      【解决方案2】:

      @anthony-sottile 提供的片段就像​​魅力一样。它有助于利用 docker 缓存。这是来自 django world 的一个工作变体。

      ARG PYTHON_VERSION=3.9-buster
      
      # define an alias for the specfic python version used in this file.
      FROM python:${PYTHON_VERSION} as python
      
      # Python build stage
      FROM python as python-build-stage
      
      ARG BUILD_ENVIRONMENT=test
      
      # Install apt packages
      RUN apt-get update && apt-get install --no-install-recommends -y \
        # dependencies for building Python packages
        build-essential \
        # psycopg2 dependencies
        libpq-dev
      
      # Requirements are installed here to ensure they will be cached.
      COPY ./requirements .
      
      # Create Python Dependency and Sub-Dependency Wheels.
      RUN pip wheel --wheel-dir /usr/src/app/wheels  \
        -r ${BUILD_ENVIRONMENT}.txt
      
      
      # Python 'run' stage
      FROM python as python-run-stage
      
      ARG BUILD_ENVIRONMENT=test
      ARG APP_HOME=/app
      
      ENV PYTHONUNBUFFERED 1
      ENV PYTHONDONTWRITEBYTECODE 1
      ENV BUILD_ENV ${BUILD_ENVIRONMENT}
      
      WORKDIR ${APP_HOME}
      
      # Install required system dependencies
      RUN apt-get update && apt-get install --no-install-recommends -y \
        # psycopg2 dependencies
        libpq-dev \
        # Translations dependencies
        gettext \
        # cleaning up unused files
        && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
        && rm -rf /var/lib/apt/lists/*
      
      # All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction
      # copy python dependency wheels from python-build-stage
      COPY --from=python-build-stage /usr/src/app/wheels  /wheels/
      
      # use wheels to install python dependencies
      RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \
          && rm -rf /wheels/
      
      COPY ./compose/test/django/entrypoint /entrypoint
      RUN chmod +x /entrypoint
      
      COPY .pre-commit-config.yaml .
      RUN git init . && pre-commit install-hooks
      
      # copy application code to WORKDIR
      COPY . ${APP_HOME}
      
      ENTRYPOINT ["/entrypoint"]
      

      然后您可以以类似的方式触发预提交检查:

      docker-compose -p project_name -f test.yml run --rm django pre-commit run --all-files
      

      【讨论】:

        猜你喜欢
        • 2020-10-28
        • 2013-12-15
        • 2017-02-08
        • 2016-09-02
        • 1970-01-01
        • 1970-01-01
        • 2017-12-10
        • 2017-11-09
        相关资源
        最近更新 更多