300

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.

Arty-chan
  • 2,572
  • 1
  • 19
  • 25
Muky
  • 3,584
  • 3
  • 19
  • 20

18 Answers18

450

This is how you do it:

git clone https://oauth2:[email protected]/vendor/package.git
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Roshan Gautam
  • 4,780
  • 1
  • 13
  • 11
134

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,655
  • 24
  • 29
xuanyuanaosheng
  • 1,684
  • 1
  • 11
  • 9
  • 9
    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
  • 2
    what are the differences for `gitlab-ci-token`, `oauth2` and `x-access-token`? all of these 3 work for me. – Lei Yang Apr 20 '22 at 06:44
56

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

git clone https://username:[email protected]/user/repo.git

Tested against 11.0.0-ee.

Zbyněk Winkler
  • 1,263
  • 1
  • 12
  • 12
48

You can do it like this:

git clone https://gitlab-ci-token:<private token>@git.example.com/myuser/myrepo.git
Tim Hughes
  • 3,144
  • 2
  • 24
  • 24
  • 3
    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
  • 6
    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
32

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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Roger Barreto
  • 2,004
  • 1
  • 17
  • 21
  • 8
    `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
15

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
  • 10,110
  • 16
  • 58
  • 71
Enlai
  • 158
  • 2
  • 4
14

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.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
some bits flipped
  • 2,592
  • 4
  • 27
  • 42
13

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
  • 1,046
  • 10
  • 16
shahar taite
  • 462
  • 6
  • 11
  • 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
12

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
  • 124,992
  • 159
  • 614
  • 958
Slawa
  • 1,141
  • 15
  • 21
10

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
  • 10,850
  • 6
  • 57
  • 88
  • 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

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 will be automatically revoked after a build is complete).

MountainAsh
  • 428
  • 6
  • 12
4

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
  • 124,992
  • 159
  • 614
  • 958
3

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

Laurent
  • 1,048
  • 11
  • 23
3

In my case, I just provided the token instead the password (second input field).

enter image description here

I pushed a local repo for the first time from the command line.

From the scratch, these are the commands I entered (remember to move inside the repo's folder first).

$ git init

$ git status

$ git add .

$ git status

$ git commit -m 'Shinra Tensei.'

$ git push --set-upstream https://gitlab.com/userName/my-repo.git master

Then, the pop-up message you can see in the picture comes up. Provided USERNAME and TOKEN.

carloswm85
  • 1,396
  • 13
  • 23
2

Using PAT (Personal Access Token):

https://pat:<your-token>@gitlab.com/<org>/<proj>
Eduardo Mior
  • 156
  • 2
  • 10
0

you can change your:

user:oauth2
password : <youraccesstoken>

example:

git clone https://oauth2:<token>@hahahehe.com/yourname/yourproject.git
m.myalkin
  • 1,128
  • 1
  • 10
  • 18
0

It may help: In your gitlab-ci file: Add this on before script step

before_script:
  - git config --global credential.helper store
  - echo "https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}" >> ~/.git-credentials
  - chmod 600 ~/.git-credentials

Thanks

Lamine BA
  • 129
  • 1
  • 8
-2

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,835
  • 1
  • 15
  • 19
  • 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