29

I want to use curl to download my private repo in GitLab. I know I can use the Gitlab API, but for some reason, It doesn't work.

Is this possible? When I try to do it this way, it always returns the login page.

Ethan
  • 4,295
  • 4
  • 25
  • 44
user3785137
  • 291
  • 1
  • 3
  • 3

5 Answers5

42

This is possible, just follow these steps:

  1. First, you have to create a "Personal Access Token":

    1. Go to Your Profile > Settings > Access Tokens.

    2. Enter a name for your "Personal Access Token".

    3. Check "api Access the authenticated user's API"

      Personal Access Tokens

    4. Click "Create personal access token"

    5. The page will reload and save your new token.

    6. Make sure you save the token somewhere safe, you won't be able to view it again.

      New "Personal Access Token"

  2. Now that you have your "Personal Access Token", you need to get your project id to use the API:

    1. Go to https://gitlab.com/api/v4/projects?private_token=XXXXXXXXXXXXXXXXXXXX (replace the Xs with your new token)

    2. Get your project's id from the json.

      Project id

    (alternatively you can just copy Project ID from its web page)

  3. Now you can call:

     wget -O your_project.tar.gz https://gitlab.com/api/v4/projects/0000000/repository/archive?private_token=XXXXXXXXXXXXXXXXXXXX
    

And that'll download your project as a .tar.gz file.

Edit: you can get tarball for specific commit/tag by adding &sha=... parameter to URL, like: https://gitlab.com/api/v4/projects/24470128/repository/archive?private_token=XXXXXXXXXXXXX&sha=606e81c69eff27eccbbc59a0546a9439780dff55

Amin Ya
  • 1,515
  • 1
  • 19
  • 30
pdeschen
  • 1,369
  • 15
  • 17
  • 2
    api v3 is no longer supported :( – OriolJ Jun 20 '18 at 11:32
  • 5
    Just replace v3 with v4 ;) – Tristan CHARBONNIER Jul 19 '18 at 13:28
  • 1
    Any idea how to get the tarball for a specific tag? – Edwin Diaz Mar 11 '19 at 21:54
  • The final URL will return a file with a complicated filename: `myrepo-master-355be558eac20b8747727e82377522fad73b5f6e.zip`. Is it possible to force a name for the archive? Like "archive.zip". I need that because I cannot specify the filename in composer repository. – Ninj May 02 '19 at 14:53
  • Well, `-O your_project.tar.gz` should get you the right name. Another option would be to use `curl 'https://gitlab.com/api/v3/projects/0000000/repository/archive?private_token=XXXXXXXXXXXXXXXXXXXX' > your_project.tar.gz` – pdeschen May 02 '19 at 19:11
  • I I have project on gitlab and I followed steps to create token + find out project ID. Mine repo is Internal one. Post that the repo download works fine on web browsers but wget or curl download empty tar.gz files. – jkumar_99 Sep 02 '19 at 09:51
  • Different compressed formats can be chosen by appending the correct format after `archive`. e.g. `archive.zip` will download as a zip file. See https://docs.gitlab.com/ee/api/repositories.html#get-file-archive for all supported formats. – David Baucum Oct 16 '20 at 20:41
13

You can use the private token that is yours (found in "profile settings") to access any resource. Just browse to the repository file you want to download, copy the "raw" file link and append ?private_token=...

Example:

curl https://git.local/user1/myrepo/raw/master/myfile.txt?private_token=ahgiretherghaeoi
Amir Katz
  • 643
  • 2
  • 10
  • 23
Michael Wyraz
  • 3,638
  • 1
  • 27
  • 25
6

You can, but you need to authenticate yourself (as in "Gitlab API: How to generate the private token")

curl http://gitlab.server/api/v3/session --data 'login=myUser&password=myPass'

Then with the private token:

curl --header "PRIVATE-TOKEN: QVy1PB7sTxfy4pqfZM1U" "http://example.com/api/v3/projects"

Or, in your case, get the repository files:

GET /projects/:id/repository/files

Or, download directly one file.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Is there a way I can get pullic raw url than downloadable url? For example, the public url for a image file which can be viewed in browser. Is there any way I can generate public url for private repo using the API? – Khurshid Alam Jan 22 '15 at 06:22
  • @KhurshidAlam you mean a public url accessible by everybody, even though it comes from a private repo? – VonC Jan 22 '15 at 06:31
  • Yes. But only with some token. Just like bitbucket or github. For example bitbucket uses a token attached to raw url of the file so that it ca be viewed publicly. `https://bytebucket.org/$username/$repo/raw/$sha/$filename.jpg?token=$sometoken` – Khurshid Alam Jan 22 '15 at 07:03
3

Provided you have your own "Personal Access Token" (as described in other answers) you can download an archive of your repository's branch by using the curl command:

curl -k --header "PRIVATE-TOKEN: xxxx" https://gitlab.xxxxx/api/v4/projects/<projectID>/repository/archive?sha=630bc911c1c20283d3980dcb95fd5cb75479bb9c -o myFilename.tar.gz

ProjectID is displayed on the repo's main page. You can obtain the SHA value from the webUI after selecting the branch you want from the pull-down and copying the value on the right for the SHA. See screenshot below:

enter image description here

The other way to do this is via wget like this:

wget --no-check-certificate -O myFilename.zip --header=PRIVATE-TOKEN:xxxx "https://gitlab.xxxx/api/v4/projects/<projectID>/repository/archive.zip?sha=630bc911c1c20283d3980dcb95fd5cb75479bb9c"

I hope that helps.

Frak
  • 832
  • 1
  • 11
  • 32
  • What is `gitlab.xxxxx`? I assume `. xxxxx` is not a valid top-level domain... – Klesun Apr 29 '21 at 10:53
  • `gitlab.xxxxx` is a generic representation of a URL that belongs to a private entity that is self-hosting its Gitlab instance. For example gitlab.cnn.com or gitlab.facebook.com or gitlab.mycompany.com etc. – Frak Apr 30 '21 at 03:29
  • Oh, thanks. In our company private repos are just like https://gitlab.com/parsiqio/portal/monitoring, but guess the solution is applicable for self-hosting sub-domains as well... Though they do not necessary start with `gitlab.` to be meticulous =P – Klesun Apr 30 '21 at 13:06
2

If you need to do this in a CI run and your private repo is on the same server, you should be able to use git submodules to clone other repos at the same time. Using the ${CI_JOB_TOKEN} is another option since GitLab 8.12.

Waddles
  • 197
  • 1
  • 5