使用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最新发布的页面


当前回答

另一个Linux解决方案使用curl和wget从最新版本页面下载单个二进制文件

curl -s -L https://github.com/bosun-monitor/bosun/releases/latest | egrep -o '/bosun-monitor/bosun/releases/download/[0-9]*/scollector-linux-armv6' | wget --base=http://github.com/ -i - -O scollector

解释:

curl -s -L是无声地下载最新版本的HTML(在执行重定向之后)

鹭-o '…'使用正则表达式找到你想要的文件

wget——base=http://github.com/ -i将管道的相对路径转换为绝对URL

和-O sccollector设置所需的文件名。

如果文件更新,可以添加-N到下载,但S3给出了403禁止错误。

其他回答

如果回购只是使用标签而不是发布——参考jQuery——基于一个URL的解决方案是行不通的。

相反,您必须查询所有标记,对它们进行排序并构造下载URL。我为语言Go和jQuery实现了这样一个解决方案:链接到Github。

也许,这对某些人有帮助。

这可以在一行代码中完成,如下所示:

$ curl -s https://api.github.com/repos/slmingol/gorelease_ex/releases/latest \
    | grep -wo "https.*Linux.*gz" | wget -qi -

这里我们:

拉出GitHub的API端,以获得带有最新标签的发布构件的信息。 解析输出,寻找与模式https.*Linux.*gz匹配的工件。 将URL传递给命令wget -qi -,以便下载

为了进一步揭示这里发生了什么,这里有一个更广泛的API端点的grep:

$ curl -s https://api.github.com/repos/slmingol/gorelease_ex/releases/latest | grep -wo "https.*" | grep gz
https://github.com/slmingol/gorelease_ex/releases/download/0.0.78/gorelease_ex_0.0.78_Darwin_x86_64.tar.gz"
https://github.com/slmingol/gorelease_ex/releases/download/0.0.78/gorelease_ex_0.0.78_Linux_x86_64.tar.gz"

上面你可以看到匹配的url。

进一步提示

您还可以参数化grep参数,以便它“动态地”确定它在哪个平台上运行,并根据该平台替换适当的字符串。

$ curl -s https://api.github.com/repos/slmingol/gorelease_ex/releases/latest \
    | grep -wo "https.*$(uname).*gz" | wget -qi -

这里$(uname)将返回Darwin、Linux等。

你可以使用GitHub发布API做一个ajax请求来获得最新版本的下载URL。它还显示了它的发布时间和下载计数:

function GetLatestReleaseInfo() { $.getJSON("https://api.github.com/repos/ShareX/ShareX/releases/latest").done(function(release) { UpdateDownloadButton(release, ".exe", $(".setup")); UpdateDownloadButton(release, "portable.zip", $(".portable")); }); } function UpdateDownloadButton(release, assetExtension, element) { let asset = release.assets.find(asset => asset.name.endsWith(assetExtension)); let releaseInfo = "Version: " + release.tag_name.substring(1) + "\nFile size: " + (asset.size / 1024 / 1024).toFixed(2) + " MB" + "\nRelease date: " + new Date(asset.updated_at).toLocaleDateString("en-CA") + "\nDownload count: " + asset.download_count.toLocaleString(); element.attr("href", asset.browser_download_url); element.attr("title", releaseInfo); } GetLatestReleaseInfo(); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a class="setup" href="https://github.com/ShareX/ShareX/releases/latest">Setup</a> <a class="portable" href="https://github.com/ShareX/ShareX/releases/latest">Portable</a>

当请求完成时,按钮的URL将自动更改为直接下载URL。

如果你只想使用curl,你可以尝试使用-w '%{url_effective}',它会在重定向链之后打印URL(如果你使用-L调用它,后面跟着curl)。举个例子

curl -sLo /dev/null -w '%{url_effective}' https://github.com/github-tools/github/releases/latest

输出https://github.com/github-tools/github/releases/tag/v3.1.0。

另一个Linux解决方案使用curl和wget从最新版本页面下载单个二进制文件

curl -s -L https://github.com/bosun-monitor/bosun/releases/latest | egrep -o '/bosun-monitor/bosun/releases/download/[0-9]*/scollector-linux-armv6' | wget --base=http://github.com/ -i - -O scollector

解释:

curl -s -L是无声地下载最新版本的HTML(在执行重定向之后)

鹭-o '…'使用正则表达式找到你想要的文件

wget——base=http://github.com/ -i将管道的相对路径转换为绝对URL

和-O sccollector设置所需的文件名。

如果文件更新,可以添加-N到下载,但S3给出了403禁止错误。