d33tah
d33tah

Reputation: 11561

How to pull submodules with --remote within Gitlab CI?

I need my Gitlab CI to update submodules with --remote flag so that the HEAD is set to the remote's HEAD. After a bit of Googling I found that I need to set GIT_SUBMODULE_STRATEGY to none and run git submodule update --recursive --remote --init manually:

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

Unfortunately I'm getting a CI failure (names edited):

$ git submodule update --recursive --remote  --init
Submodule 'current_project_name/submodule_project_name' (ssh://[email protected]: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://[email protected]: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://[email protected]: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

I can see that the CI does have permissions to clone that submodule_project_name because if I set GIT_SUBMODULE_STRATEGY e.g. to recursive, CI manages to pull it (but it's not --remote, so it doesn't work the way I want). Unfortunately when my before_script tries to do it, I'm getting the error. How can I bypass it?

Upvotes: 2

Views: 1702

Answers (1)

VonC
VonC

Reputation: 1323953

I mentioned before updating the ~/.ssh/.known_hosts file, as in here.

This is not needed when fetching the submodules before the script (which is not what you are doing with GIT_SUBMODULE_STRATEGY set to NONE)

With dind (Docker In Docker), consider also this thread, regarding ssh-add for private keys, and .dockerini / .dockerenv SSH directives.

The OP d33tah confirms in the comments:

I actually didn't add any key, assuming that since Gitlab CI's defaults can pull the key, I should be able to as well.
Then I found that docs say that I needed a deploy key and I added one

Yes: adding the public key on Gitlab side is mandatory.

Upvotes: 1

Related Questions