1

I try to download build artifacts from a CI/CD pipeline by using the GitLab API for Python. I took a look at the documentation and wrote the following example:

import os
import time
import gitlab
import subprocess

gl = gitlab.Gitlab("MyRepo.de", private_token = "A Token")
project = gl.projects.get("MyProject")
pipelines = project.pipelines.list()

# Get the latest job from the latest pipeline
CurrentJob = None
for Pipeline in project.pipelines.list():
    if(Pipeline.status == "success"):
        for Job in Pipeline.jobs.list():
            if(Job.status == "success"):
                CurrentJob = Job

        break

zipfn = "___artifacts.zip"
with open(zipfn, "wb") as f:
    CurrentJob.artifacts(streamed=True, action=f.write)
subprocess.run(["unzip", "-bo", zipfn])
os.unlink(zipfn)

But the program exit with the error 'list' object is not callable in the line CurrentJob.artifacts(streamed=True, action=f.write) and the debugger shows three different files:

enter image description here

But the example uses the same lines of code. What is the correct way to access and download the files? I don´t find any solution in the documentation.

1 Answer 1

3

The distinction here is subtle. Using pipeline.jobs.list doesn't use the same underlying API as project.jobs.list (or .get)

In terms of the Python-Gitlab library, when you do pipeline.jobs.list() you get a list of gitlab.v4.objects.ProjectPipelineJob objects. The .artifacts attribute of this type of object is not a method -- it's an a list containing information about the artifacts, like file type, size file name, etc -- which is what you're seeing in your image.

This is different from a gitlab.v4.objects.ProjectJob object, which is what is shown in the documentation. You obtain this kind of object from project.jobs.get or project.jobs.list.

So, to correct this error you can edit the tail end of your code as follows:

job_id = CurrentJob.id
CurrentJob = project.jobs.get(job_id)

zipfn = "___artifacts.zip"
with open(zipfn, "wb") as f:
    CurrentJob.artifacts(streamed=True, action=f.write)
subprocess.run(["unzip", "-bo", zipfn])
os.unlink(zipfn)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.