Download gitlab ci jobs artifacts using Python

Hi All,

I’m using the Gitlab Python API to get information from my projects pipelines and jobs. I want to download all the artifacts for the last pipeline of a projects, but I’m getting some issues with the method “job.artifacts”. It returns me an type error “TypeError: ‘list’ object is not callable”

This is what I’m doing:

image

Does anyone have an idea of why the method " job.artifacts(streamed=True, action=f.write)" is not working?

Thanks a lot in advance!
Veronica

3 Likes

Yeah…I’m a bit late to the party, but perhaps this will help someone. My first job saves the artifacts, so I wrote the following to download and unzip.

if last_pipeline:
     pipeline_job = last_pipeline.jobs.list()[0]
     job = self.project.jobs.get(pipeline_job.id, lazy=True)
     file_name = '__artifacts.zip'
     with open(file_name, "wb") as f:
         job.artifacts(streamed=True, action=f.write)
     zip = zipfile.ZipFile(file_name)
     zip.extractall()
5 Likes

This one definitely helped me! Encountered the exact same error and this answer resolved it. Someone should update the python-gitlab API doc with this answer concerning fetching of artifacts from a job.

Ditto. Extremely helpful. Thank you.

This is how I used it:

gl = gitlab.Gitlab('https://gitlab.mydomain.com', private_token=token)
project = gl.projects.get("mynamespace/some_server")
pipelines = project.pipelines.list(get_all=True) # FIXME check if we need get_all=True

for pipeline in pipelines:
    #print(pipeline)
    if pipeline.sha == someGitHash:
        #print(pipeline)
        jobs = pipeline.jobs.list()
        for job in jobs:
            #print(job)
            if job.name == "build linux 64":
                print(job)
                print("Now downloading the artifact")

                # Job methods (play, cancel, and so on) are not available on ProjectPipelineJob objects. To use these methods create a ProjectJob object:
                newjob = project.jobs.get(job.id, lazy=True)
                newjob.retry()


                zipfn = "___artifacts.zip"
                with open(zipfn, "wb") as f:
                    newjob.artifacts(streamed=True, action=f.write)
                zip = zipfile.ZipFile(file_name)

The same code is giving me the following error

return self._store[key.lower()][1]

KeyError: ‘content-type’

The error seems to be coming from python-gitlab/client.py at 99d580ab9c56933c82d975e24170c3a9b27de423 · python-gitlab/python-gitlab · GitHub

if (
            result.headers["Content-Type"] == "application/json"
            and not streamed
            and not raw
        ):

I tried to download the artifacts via curl and I could see that the response header does not include “Content-Type”. So it makes sense that it fails. but any thoughts on how to fix?