Good day, was wondering how I’d go about doing this with the API? I have my pipelines creating artifacts but each successive build has an incrementing ID. For example:
https://gitlab.example/api/v4/projects/1/jobs/artifacts/main/raw/files/project-manual-10145.pdf?job=Docs
I want to grab the latest project-manual-####.pdf from another projects builder but how to handle the unknown pipeline ID. I want the latest one so would something like this work? I tried it and get a 404 File Not Found error.
https://gitlab.example/api/v4/projects/1/jobs/artifacts/main/raw/files/project-manual-?latest.pdf?job=Docs
Appreciate any tips / help, thank you
Hello @Calicoder!
I understand you’re trying to fetch the latest artifact file without knowing the pipeline ID. You’re on the right track, but there’s a bit more to it. Let’s break it down:
-
Get the latest pipeline for a specific branch: You can use the GitLab Pipelines API to fetch the latest pipeline for a specific branch. Here’s an example API call:
GET /projects/:id/pipelines?ref=main&order_by=id&sort=desc
This will return a list of pipelines for the main
branch, ordered by ID in descending order. The first pipeline in the response will be the latest one. You can find more details in the Pipelines API documentation.
-
Get the latest job in the pipeline: Once you have the pipeline ID, you can use the Jobs API to fetch the jobs for that pipeline. Here’s an example API call:
GET /projects/:id/pipelines/:pipeline_id/jobs?scope[]=success&order_by=id&sort=desc
This will return a list of successful jobs for the given pipeline, ordered by ID in descending order. The first job in the response will be the latest one. You can find more details in the Jobs API documentation.
-
Get the job artifacts: Finally, once you have the job ID, you can use the Job Artifacts API to fetch the artifacts for that job. Here’s an example API call:
GET /projects/:id/jobs/:job_id/artifacts
This will return the artifacts for the given job. You can find more details in the Job Artifacts API documentation.
By chaining these API calls together, you can fetch the latest artifact file without knowing the pipeline ID. I hope this helps! Let me know if you have any other questions.
Happy coding!
3 Likes
That looks great, let me try it here and see how I do. Much appreciated!
1 Like
Thanks @gitlab-greg got the setup to work from. Just wondering in that last step if it’s possible to download a specific file amongst many or is it a download all as a zip option only?
1 Like
@Calicoder, in the documentation that Greg shared there’s an endpoint to download a single artifact.
1 Like