Introduction

We needed to download a software from Github in its latest release without specifying the version number. This had to be done with curl, so we came up with the following code.

Solution

First we have to retrieve all the data about the latest release, which can be easily done by the use of the GitHub API. The URL follows the same scheme:

https://api.github.com/repos/USERNAME/REPO/releases/latest

In our case:

https://api.github.com/repos/felixb/swamp/releases/latest

The result is a JSON file with the description of the release and download links for specific files:

{
  "url": "https://api.github.com/repos/felixb/swamp/releases/assets/11040706",
  "id": 11040706,
  "node_id": "MDEyOlJlbGVhc2VBc3NldDExMDQwNzA2",
  "name": "swamp_amd64",
  "label": null,
  "uploader": {
    "login": "felixb",
    "id": 192135,
    "node_id": "MDQ6VXNlcjE5MjEzNQ==",
    "avatar_url": "https://avatars1.githubusercontent.com/u/192135?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/felixb",
    "html_url": "https://github.com/felixb",
    "followers_url": "https://api.github.com/users/felixb/followers",
    "following_url": "https://api.github.com/users/felixb/following{/other_user}",
    "gists_url": "https://api.github.com/users/felixb/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/felixb/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/felixb/subscriptions",
    "organizations_url": "https://api.github.com/users/felixb/orgs",
    "repos_url": "https://api.github.com/users/felixb/repos",
    "events_url": "https://api.github.com/users/felixb/events{/privacy}",
    "received_events_url": "https://api.github.com/users/felixb/received_events",
    "type": "User",
    "site_admin": false
  },
  "content_type": "application/octet-stream",
  "state": "uploaded",
  "size": 11765256,
  "download_count": 916,
  "created_at": "2019-02-12T06:39:41Z",
  "updated_at": "2019-02-12T06:39:49Z",
  "browser_download_url": "https://github.com/felixb/swamp/releases/download/v0.10.0/swamp_amd64"
}

Now we can write our curl command to retrieve the download URL for the Linux version.

DOWNLOAD_URL=$(curl -s https://api.github.com/repos/felixb/swamp/releases/latest \
        | grep browser_download_url \
        | grep swamp_amd64 \
        | cut -d '"' -f 4)
curl -s -L --create-dirs -o ~/downloadDir "$DOWNLOAD_URL"

Note: Replace the repository URL and grep swamp_amd64 with the file you need (grep YOUR_FILE).