175

I want to clone GitLab repository without prompt for my automation script, by using my private token from my GitLab account.

Can someone provide me a sample?

I know I can do so with user and password:

git clone https://" + user + ":" + password + "@" + gitlaburl;

and I know it is possible with ssh key

But, both options are insufficient.

Lii
  • 9,906
  • 6
  • 53
  • 73
Muky
  • 2,334
  • 3
  • 16
  • 20

14 Answers14

251

I know this is old but this is how you do it:

git clone https://oauth2:ACCESS_TOKEN@somegitlab.com/vendor/package.git

Roshan Gautam
  • 2,790
  • 1
  • 10
  • 11
  • 3
    This worked for me on GitLab 8.5.7 Enterprise Edition. – Ben Patterson Aug 05 '16 at 02:45
  • 1
    Works here (GitLab Community Edition 8.16.5 064dab1) – Martin M. Apr 06 '17 at 16:15
  • 8
    It works! I wonder why on gitlab.com on project details they don't give the complete command syntax :-(( – FRa Feb 28 '18 at 17:16
  • 1
    Works for Gitlab 10.4.4 but you need to make an `api` token. A `read_user` can only read repos under `/users` – Kurt Mar 17 '18 at 15:22
  • If there is a `submodule` inside, which hosted under the same account, the `clone` will still prompt to type in username and password. Is there any workaround? – weefwefwqg3 Sep 10 '18 at 18:14
  • The `oauth2` portion is probably arbitrary. I put the name of the access token instead. – Kenny Evitt Oct 16 '18 at 20:19
  • This worked for me, after create token with API permissions – Rutrus Nov 08 '18 at 11:56
  • 5
    How to use this over `ssh`? – hemu Apr 02 '19 at 06:00
  • This answer helped me a lot but a pratical example solved my problem. Supposing to use GitLab, the solution is "git clone https://oauth2:a3Fae3FaQty5aq@gitlab.com/pippo/projectname.git", where PERSONAL_ACCESS_TOKEN = a3Fae3FaQty5aq and pippo = username. Hope it will be helpful – Gianluca Jun 19 '20 at 07:25
  • 3
    Even `read_api` and `write_api` permissions are not enough. You have to have `api`. – Ella Sharakanski Dec 02 '20 at 14:48
57

The gitlab has a lot of tokens:

  • Private token
  • Personal Access Token
  • CI/CD running token

I tested only the Personal Access Token using GitLab Community Edition 10.1.2, the example:

git clone https://gitlab-ci-token:${Personal Access Tokens}@gitlab.com/username/myrepo.git


git clone https://oauth2:${Personal Access Tokens}@gitlab.com/username/myrepo.git

or using username and password:

git clone https://${username}:${password}@gitlab.com/username/myrepo.git

or by input your password:

git clone https://${username}@gitlab.com/username/myrepo.git

But the private token seems can not work.

Roman Snitko
  • 3,654
  • 21
  • 28
xuanyuanaosheng
  • 882
  • 7
  • 9
  • 5
    Note that private tokens were removed in favour of personal access tokens in GitLab 10.2: https://about.gitlab.com/2017/09/22/gitlab-10-0-released/#private-tokens – David Planella Nov 11 '18 at 06:05
  • And about user+password+token? How to express all in one URL? Now my gitlab-software server use all, login and two-factor (or token). – Peter Krauss Jan 06 '21 at 22:53
  • pretty amazing that I had to refer to this and there is nothing (I can find) on the official GitLab Docs on this – cryanbhu Feb 14 '21 at 05:47
42

You can do it like this:

