通过复制粘贴链接URL来卷曲,是否能够下载Github Actions工件? [英] Being able to download a Github Actions artifact by copy-past the link URL to curl?

查看:55
本文介绍了通过复制粘贴链接URL来卷曲,是否能够下载Github Actions工件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在做一个概念验证,研究Github Actions为遗留系统的给定提交生成工件,然后我们需要对其进行进一步的内部处理,因此我正在研究如何相对简单地实现这一目标.现在来证明这是可行的.我们的拉链包装很好.

通过在操作"中的作业页面上的工件"上单击鼠标右键来标识的工件的示例URL:

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

问题是,我如何用最少的编码来自 https://github.com/tandeday/actions-artifacts/suites/1767549169/artifacts/33720037 https://api.github.com/repos/tandeday/actions-artifacts/actions/artifacts/33720037/zip 吗?

解决方案

如果您可以公开采购您的仓库(如果没有,请在此答案中向下滚动),如果您这样做,则无需进行身份验证一个工作流程,该工作流程会在每次提交时创建一个发行版,然后构建您的工件,然后将该工件上传到版本中.诀窍是要巧妙使用发布URL中使用的发布标签名称.此处.我已经对此进行了稍微修改,以适应您的示例存储库的情况,而是使用标记名称中提交哈希的前8个字符来生成可预测但唯一的URL:

 在:推:#在每个推送的提交上运行名称:上传发布资产职位:建造:名称:上传发布资产运行:ubuntu-latest脚步:-名称:结帐代码用途:actions/checkout @ v2#为了简单起见,我们将标记和发布命名为#提交SHA的前8个字符:-名称:获取简短的SHAid:short_sha运行:echo" :: set-output name = sha8 :: $(echo $ {GITHUB_SHA} | cut -c1-8)"-名称:构建项目#这实际上将构建您的项目,使用zip作为示例日期工件运行:日期>date.txtzip -rT app.zip date.txt-名称:创建发布id:create_release用途:actions/create-release @ v1环境:GITHUB_TOKEN:$ {{secrets.GITHUB_TOKEN}}和:#使用第二步的简短SHA标记和发布名称.tag_name:$ {{steps.short_sha.outputs.sha8}}release_name:$ {{steps.short_sha.outputs.sha8}}草稿:错误预发布:false#现在,我们将第一步中生成的工件上传到#我们在第三步中创建的版本-名称:上传发布资产id:上载发布资产用途:actions/upload-release-asset @ v1环境:GITHUB_TOKEN:$ {{secrets.GITHUB_TOKEN}}和:upload_url:$ {{steps.create_release.outputs.upload_url}}#这是从上面的CREATE RELEASE步骤中提取的,引用其ID获取其输出对象,其中包括一个"upload_url".有关更多信息,请参见此博客文章:https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-stepsasset_path:./app.zipasset_name:app.zipasset_content_type:应用程序/邮政编码 

然后,最后,您的资产将通过以下格式的URL提供: github.com/orgname/reponame/releases/download/$ {short_sha}/app.zip ,其中 short_sha 是您提交的前8个字符.由于它具有这种已知格式,因此如果内部用户可以访问存储库,则很容易在内部用户端生成URL(从回购中拉出,以SHA为首,进行一些字符串插值,将URL打印到控制台).

测试回购工作流程

测试发布资产

如果您的回购必须在组织内保持私密:

有人用一些shell脚本编写了 nice Gist 来从私有版本中获取发行版回购-您将需要一个访问令牌.

如果您不想污染回购的发行版:

您可以修改上述工作流程的 tag_name release_name 以包含 intermediate 前缀,因此很明显这不是实际的发行版.但是,如果采用这种方式,您的URL格式将有所不同-/download/之后的位将是您的新标签名称.

We are doing a proof of concept of looking into Github Actions for producing an artifact for a given commit for a legacy system, which we then need to process further in-house so I am looking into how we can do that relatively simply for now to demonstrate that this can work. We are fine with the zip wrapping.

Sample URL for such an artifact as identified by right-clicking on the Artifact in the job page in Actions: https://github.com/tandeday/actions-artifacts/suites/1767549169/artifacts/33720037

I understand that there is a full API for this, and I have successfully used this by hand to localize and download the artifact using a line similar to:

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

I would like to be able to avoid creating an API client for this proof-of-concept, but instead allow the user to just pass in the link from the web page, but I have been unable to locate a simple way to do so.

So question is, how do I with a minimum of coding come from https://github.com/tandeday/actions-artifacts/suites/1767549169/artifacts/33720037 to https://api.github.com/repos/tandeday/actions-artifacts/actions/artifacts/33720037/zip ?

解决方案

If you're ok with open-sourcing your repo (scroll further down in this answer if not), you can do this without needing to authenticate if you make a workflow that creates a release upon each commit, then builds your artifact, then uploads that artifact to the release. The trick is to be clever with your release tag's name, which is used in the release URL. A sample workflow is described here. I've modified this slightly to match your sample repo's scenario, and I instead use the first 8 characters of the commit hash in the tag name to generate a predictable but unique 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

Then, finally, your asset will be available at a URL with the pattern: github.com/orgname/reponame/releases/download/${short_sha}/app.zip, where short_sha is the first 8 characters of your commit. Since it has this known format, it's easy to generate the URL on the in-house user's side if they have access to the repository (pull from repo, get SHA at head, do some string interpolation, print URL to console).

Test repo workflow

Test release asset

In case your repo must stay private within your org:

Someone wrote up a nice Gist with some shell scripts to grab a release from a private repo - you'll need an access token.

If you don't want to pollute the releases of your repo:

You could modify the above workflow's tag_name and release_name to include an intermediate prefix so it's clear that it's not an actual release. Your URL format will be different if you take this route, though - the bit after /download/ will be your new tag name.

这篇关于通过复制粘贴链接URL来卷曲,是否能够下载Github Actions工件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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