如何传递凭据以在 Gitlab CI 脚本中拉取子模块?
How do I pass credentials to pull a submodule in a Gitlab CI script?
我有几个项目,每个项目都在自己的存储库中,它们导入一个公共库,该库也有自己的存储库。
因此,.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"
旧答案是:
但情况已经发生变化,根据文档,我们可以拥有没有相对路径的子模块,如下所示:https://docs.gitlab.com/ce/ci/git_submodules.html
tldr;像这样:
# .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..."
说明
stages: - build
和 job1: stage: build
声明是样板文件 --- 它们通知 gitlab ci 机器存在一个阶段(名为 build
) 和一份“属于”这个阶段的工作。
before_script
部分详细说明了需要在工作早期发生的事情 --- 下面的所有事情都必须在 script
开始之前完成。
git config --global credentials.helper
告诉 git
使用名为“store”的凭据助手。默认情况下,这是一个位于 ~/.git-credentials
的明文文件,其中包含以换行符分隔的用户名-密码修饰的 URI,每个对应于用户添加的给定 git 远程。
git config --global credentials.useHttpPath
告诉 git
不要忽略对 git credential
的任何调用(explicit 或其他)的 path
属性。这不是绝对必要的,而是一种很好的做法,例如,当您在同一个 host
.
上有多个 git 遥控器时
git credential approve
读取标准输入(表示为 heredoc)并将给定的凭据传递给 credential.helper
,即 store
,以写入 ~/.git-credentials
。
git submodule update --init --recursive
使用 .gitmodules
引用的内容填充现有(但尚未完成)的超级项目工作树。
假设
上述示例做出以下假设:
- 超级项目
.gitmodules
包含对远程子模块 https://gitlab.com/my-group/my-submodule-repo.git
的引用。
- git 远程子模块是 私有的;即,访问它需要用户名和密码形式的凭据(git实验室用语中的“个人访问令牌”)
- 您想使用此处描述的 Gitlab CI 依赖代理凭据对此远程进行身份验证:https://about.gitlab.com/blog/2020/12/15/dependency-proxy-updates/ .
- 您不想重写
.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
我有几个项目,每个项目都在自己的存储库中,它们导入一个公共库,该库也有自己的存储库。
因此,.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"
旧答案是:
但情况已经发生变化,根据文档,我们可以拥有没有相对路径的子模块,如下所示:https://docs.gitlab.com/ce/ci/git_submodules.html
tldr;像这样:
# .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..."
说明
stages: - build
和 job1: stage: build
声明是样板文件 --- 它们通知 gitlab ci 机器存在一个阶段(名为 build
) 和一份“属于”这个阶段的工作。
before_script
部分详细说明了需要在工作早期发生的事情 --- 下面的所有事情都必须在 script
开始之前完成。
git config --global credentials.helper
告诉 git
使用名为“store”的凭据助手。默认情况下,这是一个位于 ~/.git-credentials
的明文文件,其中包含以换行符分隔的用户名-密码修饰的 URI,每个对应于用户添加的给定 git 远程。
git config --global credentials.useHttpPath
告诉 git
不要忽略对 git credential
的任何调用(explicit 或其他)的 path
属性。这不是绝对必要的,而是一种很好的做法,例如,当您在同一个 host
.
git credential approve
读取标准输入(表示为 heredoc)并将给定的凭据传递给 credential.helper
,即 store
,以写入 ~/.git-credentials
。
git submodule update --init --recursive
使用 .gitmodules
引用的内容填充现有(但尚未完成)的超级项目工作树。
假设
上述示例做出以下假设:
- 超级项目
.gitmodules
包含对远程子模块https://gitlab.com/my-group/my-submodule-repo.git
的引用。 - git 远程子模块是 私有的;即,访问它需要用户名和密码形式的凭据(git实验室用语中的“个人访问令牌”)
- 您想使用此处描述的 Gitlab CI 依赖代理凭据对此远程进行身份验证:https://about.gitlab.com/blog/2020/12/15/dependency-proxy-updates/ .
- 您不想重写
.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