如何在 gitlab CI 中运行预提交

问题描述 投票:0回答:2

我尝试在我的

.gitlab-ci.yml
文件中设置如下工作:

precommit:
  image: python:3.10.2-slim-bullseye
  before_script:
    - pip install -r requirements.txt
  script:
    - pre-commit run --all-files

但是输出以下错误:

$ pre-commit run --all-files
An error has occurred: FatalError: git failed. Is it installed, and are you in a Git repository directory?

我检查了有一个

.git
正在使用该命令 - 并且存在 - 所以我不确定为什么会标记这个特定错误,或者如何修复它。

python gitlab continuous-integration pre-commit.com
2个回答
3
投票

python:3.10
图像不包含预提交所需的
git

安装git来解决问题:

precommit:
  image: python:3.10.2-slim-bullseye
  before_script:
    - apt update && apt install -y --no-install-recommends git
    - pip install -r requirements.txt
  # ...

作为旁注,也许还可以考虑看看这篇 SO 文章 解释了 gitlab 中预提交的使用以及其中包含的其他参考资料。


0
投票

遇到同样的错误后到达这里。问题很可能是 git 存储库所有者和 CI 镜像中的用户不匹配,这导致 git 失败并显示

fatal: detected dubious ownership in repository at '<CI_PROJECT_DIR>'
To add an exception for this directory, call:
    git config --global --add safe.directory '<CI_PROJECT_DIR>'

因此可以通过执行轻松解决这个问题

git config --global --add safe.directory $CI_PROJECT_DIR

在运行预提交之前。

© www.soinside.com 2019 - 2024. All rights reserved.