user2716281
user2716281

Reputation: 185

How do I download a release asset from GitLab via curl

Im trying to create a bash script to download a private release from GitLab using curl but it keeps getting redirected to the signin page.

How can I do this?

I have tried the following with no luck:

curl --header "PRIVATE-TOKEN: XXXXXXXXX" "https://gitlab.com/mrhid6/test/-/archive/v0.0.5/test-v0.0.5.zip"

Upvotes: 7

Views: 12364

Answers (4)

According to the bug (now at https://gitlab.com/gitlab-org/gitlab/-/issues/28978) and to my tests, this is finally possible using the Private-Token header

curl -SsfLO \
    -H 'Private-Token: <PERSONAL_ACCESS_TOKEN>' \
    https://gitlab.com/<USER>/<REPO>/-/archive/<RELEASE_NAME>/<FILE_NAME>

If using Generic Packages for your release assets (I recommend this), then use

curl -SsfLO \
    -H 'Private-Token: <PERSONAL_ACCESS_TOKEN>' \
    https://gitlab.com/api/v4/projects/<URL_ENCODED_USER_SLASH_REPOSITORY_NAME>/packages/generic/release/<RELEASE_NAME>/<FILE_NAME>

Upvotes: 0

Honza Vojtěch
Honza Vojtěch

Reputation: 923

As Win32Sector says, it is a bug, but the issue is open for quite a long time and it is constantly pushed to the next milestone...

As a workaround, you can use Package Registry as a data store and then reference the package files from the Releases page, so that user does not recognize anything:

  1. Enable Package registry - tick Packages in Project -> Settings -> General -> Visibility, project features, permissions.

  2. Upload a package via Package API:

 curl --header "PRIVATE-TOKEN: ####" --upload-file /local/file/asset-file.txt https://<your.gitlab.instance>/api/v4/projects/<PROJECT>/packages/generic/<PACKAGE_NAME>/<VERSION>/asset-file.txt
  1. Add a link to the Releases page using Releases API:
curl --request POST --header "PRIVATE-TOKEN: ####" --data name="My asset name" --data link_type="package" --data url="https://<your.gitlab.instance>/api/v4/projects/<PROJECT>/packages/generic/<PACKAGE-NAME>/<VERSION>/asset-file.txt" "https://<your.gitlab.instance>/projects/<PROJECT>/releases/<VERSION>/assets/links"
  1. Then download your asset either by clicking the link in GitLab (the user is already logged in) or using access token and Packages API:
curl --header "PRIVATE-TOKEN: ####" "https://<your.gitlab.instance>/api/v4/projects/<PROJECT>/packages/generic/<PACKAGE-NAME>/<VERSION>/asset-file.txt"

Upvotes: 6

Win32Sector
Win32Sector

Reputation: 73

Gitlab said it is bug =(

https://gitlab.com/gitlab-org/gitlab-ce/issues/62307

I want to download assets too.

Upvotes: 3

djuarezg
djuarezg

Reputation: 2541

You cannot use the Private token to access the repository zip file like that.

Check https://docs.gitlab.com/ee/api/repositories.html#get-file-archive, you should be using this:

curl --header "PRIVATE-TOKEN: <your_access_token>" https://gitlab.com/api/v4/projects/mrhid6%2Ftest/repository/archive.zip

Upvotes: -2

Related Questions