How to provide credentials for submodules not hosted in gitlab

I would like to add a git repository as a submodule to my project. This git repository is not hosted in gitlab.
My question is how to provide the credentials for this repo for CI/CD user.

Here is the only solution what I found so far:

So this approach is suggesting to define a before_script which would define custom credentials in the .submodules file before cloning the submodule

  # git clone without --recursive already happend
  - git submodule deinit --all --force # Enforce that the submodules are not cached before changing them
  - sed -i "s/\/dev\/customer-1.git/https:\/\/gitlab-ci-token:$\/dev\/customer-1.git/" .gitmodules
  - sed -i "s/\/dev\/customer-2/https:\/\/gitlab-ci-token:$\/dev\/customer-2.git/" .gitmodules
  - git submodule init
  - git submodule update
  - bundle install

Any other way to provide the credentials?
Thanks in advance!

Basically for https remotes your only option is to change the remote url for each submodule and include the basic auth credentials in it.

You can do it using the code you have found or use git submodule foreach.

But my problem is gitlab-ci job starts with git commit checkout and the init job starts executed after that - so the changes can not be applied before the checkout:

Running on runner-za396k5g-project-236-concurrent-8 via node-10-120-10-10...
Getting source from Git repository
Fetching changes...
Initialized empty Git repository in /builds/ZA396K5g/8/
Created fresh repository.
Checking out b831b37b as detached HEAD (ref is V
Submodule 'xxx' (https://gitlab-ci-token:[MASKED]@xxx) registered for path 'yyy'
Submodule 'yyy' (ssh://gerrit.yyy) registered for path 'yyy'
Synchronizing submodule url for 'xxx'
Synchronizing submodule url for 'yyy'
Cloning into '/builds/ZA396K5g/8/xxx'...
Cloning into '/builds/ZA396K5g/8/yyy'...
error: cannot run ssh: No such file or directory
fatal: unable to fork
fatal: clone of 'ssh://gerrit.yyyy' into submodule path '/builds/ZA396K5g/8/yyy' failed
Failed to clone 'yyy'. Retry scheduled

the before script is:

  stage: init
    - git config --global url."https://gerrit.yyy/".insteadOf "ssh://gerrit.yyy"
    - git submodule sync && git submodule update --init

the .gitmodules is:

[submodule "xxx"]
        path = xxx
        url = ../xxx
[submodule "yyy"]
        path = yyy
        url = ssh://gerrit.yyy

The error states that you don’t have ssh in your container. It is trying to use container, because the URL is most likely not changed. You can confirm that if you cat the config after git config --global.... I’d say you need to drop the --global flag there.