如何传递凭据以在 Gitlab CI 脚本中提取子模块?

Tho*_*mas 3 git-submodules gitlab gitlab-ci

我有几个项目,每个项目都有自己的存储库,它们导入一个公共库,该库也有自己的存储库。因此,该.gitmodules文件包含全名的库:

Submodule 'xx/yy' (https://gitlab.com/xx/yy.git) registered for path 'xx/yy'
Run Code Online (Sandbox Code Playgroud)

但这不起作用:

Fatal: could not read Username for 'https://gitlab.com': No such device or address
Run Code Online (Sandbox Code Playgroud)

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"
Run Code Online (Sandbox Code Playgroud)

旧的答案是: GitLab pull submodules inside CI

但事情已经改变,根据文档,我们可以拥有没有相对路径的子模块,如下所示: https: //docs.gitlab.com/ce/ci/git_submodules.html

rub*_*cks 6

太棒了;像这样:

# .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..."
Run Code Online (Sandbox Code Playgroud)

解释

和声明是样板文件 --- 它们通知 gitlab ci 机器存在一个阶段(名为)和stages: - build一个“属于”该阶段的作业。job1: stage: buildbuild

before_script部分详细说明了工作早期需要发生的事情——其中的所有内容都必须在script开始之前完成。

指示使用名为“store”的凭据帮助程序git config --global credentials.helpergit默认情况下,这是一个明文文件,位于~/.git-credentials包含换行符分隔的用户名密码修饰的 URI,每个 URI 对应于用户添加的给定 git 远程。

告诉不要忽略对 的任何调用(显式或其他方式)的git config --global credentials.useHttpPath属性。这并不是绝对必要的,而是一个很好的实践,例如,当您在同一个.gitpathgit credentialhost

读取git credential approve标准输入(表示为定界符)并将给定凭证传递给credential.helper,即store,以写入~/.git-credentials

使用 引用的内容填充git submodule update --init --recursive现有(但尚未完成)超级项目工作树.gitmodules

假设

上述示例做出以下假设:

参考: