使用GitHub的发布功能,可以提供一个链接来下载已发布软件的特定版本。然而,每次发布时,gh-page也需要更新。

有没有一种方法可以获取到软件最新版本的特定文件的链接?

例如,这将是一个静态链接:

https://github.com/USER/PROJECT/releases/download/v0.0.0/package.zip

我想要的是:

https://github.com/USER/PROJECT/releases/download/latest/package.zip

注意:这个问题和 GitHub最新发布 这个问题特别要求访问文件, 而不是GitHub最新发布的页面


当前回答

晚了几年,但我只是实现了一个简单的重定向到支持https://github.com/USER/PROJECT/releases/latest/download/package.zip。这应该重定向到最新的带标签的package.zip发行资产。希望它很方便!

其他回答

如果你想在alpine中使用,请遵循以下步骤:

 apk add curl ca-certificates wget
wget -q $(curl -s https://api.github.com/repos/<USER>/<REPOSITORY>/releases/latest | grep browser_download_url | grep "$ARCH" | cut -d '"' -f 4)

wget中的-q标志是安静模式。如果您想查看输出,那么使用不带-q的参数。

使用curl和jq从命令行检索最新版本的第一个文件:

curl -s https://api.github.com/repos/porjo/staticserve/releases/latest | \
  jq --raw-output '.assets[0] | .browser_download_url'

链接到版本帮助页面确实提到了“最新版本”按钮,但这并没有给你一个下载链接。

https://github.com/reactiveui/ReactiveUI/releases/latest

为此,你需要首先获得最新的标签(如“GitHub URL For最新发布的下载文件?”所述):

latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)

curl -L https://github.com/reactiveui/ReactiveUI/releases/download/$latestTag/ReactiveUI-$latestTag.zip

如前所述,jq对于这个和其他REST api很有用。

Tl;dr -详情如下

假设你想要macOS版本:

URL=$( curl -s "https://api.github.com/repos/atom/atom/releases/latest" \
   | jq -r '.assets[] | select(.name=="atom-mac.zip") | .browser_download_url' )
curl -LO "$URL"

原子释放的解决方案

注意,每个repo都可以有不同的方式提供所需的工件,因此我将演示一个表现良好的工件,比如atom。

发布资产的名称

curl -s "https://api.github.com/repos/atom/atom/releases/latest" \
    | jq -r '.assets[] | .name'

atom-1.15.0-delta.nupkg
atom-1.15.0-full.nupkg
atom-amd64.deb
...

获取所需资产的下载URL

下面原子-mac是我想要的资产通过jq的选择(.name=="原子-mac.zip")

curl -s "https://api.github.com/repos/atom/atom/releases/latest" \
    | jq -r '.assets[] | select(.name=="atom-mac.zip") | .browser_download_url'

https://github.com/atom/atom/releases/download/v1.15.0/atom-mac.zip

下载工件

curl -LO "https://github.com/atom/atom/releases/download/v1.15.0/atom-mac.zip"

金桥操场

Jq语法可能很困难。这里有一个实验上面jq的操场: https://jqplay.org/s/h6_LfoEHLZ

安全

如果可能的话,您应该采取措施确保通过sha256sum和gpg下载的工件的有效性。

这是针对Linux的。

我看到了上面被接受的答案

A few years late, but I just implemented a simple redirect to support https://github.com/USER/PROJECT/releases/latest/download/package.zip. That should redirected to the latest tagged package.zip release asset. Hope it's handy!

但有评论指出,它不支持版本化文件名。

在搜索了一会儿之后,我编写了一个适用于版本化文件名的一行调用。它使用curl获取最新的文件版本,然后使用添加的重定向支持来下载最新版本的文件。

wget $'https://github.com/<UMBRELLA PROJECT>/<REPO NAME>/releases/latest/download/<FILE NAME START>-'$(curl -s https://github.com/<UMBRELLA PROJECT>/<REPO NAME>/releases/latest | grep -o -P '(?<=releases/tag/).*(?=\">)')$'<FILE NAME END>'

So it targets a file that's named like <REPO NAME>-linux64_arm-<VERSION NUMBER>.tar.gz that's on the webpage https://github.com/<UMBRELLA PROJECT>/<REPO NAME>/releases/latest after it redirects. It does this by looking for the <VERSION NUMBER> between releases/tag/ and the "> in the text that's returned from the curl call. So to be really explicit, <FILE NAME START> is the <REPO NAME>-linux64_arm- and <FILE NAME END> is the .tar.gz in the above example. Get the START and END bits by looking at what the https://github.com/<UMBRELLA PROJECT>/<REPO NAME>/releases/latest uses as its file naming scheme.

我通过模仿其他人如何使用grep和curl来创建这个,现在才学会所有这些,所以如果它做了一些我甚至无法理解的真正淘气的事情,请告诉我!我还说<UMBRELLA PROJECT>,但用户名应该能够去那里就好了。大声喊出https://stackoverflow.com/a/13245961/2403531的grep调用,https://unix.stackexchange.com/a/10264的$字符串$连接。