I'm trying to run the pre-commit tool in a GitLab pipeline. I use a custom-built Python image that has git installed in the pipeline job. I install pre-commit at the start of the job using a pip command. I make sure to add pre-commit and it's dependencies to $PATH
and I check to see if git can be found by running git version
. When the job runs I get this error An error has occurred: FatalError: git failed. Is it installed, and are you in a Git repository directory?
Here's my GitLab YAML for the pipeline job:
lint.precommit:
image: ${CI_REGISTRY}/containers/python:v1.0.0
stage: lint
rules:
- if: '$CI_PIPELINE_SOURCE == "push"'
- when: never
variables:
PRE_COMMIT_HOME: ${CI_PROJECT_DIR}/.cache/pre-commit
before_script:
- pip install pre-commit --break-system-packages
- export PATH=/home/python/.local/bin:$PATH
script:
- git version
- pre-commit run --all
cache:
paths:
- ${PRE_COMMIT_HOME}
The pipeline job initializes a Git repository at the beginning of the job as indicated by the following output:
Getting source from Git repository
Fetching changes with git depth set to 20...
Initialized empty Git repository in /builds/renovate/.git/
Created fresh repository.
Checking out c96e0361 as detached HEAD (ref is main)...
Skipping Git submodules setup
I'm clearly in a Git repository so I don't understand why I'm getting this error. How do I solve this error?
UPDATE
When I run the Python image locally and run type git
I get git is a tracked alias for /usr/bin/git
.
When I run echo $PATH
I get /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
.
I added git config --global --add safe.directory '*'
right before running the pre-commit command and it worked. Thanks to the suggestion.
The pre-commit tool documentation does not have anything that relates to the error I originally received. Hopefully this solution will help others getting the same error in the future.