繁体   English   中英

带有访问令牌不一致行为的 git 子模块更新

[英]git submodule update with Access Token inconsistent behavior

语境

在具有多个用户和组的自我管理 GitLab 实例上,我正在尝试在 main_project 的.gitlab-ci.yml中执行git submodule update --init --recursive main_project 这个 repo 包含一个子模块( filter_lib ),它本身包含一个子模块( helper_funcs ):

main_project
├── app
│   └── filter_lib                    <- submodule
│       ├── .gitmodules
│       ├── lib
│       └── helper_funcs              <- submodule
│           └── funcs
├── .gitmodules
├── .gitlab-ci.yml
├── .gi
└── tests
    └── test_stuff.py

main_project在一个 GitLab 组中(我们称之为group1 ),两个子模块( filter_libhelper_funcs )在另一个 GitLab 组和子组( group2/subgroupA )中,彼此没有访问权限:

my_gitlab_instance
├── group1
│   └── main_project
└── group2
    └── subgroupA
        ├── filter_lib
        └── helper_funcs

问题

我想init所有子模块。
首先,我在.gitlab-ci.yml的开头尝试了这段代码:

variables:
  GIT_SUBMODULE_STRATEGY: recursive

在尝试运行我的脚本之前,此 CI 失败并出现以下错误:

Updating/initializing submodules recursively with git depth set to 50...
Submodule 'app/filter_lib' (https://gitlab-ci-token:[MASKED]@my_gitlab_instance.com/group2/subgroupA/filter_lib.git) registered for path 'app/filter_lib'
Cloning into '/builds/group1/main_project/app/filter_lib'...
Submodule path 'app/filter_lib': checked out '28d6c0f2d0bc691c29a406f44ae9b69b4e00f2b2'
Submodule 'helper_funcs' (git@gitlab:group2/subgroupA/helper_funcs) registered for path 'app/filter_lib/helper_funcs'
Cloning into '/builds/group1/main_project/app/filter_lib/helper_funcs'...
error: cannot run ssh: No such file or directory
fatal: unable to fork
fatal: clone of 'git@gitlab:group2/subgroupA/helper_funcs' into submodule path '/builds/group1/main_project/app/filter_lib/helper_funcs' failed
Failed to clone 'helper_funcs'. Retry scheduled
Cloning into '/builds/group1/main_project/app/filter_lib/helper_funcs'...
error: cannot run ssh: No such file or directory
fatal: unable to fork
fatal: clone of 'git@gitlab:group2/subgroupA/helper_funcs' into submodule path '/builds/group1/main_project/app/filter_lib/helper_funcs' failed
Failed to clone 'helper_funcs' a second time, aborting
Failed to recurse into submodule path 'app/filter_lib'

这是意料之中的,因为group1/main_project没有对group2中的任何 repo 的读取权限。

所以我尝试了另一种方法,通过将GIT_SUBMODULE_STRATEGY更改为正常并允许group1/main_project main_project 访问group2/subgroupA/filter_libgroup2/subgroupA/helper_funcs以下方式:

对于filter_lib ,我进入 repo Settings > Access Tokens并生成了一个具有所有可用范围和Maintainer角色的令牌。 然后我将此标记添加到main_project > Settings > CI/CD > Variables作为名为FILTER_LIB_CLONE_KEY的掩码变量。 我对helper_funcs做了同样的事情,变量名为HELPER_FUNCS_CLONE_KEY

请注意,以下所有命令都是通过 main_project 的.gitlab-ci.yml main_project的。

然后我在尝试git submodule update之前对 main_project 的main_project进行了 sed,因此在 CI 阶段它看起来像这样:

$ cat .gitmodules
[submodule "app/filter_lib"]
    path = app/filter_lib
    url = https://gitlab-ci-token:[MASKED(FILTER_LIB_CLONE_KEY)]@my_gitlab_instance.com/group2/subgroupA/filter_lib.git

在 main_project 中运行git submodule update --init main_project克隆了group2/subgroupA/filter_lib的内容:

$ cd app/filter_lib
$ ls -al
total 23
drwxrwxrwx    4 root     root          4096 May 17 10:51 .
drwxrwxrwx    3 root     root          4096 May 17 09:24 ..
-rw-rw-rw-    1 root     root            40 May 17 09:24 .git
-rw-rw-rw-    1 root     root           137 May 17 10:51 .gitmodules
drwxrwxrwx    2 root     root          4096 May 17 10:52 helper_funcs
drwxrwxrwx    6 root     root          4096 May 17 09:24 lib

我对app/filter_lib/.gitmodules做了同样的事情,在 sed 之后的 CI 中看起来像这样:

$ cat app/filter_lib/.gitmodules
[submodule "helper_funcs"]
    path = helper_funcs
    url = https://gitlab-ci-token:[MASKED(HELPER_FUNCS_CLONE_KEY)]@my_gitlab_instance.com/group2/subgroupA/helper_funcs.git
    ignore = dirty

filter_lib ,然后我做了:

$ git submodule update
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
fatal: clone of 'git@gitlab:group2/subgroupA/helper_funcs' into submodule path '/builds/group1/main_project/app/filter_lib/helper_funcs' failed
Failed to clone 'helper_funcs'. Retry scheduled
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
fatal: clone of 'git@gitlab:group2/subgroupA/helper_funcs' into submodule path '/builds/group1/main_project/app/filter_lib/helper_funcs' failed
Failed to clone 'helper_funcs' a second time, aborting

但是,使用 HELPER_FUNCS_CLONE_KEY 在正确的位置手动克隆helper_funcs库是可行的。

为什么可以使用相同的 repo url 进行git clone而不是git submodule update

为什么git submodule update对第一个子模块有效,但对第二个子模块无效,即使访问权限相同?

正如@torek在评论中所说,git 仍在使用通过 ssh 的身份验证,而不是你想要的 https。

我有同样的问题。 就我而言,我在工作中安装了openssh 不安装openssh是 git 开始使用 https 的方法。

更准确地说,我的安装命令来自:

    - apk add gcc linux-headers musl-dev git openssh

    - apk add gcc linux-headers musl-dev git

不过,我不确定为什么 git 在安装openssh时使用 ssh 方法而不是 https 方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM