Download latest GitHub release

2020-01-29 06:54发布

问题:

I'd like to have "Download Latest Version" button on my website which would represent the link to the latest release (stored at GitHub Releases). I tried to create release tag named "latest", but it became complicated when I tried to load new release (confusion with tag creation date, tag interchanging, etc.). Updating download links on my website manually is also a time-consuming and scrupulous task. I see the only way - redirect all download buttons to some html, which in turn will redirect to the actual latest release.

Note that my website is hosted at GitHub Pages (static hosting), so I simply can't use server-side scripting to generate links. Any ideas?

回答1:

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



回答2:

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>



回答3:

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

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


回答4:

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



回答5:

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



回答6:

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



回答7:

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>';