我有几个项目,每个项目都有自己的存储库,它们导入一个公共库,该库也有自己的存储库。
所以.gitmodules
文件包含全名的库:
Submodule 'xx/yy' (https://gitlab.com/xx/yy.git) registered for path 'xx/yy'
但这不起作用:
Fatal: could not read Username for 'https://gitlab.com': No such device or address
CI 脚本非常简单:
image: mcr.microsoft.com/dotnet/core/sdk:3.0.100-preview9-alpine3.9
variables:
GIT_SUBMODULE_STRATEGY: recursive
stages:
- build
before_script:
- "cd xx"
- "dotnet restore"
build:
stage: build
script:
- "cd xx"
- "dotnet build"
旧的答案是:GitLab 在 CI 中拉取子模块
但事情已经发生了变化,根据文档,我们可以拥有没有相对路径的子模块,如下所示:https://docs.gitlab.com/ce/ci/git_submodules.html
太棒了;像这样:
# .gitlab-ci.yml
stages:
- build
job1:
stage: build
before_script:
- git config --global credential.helper store
- git config --global credential.useHttpPath true
- |
git credential approve <<EOF
protocol=https
host=gitlab.com
path=my-group/my-submodule-repo.git
username=${CI_DEPENDENCY_PROXY_USER}
password=${CI_DEPENDENCY_PROXY_PASSWORD}
EOF
- git submodule update --init --recursive
script:
- echo "Let's start the build..."
解释
The stages: - build
and job1: stage: build
声明是样板文件 --- 它们通知 gitlab ci 机器存在一个阶段(名为build
)以及“属于”这个阶段的一项工作。
The before_script
部分详细说明了工作早期需要发生的事情 --- 其中的所有内容都必须在此之前完成script
已开始。
The git config --global credentials.helper
tells git
使用名为“store”的凭据助手。默认情况下,这是一个明文文件,位于~/.git-credentials
包含换行符分隔的用户名密码修饰的 URI,每个 URI 对应于用户添加的给定 git 远程。
The git config --global credentials.useHttpPath
tells git
不要忽视path
任何调用(显式或其他方式)的属性git credential
。这并不是绝对必要的,而是一个很好的实践,例如,当您在同一台计算机上有多个 git 遥控器时host
.
The git credential approve
读取标准输入(表示为定界符)并将给定的凭据传递给credential.helper
,即store
, 写入~/.git-credentials
.
The git submodule update --init --recursive
使用引用的内容填充现有(但尚未完成)超级项目工作树.gitmodules
.
假设
上述示例做出以下假设:
- 超级项目
.gitmodules
包含对子模块远程的引用https://gitlab.com/my-group/my-submodule-repo.git
.
- git 子模块远程是private;即,访问需要用户名和密码形式的凭据(gitlab 术语中的“个人访问令牌”)
- 您想要使用此处描述的 Gitlab CI 依赖代理凭据对此远程进行身份验证:https://about.gitlab.com/blog/2020/12/15/dependency-proxy-updates/ .
- You don't想要重写
.gitmodules
按照此处的建议使用相对 URL:https://docs.gitlab.com/ee/ci/git_submodules.html#configure-the-gitmodules-file .
参考:
- https://docs.gitlab.com/ee/ci/yaml/#before_script
- https://docs.gitlab.com/ee/ci/git_submodules.html#using-git-submodules-in-your-ci-jobs
- https://docs.gitlab.com/ee/ci/yaml/README.html#git-submodule-strategy
- https://gitlab.com/gitlab-org/gitlab/-/issues/208770
- https://git-scm.com/docs/gitcredentials
- https://git-scm.com/docs/git-credential-store
- https://git-scm.com/docs/git-credential
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)