Download latest GitHub release

元气小坏坏 提交于 2019-11-27 13:37:56
dod

Github now provides a "Latest release" button on the release page of a project, after you have created your first release.

In the example you gave, this button links to https://github.com/reactiveui/ReactiveUI/releases/latest

Dan Dascalescu

You don't need any scripting to generate a download link for the latest release. Simply use this format:

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

Examples:

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

If for some reason you want to obtain a link to the latest release download, including its version number, you can obtain that from the get latest release API:

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

Example:

$.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>
VonC

Since February 18th, 2015, the GitHUb V3 release API has a get latest release API.

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

Maybe could you use some client-side scripting and dynamically generate the target of the link by invoking the GitHub api, through some JQuery magic?

The Releases API exposes a way to retrieve the list of all the releases from a repository. For instance, this link return a Json formatted list of all the releases of the ReactiveUI project.

Extracting the first one would return the latest release.

Within this payload:

  • The html_url attribute will hold the first part of the url to build (ie. https://github.com/{owner}/{repository}/releases/{version}).

  • The assets array will list of the downloadable archives. Each asset will bear a name attribute

Building the target download url is only a few string operations away.

  • Insert the download/ keyword between the releases/ segment from the html_url and the version number
  • Append the name of the asset to download

Resulting url will be of the following format: https://github.com/{owner}/{repository}/releases/download/{version}/name_of_asset

For instance, regarding the Json payload from the link ReactiveUI link above, we've got html_url: "https://github.com/reactiveui/ReactiveUI/releases/5.99.0" and one asset with name: "ReactiveUI.6.0.Preview.1.zip".

As such, the download url is https://github.com/reactiveui/ReactiveUI/releases/download/5.99.0/ReactiveUI.6.0.Preview.1.zip

You can use the following where:

  • ${Organization} as the GitHub user or organization
  • ${Repository} is the repository name

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

The top level directory in the .tar.gz file has the sha hash of the commit in the directory name which can be a problem if you need an automated way to change into the resulting directory and do something.

The method below will strip this out, and leave the files in a folder with a predictable name.

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

As I didn't see the answer here, but it was quite helpful for me while running continuous integration tests, this one-liner that only requires you to have curl will allow to search the Github repo's releases to download the latest version

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

I use it to run PHPSTan on our repository using the following script

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

If you using PHP try follow code:

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);
}

Function usage example

echo '<a href="' .getLatestTagUrl('OzzyCzech/vestibulum') .'">Download</a>';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!