Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloning a private Repo from gitlab CI job using HTTPS without exposing my credentials into CLI

I have a gitlab ci job that does some work for me but it depends on another repo so I need to clone another repo inside this job. I can't use https cloning because gitlab will ask me for user name and password and the gitlab ci is non-interactive.
So how to clone gitlab repo using https inside gitlab ci job.
Thanks in advance.

like image 567
Shalan93 Avatar asked Jul 22 '19 09:07

Shalan93


People also ask

Can someone clone my private repository?

You can either clone a private Github repository with a password like you normally would with any other online service, or do it with a token if you enabled 2-factor-authentication on your account or your organization uses SAML SSO. Alternatively, you can also clone private Github repo with SSH credentials.

How do I clone a private git repository with username and password?

If you want to clone it to a specific folder, just insert the folder address at the end like so: git clone https://<token>@github.com/<username>/<repository.git> <folder> , where <folder> is, you guessed it, the folder to clone it to! You can of course use . , .. , ~ , etc.


2 Answers

If you are running gitlab version 8.12 or later, the permissions model was reworked. Along with this new permission model comes the the CI environment variable CI_JOB_TOKEN. The premium version of GitLab uses this environment variable for triggers, but you can use it to clone repos.

dummy_stage:
  script:
    - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.instance/group/project.git
like image 78
cecunami Avatar answered Sep 16 '22 21:09

cecunami


You should clone that repo using SSH and a read-only deploy key, possibly storing the token in a masked variable.

like image 29
Iron Bishop Avatar answered Sep 19 '22 21:09

Iron Bishop