我有一个 Gitlab 项目,比如说
parentRepo
,它引用了 Github 上存在的子模块。
[submodule "submodules/child1"]
path = submodules/child1
url = https://github.com/my-org/child1.git
[submodule "submodules/child2"]
path = submodules/child2
url = https://github.com/my-org/child2.git
当我为我的
parentRepo
运行Gitlab CI时,它无法从Github克隆子模块,因为Github上的存储库不可公开访问并且需要一些身份验证凭据才能克隆。
我在 Gitlab CI 中遇到这样的错误,
fatal: could not read Username for 'https://github.com': No such device or address
我尝试在我的
before_script
工作的build
块中使用以下代码片段进行克隆,
- echo "https://${GITHUB_USER_NAME}:${GITHUB_TOKEN}@github.com"
- git config --global credential.helper store
- echo "https://${GITHUB_USER_NAME}:${GITHUB_TOKEN}@github.com" > ~/.git-credentials
另外,尝试过这种方法,
git config --global url."https://${GITHUB_USER_NAME}:${GITHUB_TOKEN}@github.com".insteadOf https://github.com
但到目前为止还没有任何效果。
我也尝试过使用
pre_clone_script
和 pre_get_sources_script
挂钩,但这些不受支持,因为我使用的是 Gitlab 14.9.5-ee
。
我希望,应该有一种方法可以让它工作,因为 Gitlab 支持具有绝对和相对 URL 的子模块,https://docs.gitlab.com/ee/ci/runners/git_submodules.html
但后来也发现了一些未解决的问题:
非常感谢对此的任何帮助!
我最终不得不求助于本机 git 命令来使其正常工作,因为这是 Gitlab here
的一个未决问题我最终通过以下步骤让它工作:
- Disabled Gitlab CI default clone, by using GIT_SUBMODULE_STRATEGY: none
- Set up the credentials for Github
- Added native git commands to clone sub-modules
这是来自
gitlab-ci.yml
的一些代码片段
variables:
GIT_SUBMODULE_STRATEGY: none
.clone-script-before: &clone-script-before
- git config --global credential.helper store
- echo "https://${GITHUB_USER_NAME}:${GITHUB_TOKEN}@github.com" > ~/.git-credentials
- git submodule sync --recursive
- git submodule update --init --recursive
Build:
variables:
GIT_SUBMODULE_STRATEGY: none
stage: build
script:
- *clone-script-before
- MORE BUILD STEPS