How to Download Artifacts from a Latest GitHub Release with Bash

Table of Contents

Lately I’ve been working on an application for which the releases are published on GitHub, and I wanted to create an install script which always downloads the latest release.

It turned out that downloading the artifacts for a specific version is easy, we can simply build an URL with the version number to access the artifact. However, there is no direct URL to download artifacts from the latest release, so we need a bit of logic in our scripts to do that.

How to Download Artifacts from a Latest GitHub Release with Bash

Link structure

We can access a particular release with a URL like https://github.com/user/<repo>/<releases>/tag/v1.x.x. (The part after tag/ is what we specified when we created the release.)

How to Download Artifacts from a Latest GitHub Release with Bash

Note

And there is this URL, which always takes us to the latest release of a project: https://github.com/user/repo/releases/latest.

Artifacts from this particular release can be downloaded with the URL https://github.com/<user>/<repo>/releases/download/v1.x.x/myArtifact.zip

How to Download Artifacts from a Latest GitHub Release with Bash

Getting the latest version from GitHub

To achieve that goal, you will need curl or wget shipped with Linux, for this to work.

Take a look the link of zlib project https://github.com///releases/latest.

  • <user> = madler
  • <repo> = zlib
 curl -s https://api.github.com/repos/<user>/<repo>/releases/latest \
  | awk -F': ' '/browser_download_url/ && /\.<file extension>/ \
  {gsub(/"/, "", $(NF)); system("curl -LO " $(NF))}'
wget -q -nv -O - https://api.github.com/repos/<user>/<repo>/releases/latest \
  | awk -F': ' '/browser_download_url/ && /\.<file extension>/ \
  {gsub(/"/, "", $(NF)); system("wget -qi -L " $(NF))}'

Here is a quick explanation

  1. First, curl or wget obtains the response from an HTTP GET request. This response contains the URLs for the various artifacts for the latest release of the project.
  2. Next, awk processes the response, finding the line containing a download URL and matching the given file extension.
  3. It then removes the quotation marks surrounding the URL and downloads the file directly with curl or wget.

You need modify the match pattern for the file extension (.zip, .tar.gz, .tar.xz…) to make it more specific if need be. And replace user and repo the GitHub project that you need grab the installer from. 

The following example demonstrates this by fetching the latest Linux release tar.gz file of the zlib.

Download with curl:

curl -s https://api.github.com/repos/madler/zlib/releases/latest \
  | awk -F': ' '/browser_download_url/ && /\.tar.gz/ \
  {gsub(/"/, "", $(NF)); system("curl -LO " $(NF))}'
# curl -s https://api.github.com/repos/madler/zlib/releases/latest \
>   | awk -F': ' '/browser_download_url/ && /\.tar.gz/ \
>   {gsub(/"/, "", $(NF)); system("curl -LO " $(NF))}'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 1462k  100 1462k    0     0  1066k      0  0:00:01  0:00:01 --:--:-- 12.6M

# ll
-rw-r--r--. 1 root root 1497445 Oct 30 10:35 zlib-1.2.13.tar.gz

Download with wget:

wget -q -nv -O - https://api.github.com/repos/madler/zlib/releases/latest \
  | awk -F': ' '/browser_download_url/ && /\.tar.gz/ \
  {gsub(/"/, "", $(NF)); system("wget -qi -L " $(NF))}' 
# wget -q -nv -O - https://api.github.com/repos/madler/zlib/releases/latest \
>   | awk -F': ' '/browser_download_url/ && /\.tar.gz/ \
>   {gsub(/"/, "", $(NF)); system("wget -qi -L " $(NF))}'

# ll
-rw-r--r--. 1 root root 1497445 Oct 14 06:07 zlib-1.2.13.tar.gz

Leave a Comment

Required fields are marked *