在存储库 GITLAB CI/CD 中检测到可疑的所有权

Bùi*_*Trí 6 image gitlab docker cicd

我在尝试使用 gitlab ci/cd 部署脚本时遇到这个问题:

Initialized empty Git repository in C:/builds/Tri.BuiV/test-gitlab-cicd/.git/
fatal: detected dubious ownership in repository at 'C:/builds/Tri.BuiV/test-gitlab-cicd'
'C:/builds/Tri.BuiV/test-gitlab-cicd' is owned by:
    'S-1-5-83-1-1989435290-1148643240-1709935003-3943614564'
but the current user is:
    'S-1-5-93-2-1'
To add an exception for this directory, call:
    git config --global --add safe.directory C:/builds/Tri.BuiV/test-gitlab-cicd
Run Code Online (Sandbox Code Playgroud)

我试过:

git config --global --add safe.directory C:/builds/Tri.BuiV/test-gitlab-cicd
Run Code Online (Sandbox Code Playgroud)

但是同样的错误,为什么呢?

在此输入图像描述

我试过:

git config --global --add safe.directory C:/builds/Tri.BuiV/test-gitlab-cicd
Run Code Online (Sandbox Code Playgroud)

但遇到同样的问题。

fig*_*fig 10

一个很好的解决方法已发布在 GitLab 论坛。是将以下 git 相关环境变量添加到受影响的 runner 的config.toml中:

[[runners]]
  environment = ["GIT_CONFIG_COUNT=1", "GIT_CONFIG_KEY_0=safe.directory", "GIT_CONFIG_VALUE_0=*"]
Run Code Online (Sandbox Code Playgroud)

这对我有用。


Von*_*onC 5

如果错误仍然存​​在,则可能意味着您git config --global(这会影响%USERPROFILE%\.gitconfig)没有使用与运行 GitLab CI/CD 的帐户相同的帐户。

如果 GitLab 使用不同的帐户运行,它可能会尝试访问您最初创建的文件夹。
GitLab 管道本身需要包括:

git config --global --add safe.directory $CI_PROJECT_DIR
Run Code Online (Sandbox Code Playgroud)

这是我在MR 3538中为 GitLab 15.8 自动添加的内容。


solstice333在第 29022 期评论中指出,Kevin Navero解释道:gitlab-org/gitlab-runner

我为我的案例找到了解决方法。

为了更清楚地说明我的环境,我在 Windows 服务器上使用 docker-windows 执行器/运行器和 powershell。
忘记我之前提到的关于怀疑在与/git config不同的容器下运行的内容。我认为这不再准确了。git clonefetch

Gitlab-runner 14.10.1 适用于我,所以我回滚到它。

结果,不知何故,“可疑所有者”错误被推送到运行时的稍后时间点,在主.gitlab-ci.yml构建脚本中。这允许我在 中
执行,而不是在任何其他脚本中执行。我不知道其他脚本在哪里或什么容器中运行。git config --global --add safe.directory ...pre_build_scriptpre_*pre_*

AFAIK,这个版本gitlab-runner不支持该--docker-isolation论点,也不识别runners[i].docker.isolation = "hyperv".
实现此目的的替代解决方案是编辑 docker 守护程序 json 配置,位于 或%userprofile%\.docker\windows-daemon.json%programdata%\docker\config\daemon.json
要添加的条目是“ exec-opts":["isolation=hyperv"].
isolation=hyperv需要配置 CPU 和内存。否则,配置 CPU 和内存的请求将被忽略(在进程隔离中)。
当然,如果不为每个 docker-windows 执行程序配置资源子集,则需要担心的是,多个docker 容器可以在单个主机上生成,过多的进程会导致主机因过多的上下文切换而不堪重负。

在 中config.toml,我添加了以下内容:

[[runners]]
  ...
  pre_build_script = """
$CI_PROJECT_POSIX_PATH = python -c "from pathlib import Path; >print(Path(r'$CI_PROJECT_DIR').resolve().as_posix())"
echo "> git config --global --add safe.directory >$CI_PROJECT_POSIX_PATH"
git config --global --add safe.directory $CI_PROJECT_POSIX_PATH
"""
  ...
Run Code Online (Sandbox Code Playgroud)

Python3.11 可以方便地烘焙到在 中指定的 docker 映像中,.gitlab-ci.yml以便运行主构建脚本。$CI_PROJECT_DIR类似于c:\builds\nextest-eng\usa\magnum,全部为小写。
来自 Git 的“可疑所有者”错误消息建议执行git config --global --add safe.directory C:/builds/nextest-eng/usa/magnum,事实证明,即使在 Windows 上,这也是区分大小写的(我很愚蠢地忽略了这一点,因为 git-for-windows 对于跟踪路径是区分大小写的)。

Python 用于自动映射$CI_PROJECT_DIR到精确的区分大小写的路径,并使用 git-config 建议使用的 posix 分隔符。
在此示例中,$CI_PROJECT_POSIX_PATH结果为C:/builds/nextest-eng/usa/magnum.
几个小时以来,我一直将其设置c:/builds/nextest-eng/usa/magnum为“我的” safe.directory,因为驱动器号错误地小写,因此被忽略为不匹配的目录路径。

这可能适用于 gitlab-runner 15.10,使用未弃用的pre_get_sources_script钩子(或已弃用的pre_clone_script),但如果它不使用.gitlab-ci.yml指定的图像并使用 gitlab-runner-helper ,则将找不到 python 并且会失败。
为了节省时间,而且 gitlab-runner 15.10 没有提供我现在需要的任何额外增益,我将坚持使用 gitlab-runner 14.10.1。