Search code examples
dockergitlabhookpre-commit-hookpre-commit.com

Git Pre-commit hooks


I need to set up a series of Gitlab hooks within a Docker image that will be triggered on pre-commit. I have a Docker file and a pre-commit-config.yaml which builds successfully with the exception of one hook which I cannot seem to get working.

.pre-commit-config.yaml

---
repos:
  - repo: local
    hooks:
      - id: check-merge-conflict
        name: Check for merge conflicts
        description: Check for files that contain merge conflict strings.
        entry: check-merge-conflict
        language: python
        types: [text]

      - id: epp-validate
        additional_dependencies: ['puppet']
        description: Validate syntax of Puppet EPP templates
        entry: /puppet-pre-commit-hooks/epp-validate
        files: \.epp$
        language: ruby
        name: Validate EPP templates

      - id: erb-validate
        description: Validate syntax of Ruby ERB templates
        entry: /puppet-pre-commit-hooks/erb-validate
        files: \.erb$
        language: ruby
        name: Validate ERB templates

      - id: puppet-lint
        additional_dependencies: ['puppet-lint']
        description: Check Puppet manifests for stylistic problems
        entry: /puppet-pre-commit-hooks/puppet-lint
        files: \.pp$
        language: ruby
        name: puppet-lint
        args:
          - --fail-on-warnings

      - id: puppet-validate
        additional_dependencies: ['puppet']
        description: Validate syntax of Puppet manifests
        entry: /puppet-pre-commit-hooks/puppet-validate
        files: \.pp$
        language: ruby
        name: Validate Puppet manifests

      - id: r10k-validate
        additional_dependencies: ['r10k']
        description: Validate syntax of Puppetfile using r10k
        entry: /puppet-pre-commit-hooks/r10k-validate
        files: ^Puppetfile$
        language: ruby
        name: Validate r10k Puppetfile

      - id: ruby-validate
        additional_dependencies: ['ruby']
        description: Validate syntax of ruby code
        entry: /puppet-pre-commit-hooks/ruby-validate
        files: \.rb$
        language: ruby
        name: Validate ruby syntax

I have had to set up the hooks as local hooks because the environment that this is being used on has an airgap which prevents access to the internet. To overcome this I have cloned some repos which have the appropriate hooks.

This builds successfully with the exception of the check-merge-conflict hook which it fails to find. In the docker container, the python code for this hook is located in /pre-commit-hooks/pre_commit_hooks/check_merge_commit.py.

I have tried amending the entry: value to '''/pre-commit-hooks/pre_commit_hooks/check_merge_commit pre_commit_hooks/check-merge-commit check_merge_commit'''

...but none of these worked.

Docker file

FROM ruby:2.5.0

SHELL ["/bin/bash", "-o", "pipefail", "-c"]
RUN source ~/.profile
RUN curl https://pre-commit.com/install-local.py | python -
COPY pre-commit/.pre-commit-config.yaml .
RUN git clone https://github.com/pre-commit/pre-commit-hooks.git
RUN git clone https://github.com/chriskuehl/puppet-pre-commit-hooks.git
RUN git clone https://github.com/adrienverge/yamllint.git
WORKDIR /jumanjihouse
RUN git clone https://github.com/jumanjihouse/pre-commit-hooks.git
RUN rm -rf /jumanjihouse/pre-commit-hooks/.git* /yamllint/.git* /puppet-pre-commit-hooks/.git* /pre-commit-hooks/.git* /usr/local/lib/ruby/gems/2.5.0/gems/*/.git*
WORKDIR /
RUN git init
RUN /root/bin/pre-commit run -a

Does anyone know what I am missing/doing wrong?

The hook is from Github


Solution

  • You shouldn't need to clone each repository manually and use local hooks in the way you're doing -- if you have clone access during build time you should be able to do the normal installation approach

    To ensure all of the hooks are pre-installed, you'll want to run pre-commit install-hooks as one of the commands in your dockerfile

    This will ensure that all of the hook executables are properly cached in your image and then will be available later when you run pre-commit run ...


    disclaimer: I'm the author of pre-commit