GitLab CI get last artifact

只愿长相守 提交于 2021-01-01 06:50:25

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/56233243/gitlab-ci-get-last-artifact

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!