Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab: How can I programatically download the artifacts issued at end of CI pipeline?

In Gitlab, how can I programatically download the artifacts issued at end of a CI pipeline?

It is easy to download it via the UI but how can I get it through API? In other words, is it possible to access it via a token or something similar?

like image 480
John Avatar asked Feb 03 '26 00:02

John


2 Answers

It is possible through the API as in https://docs.gitlab.com/ee/api/jobs.html#get-job-artifacts

GET /projects/:id/jobs/:job_id/artifacts

Example requests:

  • Using the PRIVATE-TOKEN header:

     curl --location --header "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" "https://gitlab.example.com/api/v4/projects/1/jobs/8/artifacts"
    
  • Using the JOB-TOKEN header (only inside .gitlab-ci.yml):

     curl --location --header "JOB-TOKEN: $CI_JOB_TOKEN" "https://gitlab.example.com/api/v4/projects/1/jobs/8/artifacts"
    
  • Using the job_token parameter (only inside .gitlab-ci.yml):

     curl --location --form "job-token=$CI_JOB_TOKEN" "https://gitlab.example.com/api/v4/projects/1/jobs/8/artifacts"
    
like image 164
djuarezg Avatar answered Feb 06 '26 13:02

djuarezg


This works for me:

#!/bin/bash

GITLAB_URL="https://gitlab.example.com"
GITLAB_ARTIFACT_TOKEN="<token>"
group="<group>"
project="<project>"
branch="<branch>"
job="<job>"

outZipFile="$project.zip"
outHeadersFile="$outZipFile.httpheaders"

etagArgs=()
# The following is unfortunately not yet supported by GitLab; the returned Etag never changes:
#if [[ -f "$outHeadersFile" ]] && [[ -f "$outZipFile" ]]; then
#  etag=$(grep etag < "$outHeadersFile" | cut -f2 -d' ')
#  if [[ -n "$etag" ]]; then
#    etagArgs=("--header" "If-None-Match: $etag")
#    echo "using etag: $etag"
#  fi
#fi

response=$(curl "$GITLAB_URL/api/v4/projects/${group}%2F${project}/jobs/artifacts/$branch/download?job=$job" \
  --silent \
  -w "%{http_code}\n" \
  -D "$outHeadersFile" \
  -o "$outZipFile.tmp" \
  --header "PRIVATE-TOKEN: $GITLAB_ARTIFACT_TOKEN" \
  "${etagArgs[@]}")

if [[ "$response" == 4* ]] || [[ "$response" == 5* ]]; then
  echo "ERROR - Http status: $response"
  rm "$outZipFile.tmp"
  exit 1
elif [[ "$response" == 304 ]]; then
  echo "$project is up-to-date"
else
  echo "update $outZipFile"
  mv "$outZipFile.tmp" "$outZipFile"
fi
like image 26
Manuel Schmitzberger Avatar answered Feb 06 '26 13:02

Manuel Schmitzberger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!