Download the latest artifacts of failed gitlab pipeline

I want to automatically load test results of my gitlab pipeline (basically one xml file) into my project management application. No direct connection between these two is possible. However the gitlab API offers the following options to download artifacts of a pipeline:

  • all artifacts of the latest successful pipeline run (selected by job name)

GET /projects/:id/jobs/artifacts/:ref_name/download?job=name`

  • all artifacts of a specific job (selected by job id)

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

  • a single artifact file (selected by job id)

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

My current situation is following:

I have test reports which are saved inside the job artifacts when running the pipeline. The artifacts are created on every run of the pipeline independent of its outcome

gitlab-ci.yaml


artifacts:
when: always

The artifact I am trying to download has a dynamic name

./reports/junit/test-results-${CI_JOB_ID}.xml

If I now want to download the latest test results to a different server than the gitlab server, I have to realize that I don’t know the latest job ID, which means:

  • I can’t access the artifact directly because it has a dynamic name
  • I can’t access the artifacts of a specific job
  • I can access the artifacts of the latest job, but only if it was successful

This leaves me with the situation, that i only get to download the latest test results, if nothing went wrong while testing. To put it mildly, this is suboptimal.

Is there some way to download the artifacts from the latest job run (without knowing the job ID), independent of its outcome?

This has been answered in Stack Overflow Answer