I needed a way to get the latest version of a release inside of a bash script. I was thinking of trying to use AWK to get a list of versions and then get the first one from that, but I didn’t really feel like learning all that right now.

To accomplish this task, I created a file and put the following code inside of it.

github_get_latest_release() {
  curl --silent "https://api.github.com/repos/$1/releases/latest" | # Download the url
    grep -Po '"tag_name": "\K.*?(?=")'                              # Get release version
}

What this does is downloads a JSON file from the GitHub API. Thankfully, GitHub has something to reference the latest version. After the file is downloaded, grep is used to find the line with the latest version we are looking for.

This bash file can be referenced in any other bash script like so.

#!/bin/bash
source github_get_latest_release.sh
wget https://github.com/rancher/rke/releases/download/$(github_latest_release rancher/rke)/rke_linux-amd64 -O ~/bin/rke

Now, this script can be used to automatically download the latest version of the Rancher RKE binary.

References

  1. https://gist.github.com/lukechilds/a83e1d7127b78fef38c2914c4ececc3c