下载最新的GitHub版本

问题描述 投票:17回答:7

我想在我的网站上有“下载最新版本”按钮,该按钮代表最新版本的链接(存储在GitHub版本中)。我试图创建名为“latest”的发布标签,但是当我尝试加载新版本时(与标签创建日期混淆,标签交换等),它变得复杂了。手动更新我网站上的下载链接也是一项耗时且严格的任务。我看到唯一的方法 - 将所有下载按钮重定向到一些html,然后重定向到实际的最新版本。

请注意,我的网站托管在GitHub Pages(静态托管),所以我根本无法使用服务器端脚本来生成链接。有任何想法吗?

github tags release
7个回答
15
投票

在您创建第一个版本后,Github现在在项目的发布页面上提供“最新版本”按钮。

在您给出的示例中,此按钮链接到https://github.com/reactiveui/ReactiveUI/releases/latest


10
投票

您不需要任何脚本来生成最新版本的下载链接。只需使用以下格式:

https://github.com/:owner/:repo/zipball/:branch

例子:

https://github.com/webix-hub/tracker/zipball/master
https://github.com/iDoRecall/selection-menu/zipball/gh-pages

如果由于某种原因您想获得最新版本下载的链接,包括其版本号,您可以从get latest release API获取:

GET /repos/:owner/:repo/releases/latest

例:

$.get('https://api.github.com/repos/idorecall/selection-menu/releases/latest', function (data) {
  $('#result').attr('href', data.zipball_url);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="result">Download latest release (.ZIP)</a>

3
投票

February 18th, 2015以来,GitHUb V3 release API有一个get latest release API

GET /repos/:owner/:repo/releases/latest

2
投票

也许您可以使用一些客户端脚本并通过调用GitHub api通过一些JQuery魔法动态生成链接的目标?

Releases API公开了一种到retrieve the list of all the releases from a repository的方法。例如,this link返回所有releases of the ReactiveUI project的Json格式列表。

提取第一个将返回最新版本。

在此有效负载内:

  • html_url属性将保存要构建的url的第一部分(即.https://github.com/{owner}/{repository}/releases/{version})。
  • assets数组将列出可下载的档案。每个asset将具有name属性

构建目标下载URL只需要几个字符串操作。

  • 在html_url的download/段和版本号之间插入releases/关键字
  • 附加要下载的资产名称

生成的url将采用以下格式:https://github.com/{owner}/{repository}/releases/download/{version}/name_of_asset

例如,关于上面链接ReactiveUI链接的Json有效负载,我们有html_url: "https://github.com/reactiveui/ReactiveUI/releases/5.99.0"name: "ReactiveUI.6.0.Preview.1.zip"的一个资产。

因此,下载网址是https://github.com/reactiveui/ReactiveUI/releases/download/5.99.0/ReactiveUI.6.0.Preview.1.zip


1
投票

您可以使用以下位置:

  • $ {Organization}作为GitHub用户或组织
  • $ {Repository}是存储库名称

curl -L https://api.github.com/repos/${Organization}/${Repository}/tarball > ${Repository}.tar.gz

.tar.gz文件中的顶级目录在目录名称中具有提交的sha哈希值,如果您需要以自动方式更改为生成的目录并执行某些操作,则可能会出现问题。

下面的方法将删除它,并将文件保留在具有可预测名称的文件夹中。

mkdir ${Repository} curl -L https://api.github.com/repos/${Organization}/${Repository}/tarball | tar -zxv -C ${Repository} --strip-components=1


1
投票

由于我没有在这里看到答案,但是在运行持续集成测试时对我来说非常有帮助,这个只需要你有curl的单行将允许搜索Github repo的发行版以下载最新版本

https://gist.github.com/steinwaywhw/a4cd19cda655b8249d908261a62687f8

我使用它来使用以下脚本在我们的存储库上运行PHPSTan

https://gist.github.com/rvanlaak/7491f2c4f0c456a93f90e31774300b62


0
投票

如果您使用PHP,请尝试按照代码:

function getLatestTagUrl($repository, $default = 'master') {
    $file = @json_decode(@file_get_contents("https://api.github.com/repos/$repository/tags", false,
        stream_context_create(['http' => ['header' => "User-Agent: Vestibulum\r\n"]])
    ));

    return sprintf("https://github.com/$repository/archive/%s.zip", $file ? reset($file)->name : $default);
}

功能用法示例

echo '<a href="' .getLatestTagUrl('OzzyCzech/vestibulum') .'">Download</a>';
© www.soinside.com 2019 - 2024. All rights reserved.