如何在Gitlab CI中使用--remote提取子模块?

时间:2019-05-30 22:26:06

标签: gitlab gitlab-ci gitlab-ci-runner

我需要Gitlab CI来更新带有--remote标志的子模块,以便将HEAD设置为遥控器的HEAD。经过一番谷歌搜索之后,我发现需要将GIT_SUBMODULE_STRATEGY设置为none并手动运行git submodule update --recursive --remote --init

variables:
    GIT_STRATEGY: clone
    GIT_SUBMODULE_STRATEGY: none

before_script:
  - apk add git || ( apt-get update && apt-get -y install git )
  - git submodule update --recursive --remote  --init

test:build:
  services:
  - docker:dind
  image: ubuntu
  variables:
    DOCKER_HOST: tcp://docker:2375
    DOCKER_DRIVER: overlay2
  script:
  - echo "done

不幸的是,我遇到配置项失败(名称已编辑):

$ git submodule update --recursive --remote  --init
Submodule 'current_project_name/submodule_project_name' (ssh://git@gitlab.someserver.net:9931/someorg/submodule_project_name.git) registered for path 'current_project_name/submodule_project_name'
Cloning into '/builds/someorg/current_project_name/current_project_name/submodule_project_name'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: clone of 'ssh://git@gitlab.someserver.net:9931/someorg/submodule_project_name.git' into submodule path '/builds/someorg/current_project_name/current_project_name/submodule_project_name' failed
Failed to clone 'current_project_name/submodule_project_name'. Retry scheduled
Cloning into '/builds/someorg/current_project_name/current_project_name/submodule_project_name'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
fatal: clone of 'ssh://git@gitlab.someserver.net:9931/someorg/submodule_project_name.git' into submodule path '/builds/someorg/current_project_name/current_project_name/submodule_project_name' failed
Failed to clone 'current_project_name/submodule_project_name' a second time, aborting

我可以看到CI确实具有克隆该submodule_project_name的权限,因为如果我设置了GIT_SUBMODULE_STRATEGY例如到recursive,CI设法将其拉出(但不是--remote,因此它无法按我想要的方式工作)。不幸的是,当我的before_script尝试执行此操作时,出现了错误。我该如何绕过?

1 个答案:

答案 0 :(得分:1)

mentioned before更新~/.ssh/.known_hosts文件为in here

fetching the submodules before the script(不需要将GIT_SUBMODULE_STRATEGY设置为NONE时执行此操作)

使用dind(在Docker中,在Docker中),还要考虑this thread,关于私钥的ssh-add和.dockerini / .dockerenv SSH指令。

OP d33tah确认in the comments

  

实际上我没有添加任何密钥,因为既然Gitlab CI的默认值可以拉出密钥,那么我也应该能够。
  然后我发现文档说我需要一个部署密钥,然后添加了一个

是的:必须在Gitlab一侧添加公钥。