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


当前回答

你可以使用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和jq从命令行检索最新版本的第一个文件:

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

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

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

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

根据2018-05-23的GitHub支持,不可能

联系support@github.com 2018-05-23,留言:

你能否确认一下,目前除了干扰API,没有别的办法了?

他们回答说:

谢谢你伸出援手。我们建议使用API来获取最新的版本,因为这种方法是稳定的,有文档记录的,并且不会很快改变: https://developer.github.com/v3/repos/releases/#get-the-latest-release

我也会继续关注:https://github.com/isaacs/github/issues/658

没有任何依赖的Python解决方案

健壮、便携:

#!/usr/bin/env python3

import json
import urllib.request

_json = json.loads(urllib.request.urlopen(urllib.request.Request(
    'https://api.github.com/repos/cirosantilli/linux-kernel-module-cheat/releases/latest',
     headers={'Accept': 'application/vnd.github.v3+json'},
)).read())
asset = _json['assets'][0]
urllib.request.urlretrieve(asset['browser_download_url'], asset['name'])

参见:

在Python中获取HTTP GET的最快方法是什么? 基本的http文件下载和保存到磁盘在python?

还要考虑预发行版

/latest看不到预发布版本,但是很容易做到,因为/releases首先显示最新的版本:

#!/usr/bin/env python3

import json
import urllib.request

_json = json.loads(urllib.request.urlopen(urllib.request.Request(
    'https://api.github.com/repos/cirosantilli/linux-kernel-module-cheat/releases',
     headers={'Accept': 'application/vnd.github.v3+json'},
)).read())
asset = _json[0]['assets'][0]
urllib.request.urlretrieve(asset['browser_download_url'], asset['name'])

我想从README下载版本。Md文件在存储库描述中。在那里,我无法执行JavaScript。

我可以为我所有的存储库在README文件或github页面中添加这样的链接:

https://niccokunzmann.github.io/download_latest/ <用户名> / <库> / <文件> 从存储库下载最新的发布文件。 https://niccokunzmann.github.io/download_latest/ <文件> 这是因为JavaScript引用器已经设置好,要下载的存储库是通过document.referrer确定的。因此,该链接也适用于分叉。

你可以在这里找到源代码,fork或使用我的repo。