How to download the latest release from Github

GitHub Releases are a great resource for open source projects to expand on the simple git tag concept. You can add release notes in Markdown format, and you can upload finalized assets – such as compiled executables.

As a user I had the question – how do I script "download the latest release, please?"

For example, when Geoff Franks or someone from the https://spruce.cf team cuts a new spruce version, what can I put in my bash scripts to automatically download that new version, rather than a fixed version number.

curl, jq and the Github API to the rescue:

spruce_type=linux_amd64
curl -s https://api.github.com/repos/geofffranks/spruce/releases/latest | jq -r ".assets[] | select(.name | test(\"${spruce_type}\")) | .browser_download_url"

The magic is the .../releases/latest API endpoint which automatically figures out which is the latest release.

Will return the download URL for the linux_amd64 version of Spruce:

https://github.com/geofffranks/spruce/releases/download/v1.0.1/spruce_1.0.1_linux_amd64.tar.gz

The | select(.name | test"<some-filter>") portion of the jq filter would be specific to the release and its files.

The spruce project releases the following files each time:

curl -s https://api.github.com/repos/geofffranks/spruce/releases/latest | jq -r ".assets[].name"

Which returns (for the current v1.0.1 release):

LICENSE
README.md
spruce_1.0.1_darwin_amd64.zip
spruce_1.0.1_linux_386.tar.gz
spruce_1.0.1_linux_amd64.tar.gz

So we can find the target file by filtering between darwin_amd64 or linux_386 or linux_amd64.

For another project, the files and how to filter to get the right one might be different.

Spread the word

twitter icon facebook icon linkedin icon