git clone https://gitlab-ci-token:<private token>@git.example.com/myuser/myrepo.git
Tim Hughes
  • 2,405
  • 1
  • 19
  • 23
  • 2
    this seems right but it always fails authentication for me :( – Randyaa Apr 25 '16 at 01:52
  • same to me: fatal: Authentication failed for – vogash May 03 '16 at 10:22
  • 4
    needs to be replaced with the CI runner's token, not the account's private token. – Kip Jun 02 '16 at 15:08
  • 2
    i think you should also be able to use your personal token right @tim – Gobi Dasu Jul 02 '16 at 10:55
  • You can use the project specific ci token (enable builds, then go to the project/runners config). – BM5k Jul 26 '16 at 19:10
  • but I host an gitlab server for myself and its version is 6.6.2. I only have private token. When I run the command git clone https://" + user + ":" + password + "@" + gitlaburl; I got error fatal: Authentication failed for . same as @vogash – biolinh Feb 09 '17 at 12:02
  • It still works for me. @biolinh Did you try generating the personal access token with api scope, from the Acess Tokens section under user settings. – darkdefender27 Mar 08 '18 at 06:52
  • tested using personal access token, works as of 2019-03 – Erik Aronesty Mar 04 '19 at 15:29
  • This solution worked for me, the highest voted solution (using oauth2) did not work. Literally just changed from oauth2 to gitlab-ci-token and it started working – ET Come Back Mar 08 '19 at 20:02
31

Use the token instead of the password (the token needs to have "api" scope for clone to be allowed):

git clone https://username:token@gitlab.com/user/repo.git

Tested against 11.0.0-ee.

Zbyněk Winkler
  • 889
  • 1
  • 9
  • 10
16

If you already has a repository and just changed the way you do authentication to MFA, u can change your remote origin HTTP URI to use your new api token as follows:

git remote set-url origin https://oauth2:TOKEN@ANY_GIT_PROVIDER_DOMAIN/YOUR_PROJECT/YOUR_REPO.git

And you wont need to re-clone the repository at all.

Roger Barreto
  • 1,758
  • 1
  • 14
  • 21
  • 4
    `git clone https://oauth2:TOKEN@ANY_GIT_PROVIDER_DOMAIN/YOUR_PROJECT/YOUR_REPO.git` also worked for me, thank you!! I will Answer this thread with my correct solution. – Rutrus Nov 08 '18 at 11:54
14

You can use the runners token for CI/CD Pipelines of your GitLab repo.

git clone https://gitlab-ci-token:<runners token>@git.example.com/myuser/myrepo.git

Where <runners token> can be obtained from:

git.example.com/myuser/myrepo/pipelines/settings

or by clicking on the Settings icon -> CI/CD Pipeline and look for Runners Token on the page

Screenshot of the runners token location: Screenshot of the runners token location

jmq
  • 9,602
  • 14
  • 54
  • 68
Enlai
  • 148
  • 2
  • 4
10

One possible way is using a deploy token (https://docs.gitlab.com/ee/user/project/deploy_tokens). After creating the token, use:

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

as mentioned in the link above.

Vix
  • 818
  • 8
  • 16
shahar taite
  • 382
  • 5
  • 10
  • Neither does this seem to be working with npm install on a fresh docker container, defaults to ssh. – Vix Apr 17 '19 at 10:57
9

As of 8.12, cloning using HTTPS + runner token is not supported anymore, as mentioned here:

In 8.12 we improved build permissions. Being able to clone project using runners token it is no supported from now on (it was actually working by coincidence and was never a fully fledged feature, so we changed that in 8.12). You should use build token instead.

This is widely documented here - https://docs.gitlab.com/ce/user/project/new_ci_build_permissions_model.html.

Community
  • 1
  • 1
Yan Foto
  • 8,951
  • 4
  • 45
  • 79
  • 1
    It isn't possible using runners tokens but is using personal access tokens. Please see my answer: https://stackoverflow.com/questions/25409700/using-gitlab-token-to-clone-without-authentication/47347515#47347515 – Muhan Alim Nov 17 '17 at 09:42
  • @MuhanAlim I'd recommend no one to expose their whole account using access tokens. That's why they are called ***Private** Access Tokens*! – Yan Foto Nov 17 '17 at 11:09
  • The question doesn't mention anything about making the key public, only how to use the key in place of a username and password for cloning. But that it is a good point, I would not recommend anyone use the keys anywhere that is public. – Muhan Alim Nov 17 '17 at 11:45
  • 2
    *automation script* implies that the whole procedure is not running locally. Probably somewhere where others also have access to. – Yan Foto Nov 17 '17 at 15:19
8

Inside a GitLab CI pipeline the CI_JOB_TOKEN environment variable works for me:

git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/...

Source: Gitlab Docs

BTW, setting this variable in .gitlab-ci.yml helps to debug errors.

variables:
    CI_DEBUG_TRACE: "true"
Martin Thoma
  • 91,837
  • 114
  • 489
  • 768
Slawa
  • 953
  • 11
  • 20
5

many answers above are close, but they get ~username syntax for deploy tokens incorrect. There are other types of tokens, but the deploy token is what gitlab offers (circa 2020+ at least) per repo to allow customized access, including read-only.

from a repository (or group), find the settings --> repository --> deploy tokens. Create a new one. A username and token field are created. The username is NOT a fixed value by default; it's unique to this token.

git clone https://<your_deploy_token_username>:<the_token>@gitlab.com/your/repo/path.git

tested on gitlab.com public, free account.

some bits flipped
  • 1,524
  • 2
  • 17
  • 32
2

I went SSH using the per project deploy keys setting (read only)

Laurent
  • 998
  • 9
  • 22
1

To make my future me happy: RTFM - don't use the gitlab-ci-token at all, but the .netrc file.

There are a couple of important points:

  1. echo -e "machine gitlab.com\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
  2. Don't forget to replace "gitlab.com" by your URL!
  3. Don't try to be smart and create the .netrc file directly - gitlab will not replace the $CI_JOB_TOKEN within the file!
  4. Use https://gitlab.com/whatever/foobar.com - not ssh://git@foobar, not git+ssh://, not git+https://. You also don't need any CI-TOKEN stuff in the URL.
  5. Make sure you can git clone [url from step 4]

Background: I got

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

when I tried to make Ansible + Gitlab + Docker work as I imagine it. Now it works.

Martin Thoma
  • 91,837
  • 114
  • 489
  • 768
0

These days (Oct 2020) you can use just the following

git clone $CI_REPOSITORY_URL

Which will expand to something like:

git clone https://gitlab-ci-token:[MASKED]@gitlab.com/gitlab-examples/ci-debug-trace.git

Where the "token" password is ephemeral token, it should be revoked after a build is complete.

MountainAsh
  • 300
  • 3
  • 10
-1

Customising the URL is not needed. Just use a git configuration for gitlab tokens such as

git config --global gitlab.accesstoken {TOKEN_VALUE}

extended description here

Bizmate
  • 1,718
  • 1
  • 14
  • 18
  • This did not work for me and I also couldn't find documentation anywhere on a config option with this name – mark.monteiro Nov 03 '20 at 00:02
  • Have you read the article in the link? This variable is what gitlab will take from your git client to authenticate and you need a personal access token. – Bizmate Nov 03 '20 at 01:00
  • I did read the linked article. `gitlab.accesstoken` does not do anything and there is no documentation anywhere on GitLab referencing it – mark.monteiro Nov 03 '20 at 15:40