Using this request it's possible to download the latest artifacts created in the Gitlab CI on a specific branch. For example:
curl "https://mygitlab.com/api/v4/projects/MYPROJECTNAME/jobs/artifacts/MYBRANCH/download" \
--data-urlencode "job=build"
How do I do the same thing using the Gitlab python API? This page was all the documentation I could find, but it doesn't have a way of finding the latest job or artifact.
You can download the latest GitLab artifact from a job using the requests library or python-gitlab. You'll need to setup the following environment variables:
GITLAB_TOKEN: If you have a paid version of GitLab, you can use the built-in CI_PROJECT_TOKEN and use PROJECT-TOKEN instead of PRIVATE-TOKEN in the request header. Otherwise, add this as a masked variable.
CI_PROJECT_ID: If you're running from a pipeline, you get this for free. Otherwise, you can find the ID under the title in the project home page in GitLab.
To download a specific file from the latest job in a branch:
import gitlab
project_id = os.getenv('CI_PROJECT_ID') # built-in pipeline variable
gitlab_token = os.getenv('GITLAB_TOKEN') # store in masked CI/CD variable
branch_name = 'master'
job_name = 'system-tests'
gl = gitlab.Gitlab('https://gitlab.example.com', private_token=gitlab_token)
project = gl.projects.get(project_id)
raw_data = project.artifact(ref_name=branch_name, artifact_path='path/to/filename', job=job_name)
The API docs show some examples for writing the streamed data to file: https://python-gitlab.readthedocs.io/en/stable/gl_objects/pipelines_and_jobs.html#streaming-example
To download the artifact as a zip from a job, it will be simpler to just use the requests library.
The code below downloads all artifacts of the job as a zip file. To download the contents of a single artifact file instead of the whole zip, simply replace download in the URL below with raw/path/to/filename
import requests
project_id = os.getenv('CI_PROJECT_ID') # built-in pipeline variable
gitlab_token = os.getenv('GITLAB_TOKEN') # stored in masked CI/CD variable
branch_name = 'master'
job_name = 'system-tests'
url = f'https://gitlab.example.com/api/v4/projects/{project_id}/jobs/artifacts/{branch_name}/download'
headers = {'PRIVATE-TOKEN': gitlab_token}
params = {'job': job_name}
response = requests.get(url=url, headers=headers, params=params)
# if artifact is a text file:
print(response.text)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With