【问题标题】:Download latest GitHub release下载最新的 GitHub 版本
【发布时间】:2014-02-21 17:42:07
【问题描述】:

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

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

【问题讨论】:

    标签: github tags release


    【解决方案1】:

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

    Release 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 的 releases/ 段和版本号之间插入 download/ 关键字
    • 附加要下载的资产名称

    生成的 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

    【讨论】:

    • 谢谢,你的方法很好,但我不想将用户绑定到 JS,因为它可以简单地在用户端关闭(并且使用 <noscript> 标签让我们回到体积问题)。
    • gist.github.com/ambrosechua/d426ac039e0a5f09059c 是执行此操作的快速草稿脚本,但仅适用于发布,但不适用于源代码下载。
    【解决方案2】:

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

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

    【讨论】:

    • 这种方法在我有兴趣使用它的项目中似乎已经失效。 :(
    • @ThorSummoner:一些项目维护者不会费心添加版本。在such cases 中,API 将返回一个空数组。
    【解决方案3】:

    如果您使用 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>';
    

    【讨论】:

    • 很遗憾,GitHub Pages 服务不支持 PHP。
    【解决方案4】:

    由于February 18th, 2015GitHUb V3 release API 有一个get latest release API

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

    【讨论】:

      【解决方案5】:

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

      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>

      【讨论】:

      【解决方案6】:

      您可以在 where 中使用以下内容:

      • ${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

      【讨论】:

        【解决方案7】:

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

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

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

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

        【讨论】:

          【解决方案8】:

          正如@Dan Dascalescu 在对已接受答案的评论中指出的那样,有一些项目(大约 30%)不需要提交正式版本,因此“最新版本”按钮和 /releases/latest API 调用都不会返回有用的数据。

          可靠地获取 GitHub 项目的最新版本,您可以使用 lastversion

          【讨论】: