Search code examples
gitlabgitlab-cigitlab-api

Download a single file from GitLab artifacts archive


I'm running GitLab CE 11.5.0, and one of my jobs produces an artifact from which I'd like to download a single file.

Downloading the complete artifacts archive as zip file works as described here:

$ export TOKEN="12345678"
$ export GITLAB_HOST="gitlab.example.com"
$ export PROJECT_ID="foo"
$ export JOB_NAME="bundle"
$ curl --header "PRIVATE-TOKEN: $TOKEN" \
  "https://$GITLAB_HOST/api/v4/projects/$PROJECT_ID/jobs/artifacts/master/download?job=$JOB_NAME" -o artifacts.zip

The archive contents look like this:

$ unzip -l artifacts.zip
Archive:  artifacts.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
   262107  02-18-2019 16:17   build/ui-bundle.zip
---------                     -------
   262107                     1 file

Now I'd like to download only a single file from that archive, as documented here.

$ export FILE_PATH="build/ui-bundle.zip"
$ curl --header "PRIVATE-TOKEN: $TOKEN" \
  "https://$GITLAB_HOST/api/v4/projects/$PROJECT_ID/jobs/artifacts/master/raw/$FILE_PATH?job=$JOB_NAME"

But it seems that Gitlab cannot find the requested file:

{"error":"404 Not Found"}

I realize that the artifacts archive is a zip file containing just a single embedded zip file, but I'd assume that shouldn't make a difference.

Can anyone help?


Solution

  • The gitlab's documentation should be improved. You need to urlencode the artifact path and the project id:

    $ export TOKEN="12345678"
    $ export GITLAB_HOST="gitlab.example.com"
    $ export PROJECT_ID="foo"
    $ export URLENCODED_PROJECT_ID="foo"
    $ export JOB_NAME="bundle"
    $ export FILE_PATH="build/ui-bundle.zip"
    $ export URLENCODED_FILE_PATH="build%2Fui-bundle.zip"
    $ curl --header "PRIVATE-TOKEN: $TOKEN"\
      "https://$GITLAB_HOST/api/v4/projects/$URLENCODED_PROJECT_ID/jobs/artifacts/master/raw/$URLENCODED_FILE_PATH?job=$JOB_NAME"
    

    This answer has a bash implementation for urlencode function.