GitLab CI 获取最后一个工件 [英] GitLab CI get last artifact

查看:32
本文介绍了GitLab CI 获取最后一个工件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 curl 获取最新的构建工件.这是我尝试过的.

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

首先,获取最后一个管道id:

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

接下来,根据之前刚刚获取的 pipeline id 获取 job id:

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'

最后,根据作业 id 获取工件到 build.zip:

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

上述这些步骤确实有效,但我必须点击三个端点(并为每个步骤处理 JSON 响应).

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

我还阅读了 GitLab 的文档,有一个 single 端点可用于此.所以我也尝试了这个:

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?

推荐答案

脚本工件下载

两种方法.第二个是您的用例中的解决方案.

Scripting artifacts download

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

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

注意:我想你不能在这里使用你的个人令牌,它保留给下面的 API 使用.

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

Gitlab 文档:下载最新的工件

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"

注意1:-l 告诉 curl 遵循重定向,因为 gitlab 会将您移动到其 google 存储位置.
注意 2:如果您的项目是 gitlab 中的一部分或组或子组,则 %2F 值是斜杠的 url 编码替换.

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 文档:下载最新的工件

这篇关于GitLab CI 获取最后一个工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