areller
areller

Reputation: 5238

GitLab pull submodules inside CI

I have a GitLab project that utilises GitLab CI. The project also uses submodules, both the project and it's submodules are under the same GitLab account.

Here is my .gitmodules file

[submodule "proto_contracts"]
    path = proto_contracts
    url = https://gitlab.com/areller/proto_contracts.git

I also have this piece in the .gitlab-ci.yml file

variables:
  GIT_SUBMODULE_STRATEGY: recursive

However, when i run the CI I get this error

fatal: could not read Username for 'https://gitlab.com': No such device or address

Both the project and the submodules are in a private repository so you would expect to be prompted for authentication, but as I've mentioned, the project and the submodule are under the same account and one of the runner's jobs is to clone the original repository

enter image description here

So it's odd that it's unable to reach the submodule Is there a way around it?

Upvotes: 18

Views: 22911

Answers (2)

Erik Näslund
Erik Näslund

Reputation: 1376

So things have changed a bit over at Gitlab over the years, but these instructions work for me as of now.

  1. It's perfectly fine (and even encouraged) to use absolute URLs in your .gitmodules file.

  2. Add the following to your .gitlab-ci.yml file to configure the runner:

variables:
  GIT_SUBMODULE_STRATEGY: recursive
  GIT_SUBMODULE_FORCE_HTTPS: "true"

This tells Gitlab to convert any [email protected]/... URLs to their https:// equivalent and then automatically make use of the CI_JOB_TOKEN to get access to the repository the submodule is in.

  1. Runners don't automatically have access to other repositories. You need to go to the submodule repository and grant that access. Click the "Add project" you can see in the screenshot below, and enter the path to the repository that depends on the submodule.

gitlab token access setup

As long as the user who triggers the pipeline job has access to both the "main" and the submodule repository the runner should have all the access it needs now, and hopefully you should get a successful pipeline run! :).

Upvotes: 8

ilia
ilia

Reputation: 1132

You must use relative URLs for submodules. Update your .gitmodules as follow:

    [submodule "proto_contracts"]
        path = proto_contracts
        url = ../../areller/proto_contracts.git

Further reading: Using Git submodules with GitLab CI | GitLab Docs

Upvotes: 29

Related Questions