46

Although this question is similar to GitHub latest release, it's actually different -- it's about a link that means "the latest version of the download file itself".

GitHub provides a "Latest" URL that redirects to the information page for the latest release. For example: https://github.com/reactiveui/ReactiveUI/releases/latest will redirect to https://github.com/reactiveui/ReactiveUI/releases/tag/5.99.6 (as I type this; or to the page for a newer version, someday).

That's great but I need a URL to the download file itself. In this example, the .zip file associated with the green download button, https://github.com/reactiveui/ReactiveUI/releases/download/5.99.6/ReactiveUI-5.99.6.zip (as I type this; or to a newer zip file, someday).

Why? I want to give the URL to curl, as part of a Travis CI script, to download the latest version.

I guessed at a few URLs like /releases/download/latest/file.zip (substituting "latest" for the version part) and /releases/download/file.zip but those 404.

Is there any way to do this -- in the context of a shell script and curl (note: not in a browser page with JS)?

Greg Hendershott
  • 16,100
  • 6
  • 36
  • 53

10 Answers10

41

For releases that do not contain the version number or other variable content in their assets' names, you can use a URL of the format:

https://github.com/owner/repository/releases/latest/download/ASSET.ext

As per the docs:

If you'd like to link directly to a download of your latest release asset you can link to /owner/name/releases/latest/download/asset-name.zip.

  • This is my preferred option, when scripting on Windows. E.g. when downloading the latest version of AWS Vault, I use: `https://github.com/99designs/aws-vault/releases/latest/download/aws-vault-windows-386.exe`. Whereas the current release (as at today's date) is at `https://github.com/99designs/aws-vault/releases/download/v6.2.0/aws-vault-windows-386.exe`. You should handle the case where there's no such file at that URL, of course. – Rob Pomeroy Nov 30 '20 at 14:09
23

Here is a way to do it w/o Github if you have a single download in the release:

wget $(curl -s https://api.github.com/repos/USERNAME/REPONAME/releases/latest | grep 'browser_' | cut -d\" -f4)

It is pretty easy (though not pretty), and of course you can swap out wget for another curl call if you want to pipe it to something.

Basically, the curl call nets you a JSON structure, and I'm just using basic shell utilities to extract the URL to the download.

The Real Bill
  • 14,884
  • 8
  • 37
  • 39
  • I've been looking for this. Thx! Just to make it uglier and easier, I've added a final pipe to `grep "docker-machine-\`uname -s\`-\`uname -m\`"`. Works as long as you aren't on Windows. – Mike Mar 05 '16 at 16:29
  • I've been thinking of writing this as a Go command line too actually. :) I'll keep that in mind for it. – The Real Bill Mar 05 '16 at 16:40
  • 1
    If your releases have unique file names, instead of `grep`ping `'browser_'` you can also grep the file name. That made me help to specify and help to download only the correct file in a release which has multiple downloads. – Arda Nov 07 '16 at 21:15
  • To remove the wget dependency, `curl -L $(curl -s https://api.github.com/repos/USER/REPO/releases/latest | grep browser_ | cut -d\" -f4)`. And, as @Arda mentioned, you can pipe to another `| grep PATTERN` after `| grep browser_` to narrow it based on asset name. – rocky Sep 11 '18 at 03:15
  • no need for grepping the release version from the API. See haykams answer: https://github.com/owner/repository/releases/latest/download/ASSET.ext – Echsecutor Aug 05 '20 at 12:30
3

Very interesting, I haven't noticed a "latest" tag in GitHub-releases yet. As i now figured out, they're given away if you're using the "pre-release"-capabilities of GitHubs release-system. But i don't know any way to access binaries via a latest-path.

I would like to suggest you using git (which is available in your travis-vm) to download the latest tag.

Like Julien Renault describes in his blog post, you will be able to checkout the latest tag in the repository like this:

# this step should be optional
git fetch --tags

latestTag=$(git describe --tags `git rev-list --tags --max-count=1`)
git checkout $latestTag

This solution is based on the assumption that the latest tag is also the latest version.

Florian Neumann
  • 5,587
  • 1
  • 39
  • 48
  • Although I'm not sure I understand how most of your answer relates to my question, your idea to "use git" is spot on. Of course I should use that -- thanks! – Greg Hendershott Jun 06 '14 at 23:46
  • 1
    @greghendershott - I'm happy i gave you a hint to the solution. Unfortunately i did not recognize you were searching for an answer on how to release your own repository and make its last release available via github. That could possibly be done more easily via a branch `latest`. But i can't dig in deeper, since i'm hanging around a beach with my mobile. :) – Florian Neumann Jun 08 '14 at 10:38
  • 1
    You can simply use `latestTag=$(git rev-list --tags --max-count=1)`. –  Jul 04 '18 at 12:25
2

I use this to get the download URLs in PowerShell 5+ (replace ACCOUNT & REPO)

Invoke-RestMethod -uri  https://api.github.com/repos/ACCOUNT/REPO/releases/latest | select -ExpandProperty assets | select -expand browser_download_url 

Note if they have more than one package this will be a list. If you want to pick a certain one find a unique part of the name i.e. win for Windows and use: (replace ACCOUNT, REPO & SELECTOR)

Invoke-RestMethod -uri  https://api.github.com/repos/ACCOUNT/REPO/releases/latest | select -ExpandProperty assets | ? { $_.name.Contains("SELECTOR")} | select -expand browser_download_url

As a bonus if you assign the above to a variable you can then grab the file and extract it with the following (assuming you assign to $uri):

Invoke-WebRequest $uri -OutFile "release.zip"
Expand-Archive .\release.zip

In PowerShell 6+ this should work on other platforms than Windows.

Lex
  • 658
  • 4
  • 17
0

On windows, only using powershell, this works for me. It can probably be written a lot shorter.

#Downloads latest paket.bootstrapper.exe from github
$urlbase = "https://github.com"
$latestPage="$urlbase/fsprojects/Paket/releases/latest"
Write-Host "Parsing latest release page: $latestPage"
$page=Invoke-Webrequest -uri $latestPage
$latestBootStrapper=($page.Links | Where-Object { $_.href -match "bootstrapper" }).href

$dlurl="$urlbase$latestBootStrapper"
Write-Host "Downloading paket.bootstrapper.exe from $dlurl"

$wc=new-object net.webclient
$wc.UseDefaultCredentials=$true
$wc.Proxy.Credentials=$wc.Credentials
$wc.DownloadFile($dlurl, (join-path (resolve-path ".\") "paket.bootstrapper.exe"))
8DH
  • 2,022
  • 23
  • 36
0
$repoName = "PowerShell/PowerShell"
$assetPattern = "*-win-x64.msi"
$extractDirectory = "C:\Users\Public\Downloads"


$releasesUri = "https://api.github.com/repos/$repoName/releases/latest"
$asset = (Invoke-WebRequest $releasesUri | ConvertFrom-Json).assets | Where-Object name -like $assetPattern
$downloadUri = $asset.browser_download_url

$extractPath = [System.IO.Path]::Combine($extractDirectory, $asset.name)
Invoke-WebRequest -Uri $downloadUri -Out $extractPath
maxc137
  • 2,291
  • 3
  • 20
  • 32
0

You can use curl with https://api.github.com. It gives JSON output from which you can easily extract what you need with jq or your favorite json tool.

For example, using the repository in the question:

gituser=reactiveui; repo=ReactiveUI

tag_name=$(curl -sL https://api.github.com/repos/$gituser/$repo/releases/latest | jq -r '.tag_name'); echo $tag_name
# output: "16.3.10"

tarurl=$(curl -sL https://api.github.com/repos/$gituser/$repo/releases/latest | jq -r '.tarball_url'); echo $tarurl
# output: https://api.github.com/repos/reactiveui/ReactiveUI/tarball/16.3.10

zipurl=$(curl -sL https://api.github.com/repos/$gituser/$repo/releases/latest | jq -r '.zipball_url'); echo $zipurl
# output: https://api.github.com/repos/reactiveui/ReactiveUI/zipball/16.3.10

So you could get the download with a nested curl in a one-liner:

curl -OL $(curl -sL https://api.github.com/repos/filesender/filesender/releases/latest | jq -r '.tarball_url')

This will download the file, and save it with the name of its tag_name, but without extension. So you may want to rename it by appending ".tgz" or ".zip", depending on which you downloaded.

Note for Windows users: curl is now installed by default on Windows too, but beware that it must be called as curl.exe. That's because Powershell has an alias stupidly called "curl" which is not the same!

mivk
  • 13,452
  • 5
  • 76
  • 69
0

Centos/RHEL

There are 2 options to download using the URL directly.

  1. Via Github API (using CURL and jq package)
  2. Via Github direct (using CURL and sed)

I am listing a demonstration script for each option.

Option 1

#!/bin/bash

# author: fullarray
# Contribution shared on: stackoverflow
# Contribution shared on: github
# date: 06112022

compose_version=$(curl https://api.github.com/repos/docker/compose/releases/latest | jq .name -r)

get_local_os_build=$(uname -s)-$(uname -m)

curl -L https://github.com/docker/compose/releases/download/$compose_version/docker-compose-$get_local_os_build

Option 2

#!/bin/bash

# author: fullarray
# Contribution shared on: stackoverflow
# Contribution shared on: github
# date: 06112022

get_local_os_build=$(uname -s)-$(uname -m)

compose_latest_version=$(curl -L "https://github.com/docker/compose/releases/download/`curl -fsSLI -o /dev/null -w %{url_effective} https://github.com/docker/compose/releases/latest | sed 's#.*tag/##g' && echo`/docker-compose-$get_local_os_build")
Full Array
  • 769
  • 7
  • 12
0

If you are fine with cloning the repository first, you can use git tag, which also allows you to sort the tags by version in various ways.

git clone https://github.com/reactiveui/ReactiveUI.git .
LATEST="$(git tag --sort=v:refname | tail -n1)"
git checkout "$LATEST"

This allows for more flexibility, as you can filter the tags you're not interested in with grep, e.g.:

git tag --sort=v:refname | grep -vE '-RC[0-9]+$' | tail -n1

Here's an excerpt from the documentation on git-tag:

Sort based on the key given. Prefix - to sort in descending order of the value. You may use the --sort=<key> option multiple times, in which case the last key becomes the primary key. Also supports version:refname or v:refname (tag names are treated as versions). The version:refname sort order can also be affected by the versionsort.suffix configuration variable. The keys supported are the same as those in git for-each-ref. Sort order defaults to the value configured for the tag.sort variable if it exists, or lexicographic order otherwise. See git-config(1).

If you really don't want to clone the repository, the --sort option also works with git ls-remote. It'll just take a bit more work to get the part you're interested in:

git ls-remote --tags --sort=v:refname https://github.com/reactiveui/ReactiveUI.git  | awk -F'/' '{ print $NF }'

This approach doesn't seem to work all too well for the ReactiveUI repository in particular, because their tags are a bit messy, but it's an option.

Please note, that the sorting isn't quite the same as with semantic versioning, but git does allow you to work around most of these cases. As an example mqtt2prometheus has release candidates using the suffix RC1, RC2 etc., but git sorts 0.1.6-RC1 as being newer than 0.1.6. You can tell git that "RC" is a pre-release suffix to make it sort them correctly.

git tag -c 'versionsort.suffix=-RC' --sort=v:refname | tail -n1

Here's an excerpt from the documentation on git-config:

By specifying a single suffix in this variable, any tagname containing that suffix will appear before the corresponding main release. E.g. if the variable is set to "-rc", then all "1.0-rcX" tags will appear before "1.0". If specified multiple times, once per suffix, then the order of suffixes in the configuration will determine the sorting order of tagnames with those suffixes. E.g. if "-pre" appears before "-rc" in the configuration, then all "1.0-preX" tags will be listed before any "1.0-rcX" tags.

You can also sort by the date of the tag using --sort=taggerdate, that might work better in some situations.

Steen Schütt
  • 1,355
  • 1
  • 17
  • 31
-1

As @florianb pointed out, I should use git.

Originally my .travis.yml was something like:

before_install:
- curl -L https://raw.githubusercontent.com/greghendershott/travis-racket/master/install-racket.sh | bash

This would automatically get whatever the latest version is, from the repo.

But someone pointed out to me that GitHub doesn't want people to use raw.github.com for downloads. Instead people should use "releases". So I was a good doob and manually made a release each time. Then my .travis.yml was something like:

before_install:
- curl -L https://github.com/greghendershott/travis-racket/releases/download/v0.6/install-racket.sh | bash

But it's a PITA to make a release each time. Worse, all .travis.yml files need to be updated to point to the newer version of the file.

Instead -- just use git to clone the repo, and use the file within it:

before_install:
- git clone https://github.com/greghendershott/travis-racket.git
- cat travis-racket/install-racket.sh | bash # pipe to bash not sh!
Greg Hendershott
  • 16,100
  • 6
  • 36
  • 53