Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab CI get last artifact

I'm trying to get the latest build artifact using curl. Here's what I've tried.

First, get last pipeline id:

curl -v -H "Content-Type: application/json" -H "PRIVATE-TOKEN: <my-token-here>" https://<project>/api/v4/projects/<project>/pipelines?per_page=1&page=1

Next, get job id based on pipeline id just obtained before:

curl -sS --header "PRIVATE-TOKEN: <my-token-here>" "https://[redacted,host]/api/v4/projects/[redacted,project]/pipelines/<pipeline-id>/jobs" | jq '.[] | select(.name == "build-assets" and .status == "success" and .artifacts_file != null) | .id'

Finally, get the artifacts to build.zip based on job id:

curl -sS --header "PRIVATE-TOKEN: <my-token-here>" "https://[redacted,host]/api/v4/projects/[redacted, project]/jobs/<JOB_ID>/artifacts" > build.zip

These steps above do work, but I have to hit three endpoints (and process the JSON response for each step).

I also read in GitLab's documentation, that there's a single endpoint available for this. So I also tried this:

curl -sS --header "PRIVATE-TOKEN: <my-token-here>" "https://<url>/<namespace>/<project>/-/jobs/artifacts/<refs>/download?job=<job_name>"

but this always redirects me to the login page, saying this:

<html><body>You are being <a href="https://<url>/users/sign_in">redirected</a>.</body></html>

Is there any simpler way to do this task? Or how to use the endpoint that is described on the documentation above propeprly?

like image 381
thekucays Avatar asked Dec 04 '22 18:12

thekucays


1 Answers

Scripting artifacts download

Two methods. The second one is the solution in your use case.

From a Public project

curl -sS "https://<url>/<namespace>/<project>/-/jobs/artifacts/<refs>/download?job=<job_name>"

Note : I gess you can't use your personal token here, it's reserved to API usage bellow.

Gitlab doc : downloading the latest artifacts

From a Private project

curl -L --header "PRIVATE-TOKEN: 123456abcdef" "https://gitlab.com/api/v4/projects/awesome-organization%2Fawesome-group%2Fowesome-project/jobs/artifacts/master/download?job=publish-release"

Note1 : the -l tells curl to follow the redirection since gitlab will move you to its google storage location.
Note2 : the %2F value is the url-encoded replacement for the slash if your project is part or a group or subgroup in gitlab.

Gitlab api doc : downloading the latest artifacts

like image 71
Xavier D Avatar answered Dec 21 '22 17:12

Xavier D