68

I have GitLab & GitLab CI set up to host and test some of my private repos. For my composer modules under this system, I have Satis set up to resolve my private packages.

Obviously these private packages require an ssh key to clone them, and I have this working in the terminal - I can run composer install and get these packages, so long as I have the key added with ssh-add in the shell.

However, when running my tests in GitLab CI, if a project has any of these dependencies the tests will not complete as my GitLab instance needs authentication to get the deps (obviously), and the test fails saying Host key verification failed.

My question is how do I set this up so that when the runner runs the test it can authenticate to gitlab without a password? I have tried putting a password-less ssh-key in my runners ~/.ssh folder, however the build wont even add the key, "eval ssh-agent -s" followed by ssh-add seems to fail saying the agent isn't running...

CSchulz
  • 10,882
  • 11
  • 60
  • 114
danbroooks
  • 2,712
  • 5
  • 21
  • 43
  • 1
    As of 2017, the highest-ranked answer is out-of-date. [Marco's answer using `GIT_SUBMODULE_STRATEGY`](http://stackoverflow.com/a/42224451/119527) is correct. I added this feature specifically to avoid the mess of dealing with injecting SSH keys. – Jonathon Reinhart Feb 25 '17 at 16:25
  • @JonathonReinhart But isn't this solution a securty issue? I could clone any private GitLab repository as long as I know the relative URL? The runner could zip the cloned content and send it via email. – Paebbels Feb 13 '18 at 00:17
  • 1
    @Paebbels No it is not an issue. As of GitLab 8.12, CI jobs run [as the user that triggered the pipeline](https://docs.gitlab.com/ee/user/project/new_ci_build_permissions_model.html), with a token that has a reduced set of privileges. The CI job can only access the same repositories as the person who pushed the code. – Jonathon Reinhart Feb 13 '18 at 01:02

12 Answers12

53

See also other solutions:


Here a full howto with SSH keys:

General Design

  • generating a pair of SSH keys
  • adding the private one as a secure environment variable of your project
  • making the private one available to your test scripts on GitLab-CI
  • adding the public one as a deploy key on each of your private dependencies

Generating a pair of public and private SSH keys

Generate a pair of public and private SSH keys without passphrase:

ssh-keygen -b 4096 -C "<name of your project>" -N "" -f /tmp/name_of_your_project.key

Adding the private SSH key to your project

You need to add the key as a secure environment variable to your project as following:

  • browse https://<gitlab_host>/<group>/<project_name>/variables
  • click on "Add a variable"
  • fill the text field Key with SSH_PRIVATE_KEY
  • fill the text field Value with the private SSH key itself
  • click on "Save changes"

Exposing the private SSH key to your test scripts

In order to make your private key available to your test scripts you need to add the following to your .gitlab-ci.yml file:

before_script:
  # install ssh-agent
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  # run ssh-agent
  - eval $(ssh-agent -s)
  # add ssh key stored in SSH_PRIVATE_KEY variable to the agent store
  - ssh-add <(echo "$SSH_PRIVATE_KEY")
  # disable host key checking (NOTE: makes you susceptible to man-in-the-middle attacks)
  # WARNING: use only in docker container, if you use it with shell you will overwrite your user's ssh config
  - mkdir -p ~/.ssh
  - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config

Code Snippet comes from GitLab documentation

Adding the public SSH key as a deploy key to all your private dependencies

You need to register the public SSH key as deploy key to all your private dependencies as following:

  • browse https://<gitlab_host>/<group>/<dependency_name>/deploy_keys
  • click on "New deploy key"
  • fill the text field Title with the name of your project
  • fill the text field Key with the public SSH key itself
  • click on "Create deploy key"
toch
  • 3,905
  • 2
  • 25
  • 34
  • @Ridermansb Thanks for the link. The answer comes from a personal doc. I forgot the origin of the code snippet. It is worth to mention it. Thanks again for having pointed out. – toch Jul 28 '16 at 10:25
  • Instead of echoing to disable StrictHostKeyChecking (and risking accidentally clobbering a real system's ssh config), add a gitlab CI variable like KNOWN_HOSTS that has the known host(s). See this for details: https://docs.gitlab.com/ee/ci/ssh_keys/#verifying-the-ssh-host-keys – labyrinth Jul 27 '18 at 16:49
  • Can I use a name other than `SSH_PRIVATE_KEY`? – a06e Nov 05 '18 at 15:36
  • becko, you can change the name of the var SSH_PRIVATE_KEY. If you do, be sure to update it everywhere. – toch Mar 21 '19 at 09:32
  • Security warning: That documentation doesn't use a passphrase on the private key. – bbaassssiiee Apr 23 '21 at 14:36
45

If you don't want to fiddle around with ssh keys or submodules, you can override the repo in git's configuration to authenticate with the job token instead (in gitlab-ci.yml):

before_script:
  - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.example.com/group/repo.git".insteadOf git@gitlab.example.com:group/repo.git
a544jh
  • 738
  • 9
  • 15
  • I have encountered a problem with this - the git config global persists on next jobs on my runner. It completely breaks the CI. Have you had this problem? I can not clone any resources from my gitlab because the `CI_JOB_TOKEN` is stale. – Darkowic Sep 17 '18 at 19:24
  • @Darkowic I use the Docker runner so I haven't encountered that problem. – a544jh Sep 18 '18 at 13:19
  • 1
    Me too. Though I solved it today :) Its because of npm... By default `npm install` modifies `package-lock.json`. When you set in your CI to only fetch changes instead of cloning, this modified `packge-lock.json` file persists between jobs and npm cannot install anything... – Darkowic Sep 18 '18 at 13:28
  • 2
    This solution is the most elegant. It works with dockerized GitLab Community Edition 11.6.4 – vpuente Jan 18 '19 at 17:40
  • You can also omit the group and repo to use the job token for all repos on the gitlab instance. See https://gist.github.com/Kovrinic/ea5e7123ab5c97d451804ea222ecd78a – Oliver Evans Mar 01 '21 at 00:06
  • please make sure you replace the colon on `git@gitlab.com:group/repo` to `gitlab.com/group/repo`. took me a while. – krivar Dec 13 '22 at 09:33
  • Why and how does GitLab allows `CI_JOB_TOKEN` to authenticate to a private repo? How does it know that given repo shouldnt be hidden but accessible to the repo where the CI is running? – damdafayton Feb 21 '23 at 14:39
31

I'm posting this as an answer since others weren't completely clear and/or detailed IMHO

Starting from GitLab 8.12+, assuming the submodule repo is in the same server as the one requesting it, you can now:

  1. Set up the repo with git submodules as usual (git submodule add git@somewhere:folder/mysubmodule.git)

  2. Modify your .gitmodules file as follows

     [submodule "mysubmodule"]
       path = mysubmodule
       url = ../../group/mysubmodule.git
    

    where ../../group/mysubmodule.git is a relative path from your repository to the submodule's one.

  3. Add the following lines to gitlab-ci.yml

     variables:
       GIT_SUBMODULE_STRATEGY: recursive
    

    to instruct the runner to fetch all submodules before the build.

Caveat: if your runner seems to ignore the GIT_SUBMODULE_STRATEGY directive, you should probably consider updating it.

(source: https://docs.gitlab.com/ce/ci/git_submodules.html)

Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • 6
    I don't favour this approach because I don't think Gitlab should be forcing me to configure submodules in a specific way. I prefer to [use Gitlab tokens and keep the configuration in `.gitlab-ci.yml`](https://stackoverflow.com/a/53758544/474189). – Duncan Jones Dec 13 '18 at 09:23
  • Does this solution work if you want to run `git submodule update --init --recursive` locally? – Gus Jan 04 '19 at 06:33
  • In particular, how can one use this `.gitmodules`and `.gitlab-ci.yml` file to run `gitlab-runner` locally? – Gus Jan 04 '19 at 06:48
  • It doesn’t work for repositories on another server, that cannot have a relative path. – Thomas Sep 21 '19 at 12:30
  • @Gus Yes if you locally run `git submodule update --recursive --remote` it works. In my case all my submodules are in gitlab under same group though. – Sourav Sarkar May 08 '20 at 14:27
  • That doesn't seem to work anymore, at least not with `gitlab-runner` **13.3.1**. The runner does seem to use that relative path poorly (ie, from the current directory the command is ran from instead of from the repository url). – cglacet Oct 30 '20 at 10:23
  • In case the sumodule origin project in Gitlab is private, you need to add the a username and a username's access token to the URL in the .gitmodules file in order for the update to succeed: `url = https://:@gitlab/../group/mysubmodule.git` – Naama L Ackerman Aug 19 '21 at 11:36
26

The currently accepted answer embeds Gitlab-specific requirements into my .gitmodules file. This forces a specific directory layout for local development and would complicate moving to another version control platform.

Instead, I followed the advice in Juddling's answer. Here's a more complete answer.

My .gitmodules files has the following contents:

[submodule "myproject"]
    url = git@git.myhost.com:mygroup/myproject.git

In my gitlab-ci.yml I have the following:

build:
  stage: build
  before_script:
    - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@git.myhost.com/".insteadOf "git@git.myhost.com:"
    - git submodule sync && git submodule update --init

The trailing / and : are critical in the git config line, since we are mapping from SSH authentication to HTTPS. This tripped me up for a while with "Illegal port number" errors.

I like this solution because it embeds the Gitlab-specific requirements in a Gitlab-specific file, which is ignored by everything else.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • 5
    To save someone else from making the same silly pattern-matching mistake, for projects hosted on gitlab.com, `git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/".insteadOf "git@gitlab.com:"` – Gus Jan 04 '19 at 06:57
  • 1
    @Gus, can you post a working example of a ci config file? I posted a similar question here: https://stackoverflow.com/questions/58040183/can-anyone-post-a-working-example-of-gitlab-ci-that-has-external-submodules but can’t make this work at all – Thomas Sep 21 '19 at 12:48
7

I used deploy tokens to solve this issue, as setting up SSH keys for a test runner seems a little long winded.

git clone http://<username>:<deploy_token>@gitlab.example.com/tanuki/awesome_project.git

The deploy tokens are per project and are read only.

Juddling
  • 4,594
  • 8
  • 34
  • 40
4

One way to solve this without changing the git repository's structure is to perform the following steps:

1. get ssh host keys

Get the ssh host keys of the server that you are running on. For gitlab.com:

  1. run ssh-keyscan gitlab.com > known_hosts
  2. check that ssh-keygen -lf known_hosts agrees with the fingerprints reported here.
  3. copy the content of the known_hosts and paste it on a variable called SSH_KNOWN_HOSTS on the repository.

This step is only needed once.

2. configure the job to use ssh

before_script:
    - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com".insteadOf "git@gitlab.com:"
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts

The "ssh://git@gitlab.com" bit may be different if you are trying to do git clone git@gitlab.com: or pip install -e git+ssh://git@gitlab.com/...; adjust it accordingly to your needs.

At this point, your CI is able to use ssh to fetch from another (private) repository.

3. [Bonus DRY]

Use this trick to write it generically:

.enable_ssh: &enable_ssh |-
    git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com".insteadOf "ssh://git@gitlab.com"
    mkdir -p ~/.ssh
    chmod 700 ~/.ssh
    echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
    chmod 644 ~/.ssh/known_hosts

and enable it on jobs that need it

test:
    stage: test
    before_script:
        - *enable_ssh
    script:
        - ...
Jorge Leitao
  • 19,085
  • 19
  • 85
  • 121
  • 1
    the [3] worked for me when I needed to get private repositories using gitlab and `go get` in particular the `git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com".insteadOf "https://gitlab.com"` (it's a bit modified) – saniales Jan 25 '20 at 11:15
2
  1. If your CI runner is running on a container model, you need to use the deploy key. doc: https://docs.gitlab.com/ee/user/project/deploy_tokens/#git-clone-a-repository
git clone https://<username>:<deploy_token>@gitlab.example.com/tanuki/awesome_project.git
  1. Create your deploy token
  2. Add your token in CI pipeline Variable
  3. make sure your container has the git and change the git URL by insteadOf
  image: docker:latest
  before_script:
    - apk add --no-cache curl jq python3 py3-pip git
    - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.example.come/".insteadOf 'git@gitlab.example.come:'

for replace URL: https://docs.gitlab.com/ee/user/project/working_with_projects.html#authenticate-git-fetches

fei yang
  • 83
  • 7
1

I had a scenario where I had to use my ssh key in 3 different scripts, so I put the ssh key stuff in a single shell script and called it first, before the other 3 scripts. This ended up not working, I think due to the ssh-agent not persisting between shell scripts, or something to that effect. I ended up actually just outputting the private key into the ~/.ssh/id_rsa file, which will for sure persist to other scripts.

.gitlab-ci.yml

script:
    - ci/init_ssh.sh
    - git push # or whatever you need ssh for

ci/init_ssh.sh

# only run in docker:
[[ ! -e /.dockerenv ]] && exit 0

mkdir -p ~/.ssh
echo "$GITLAB_RUNNER_SSH_KEY" > ~/.ssh/id_rsa
chmod 400 ~/.ssh/id_rsa
echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > /.ssh/config

It works like a charm!

  • 1
    Hi @user3246077! Thank you so much for the answer. I found a mistake, in the last line the `~` symbol to reference the user's home is missing. Can you fix it? – garciparedes Mar 04 '20 at 10:37
1

If you are using an alpine-based image (maybe docker:latest or docker:dind), your before_script might look like this:

before_script:
    - apk add --no-cache openssh-client git
    - mkdir -p /.ssh && touch /.ssh/known_hosts
    - ssh-keyscan gitlab.com >> /.ssh/known_hosts
    - echo $SSH_KEY | base64 -d >> /.ssh/id_rsa && chmod 600 /.ssh/id_rsa
    - git clone git@git.myhost.com:mygroup/myproject.git
chaitan94
  • 2,153
  • 1
  • 18
  • 19
0

Adding this to .gitlab-ci.yml did the trick for me. (as mentioned here: https://docs.gitlab.com/ee/user/project/new_ci_build_permissions_model.html#dependent-repositories)

before_script:
    echo -e "machine gitlab.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc

(I tried setting up SSH_PRIVATE_KEY as mentioned in one of the answers above, it won't work)

Jay
  • 1
  • 1
0

Gitlab 15.9.0 introduces an update to the pre-defined variable CI_JOB_TOKEN. Now you can control other projects' access to your private repository, see the release note and documentation.

Once access is granted, you can clone private repositories by adding this line to your job's scripts or before_scripts.

git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.example.com/<namespace>/<project>

Unfortunately, this still does not play nicely with the submodule integration with Gitlab CI/CD. Instead, I do this in my projects.

# .gitlab-ci.yml
default:
  before_script:
  - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.example.com/<namespace>/<project>.git".insteadOf 'git@gitlab.example.com/<namespace>/<project>.git'
  - git submodule update --init --recursive

or like this

# .gitlab-ci.yml
default:
  before_script:
  - |
    cat << EOF > ~/.gitconfig
    [url "https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.example.com/<namespace>/<project>.git"]
        insteadOf = git@gitlab.example.com/<namespace>/<project>.git
    EOF
  - git submodule update --init --recursive

And this is what my .gitmodules would look like

[submodule "my-submodule"]
    path = modules/<project>
    url = git@gitlab.example.com/<namespace>/<project>.git
    branch = main

Hope this help!

Juno Wong
  • 1
  • 2
-1

Seems there is finally a reasonable solution.

In short as of GitLab 8.12 all you need to do is use relative paths in the .submodules, and the git submodule update --init will simply work

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177