是否能够通过将链接 URL 复制粘贴到 curl 来下载 Github Actions 工件?

Tho*_*sen 5 curl github-actions github-actions-artifacts

我们正在做一个概念验证,研究 Github Actions 来为遗留系统的给定提交生成一个工件,然后我们需要在内部进一步处理它,所以我正在研究我们现在如何相对简单地做到这一点证明这是可行的。我们对拉链包装没问题。

通过在操作中的作业页面中右键单击工件来识别此类工件的示例 URL: https://github.com/tandeday/actions-artifacts/suites/1767549169/artifacts/33720037

据我所知,有一个完整的 API,并且我已经成功地手动使用它来本地化和下载工件,使用类似于以下内容的行:

curl -O -J -L -H 'Authorization: token ...'  -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/tandeday/actions-artifacts/actions/artifacts/33720037/zip
Run Code Online (Sandbox Code Playgroud)

我希望能够避免为此概念验证创建 API 客户端,而是允许用户仅从网页传递链接,但我一直无法找到一种简单的方法来做到这一点。

所以问题是,我如何以最少的编码从https://github.com/tandeday/actions-artifacts/suites/1767549169/artifacts/33720037https://api.github.com/repos/tandeday/动作-工件/动作/工件/33720037/zip

jid*_*ula 3

如果您同意开源您的存储库(如果不同意,请在这个答案中进一步向下滚动),如果您创建一个工作流程,在每次提交时创建一个版本,然后构建您的工件,然后上传,则无需进行身份验证即可执行此操作那个工件要发布。诀窍是巧妙地使用发布 URL 中使用的发布标签名称。此处描述了示例工作流程。我对此进行了稍微修改以匹配您的示例存储库的场景,并且我使用标签名称中提交哈希的前 8 个字符来生成可预测但唯一的 URL:

on:
  push:
    # Runs on every pushed commit

name: Upload Release Asset

jobs:
  build:
    name: Upload Release Asset
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

        # For the sake of simplicity, we'll name the tag and the release with
        # the first 8 chars of the commit SHA:
      - name: Get short SHA
        id: short_sha
        run: echo "::set-output name=sha8::$(echo ${GITHUB_SHA} | cut -c1-8)"

      - name: Build project # This would actually build your project, using zip for your example date artifact
        run:  |
          date > date.txt
          zip -rT app.zip date.txt
      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          # Using short SHA from 2nd step for tag and release name.
          tag_name: ${{steps.short_sha.outputs.sha8}}
          release_name: ${{steps.short_sha.outputs.sha8}}
          draft: false
          prerelease: false
        # Now we upload the artifact we generated in the first step to
        # the release we created in the 3nd step
      - name: Upload Release Asset
        id: upload-release-asset 
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing its ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps 
          asset_path: ./app.zip
          asset_name: app.zip
          asset_content_type: application/zip
Run Code Online (Sandbox Code Playgroud)

最后,您的资产将在具有以下模式的 URL 上可用:github.com/orgname/reponame/releases/download/${short_sha}/app.zip,其中short_sha是您提交的前 8 个字符。由于它具有这种已知格式,因此如果内部用户有权访问存储库(从存储库中提取,在头部获取 SHA,进行一些字符串插值,将 URL 打印到控制台),则可以轻松在内部用户端生成 URL。

测试回购工作流程

测试发布资产

如果您的存储库必须在您的组织内保持私有:

有人写了一个很好的 Gist,其中包含一些 shell 脚本来从私人存储库中获取版本 - 您需要一个访问令牌。

如果您不想污染您的存储库的版本:

您可以修改上述工作流程tag_namerelease_name添加intermediate前缀,以便清楚地表明它不是实际版本。不过,如果您采用此路线,您的 URL 格式将会有所不同 - 后面的位/download/将是您的新标签名称。