使用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。
另一个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禁止错误。
只需使用下面的网址下载最新版本:
(以boxbilling项目中的url为例):https://api.github.com/repos/boxbilling/boxbilling/releases
以zip格式下载最新版本:
https://api.github.com/repos/boxbilling/boxbilling/zipball
下载最新版本为tarball:
https://api.github.com/repos/boxbilling/boxbilling/tarball
点击其中一个网址立即下载最新版本。当我写这一行时,它目前是:boxbilling-boxbilling-4.20-30-g452ad1c[.zip/.tar.gz]
更新:在我的日志文件中发现了另一个url(参考上面的例子)
https://codeload.github.com/boxbilling/boxbilling/legacy.tar.gz/master
如前所述,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下载的工件的有效性。
在PHP -重定向到最新版本下载。简单地放在你的网络空间
<?php
/**
* Download latest release from github release articats
* License: Public Domain
*/
define('REPO', 'imi-digital/iRobo');
$opts = [
'http' => [
'method' => 'GET',
'header' => [
'User-Agent: PHP'
]
]
];
$context = stream_context_create($opts);
$releases = file_get_contents('https://api.github.com/repos/' . REPO . '/releases', false, $context);
$releases = json_decode($releases);
$url = $releases[0]->assets[0]->browser_download_url;
header('Location: ' . $url);
根据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'])
这种解决方案的好处是您不必指定任何版本或标记号—它只会抓取LATEST。
测试:
我使用以下Github用户和repo进行了测试:
“f1linux” = Github用户
“pi-ap” = 存储库
repo保存到的任意目录名称设置为:
--one-top-level="pi-ap"
直接:
使用Firefox的“Web Developer”工具(右上角的3条),在“Network”部分,我发现https://api.github.com重定向到https://codeload.github.com,所以通过管道curl到tar,我能够获取最新版本的repo并将其保存为可预测的名称,以便对其进行操作:
curl https://codeload.github.com/f1linux/pi-ap/legacy.tar.gz/master | tar xzvf - --one-top-level="pi-ap" --strip-components 1
间接:
在我使用DIRECT URL实现了最新版本的全自动下载后,我将注意力转向了用Github的重定向实现同样的功能:
curl -L https://api.github.com/repos/f1linux/pi-ap/tarball | tar xzvf - --one-top-level="pi-ap" --strip-components 1
首选的方法:
然而,请注意,根据冯的评论,间接是首选的方法
进一步验证:
为了确保我的结果可复制到其他版本的Github reppos,我们成功地为Digital Ocean的doctl api工具包执行了相同的测试(这实际上是整个练习的开始!):
DIRECT和INDIRECT都使用与上面相同的表单,只需更改用户名和repo:
直接:
curl https://codeload.github.com/digitalocean/doctl/legacy.tar.gz/master | tar xzvf - --one-top-level="doctl" --strip-components 1
间接:
curl -L https://api.github.com/repos/digitalocean/doctl/tarball | tar xzvf - --one-top-level="doctl" --strip-components 1
这是针对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的$字符串$连接。
这可以在一行代码中完成,如下所示:
$ 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等。