如何传递凭据以在 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: - buildjob1: 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 引用的内容填充现有(但尚未完成)的超级项目工作树。

假设

上述示例做出以下假设:

参考文献: