GitHub操作构建的最后一个工件的URL

时间:2020-03-21 15:46:20

标签: github-actions

我使用GitHub-action进行构建,它会生成多个工件(使用不同的名称)。

有没有一种方法可以预测上一次成功构建的工件的URL? 在不知道sha1 的情况下,只知道工件和存储库的名称吗?

3 个答案:

答案 0 :(得分:1)

根据comments from staff,目前还没有,尽管随着upload-artifact动作的未来版本可能会改变。

自言自语之后,可以使用GitHub action API来获得此信息: https://developer.github.com/v3/actions/artifacts/

GET /repos/:owner/:repo/actions/runs/:run_id/artifacts

因此,您可以接收JSON答复并遍历“工件”数组以获得相应的“ archive_download_url”。工作流可以这样填写URL:

/ repos / $ {{github.repository}} / actions / runs / $ {{github.run_id}} / artifacts

答案 1 :(得分:0)

我开发了一项服务,该服务将可预测的URL暴露给存储库分支+工作流的最新工件或特定工件。

https://nightly.link/
https://github.com/oprypin/nightly.link

这是作为GitHub应用程序实现的,并且与GitHub的通信已通过身份验证,但是仅下载的用户甚至不需要登录GitHub。


该实现通过3个步骤通过API进行获取:

  • https://api.github.com/repos/:owner/:repo/actions/workflows/someworkflow.yml/runs?per_page=1&branch=master&event=push&status=success
  • https://api.github.com/repos/:owner/:repo/actions/runs/123456789/artifacts?per_page=100
  • https://api.github.com/repos/:owner/:repo/actions/artifacts/87654321/zip

(最后一个将您重定向到临时直接下载URL)

请注意,必须进行身份验证。对于public_repo(或repo,如果适用)的OAuth。对于“操作” /“只读”的GitHub应用。


确实没有更直接的方法可以做到这一点。

一些相关问题是

答案 2 :(得分:0)

我不是 GitHub 和 jq 专家。可能还有更多最佳解决方案。

jq playground linkhttps://jqplay.org/s/Gm0kRcv63C - 测试我的解决方案和其他可能的想法。我删除了一些不相关的字段来缩小示例 JSON 大小(例如:node_id、size_in_bytes、created_at...) 有关以下代码示例中方法的更多详细信息。

####### You can get the max date of your artifacts.
####### Then you need to choose the artifact entry by this date.
#######
####### NOTE: I just pre-formatted the first command "line".
####### 2nd "line" has a very similar, but simplified structure.
####### (at least easy to copy-paste into jq playground)

####### NOTE: ASSUMPTION:
####### First "page" of json response contains the most recent entries
#######   AND includes artifact(s) with that specific name.
#######
####### (if other artifacts flood your API response, you can increase
#######  the page size of it or do iteration on pages as a workaround)

bash$ cat artifact_response.json | \
  jq '
    (
      [
        .artifacts[]
        | select(.name == "my-artifact" and .expired == false)
        | .updated_at
      ]
      | max
    ) as $max_date
    | { $max_date }'

####### output
{ "max_date": "2021-04-29T11:22:20Z" }

另一种方式:

####### Latest ID of non-expired artifacts with a specific name.
####### Probably this is better solution than the above since you
####### can use the "id" instantly in your download url construction:
#######
####### "https://api.github.com/repos/blabla.../actions/artifacts/92394368/zip"
#######
####### ASSUMPTION: higher "id" means higher "update date" in your workflow
####### (there is no post-update of artifacts aka creation and
#######  update dates are identical for an artifact)


cat artifact_response.json | \
jq '[ .artifacts[] | select(.name == "my-artifact" and .expired == false) | .id ] | max'

####### output
92394368

更紧凑的过滤器假设在 API 响应中按日期或 ID 逆序排列:

####### no full command line, just jq filter string
#######
####### no "max" function, only pick the first element by index
#######
'[ .artifacts[] | select(.name == "my-artifact" and .expired == false) | .id ][0]'