Skip to content

Instantly share code, notes, and snippets.

@CybotTM
Forked from lukechilds/get_latest_release.sh
Last active October 18, 2023 09:14
Show Gist options
  • Save CybotTM/0da4a8faf9993bdcf13b446d9e2d0ed2 to your computer and use it in GitHub Desktop.
Save CybotTM/0da4a8faf9993bdcf13b446d9e2d0ed2 to your computer and use it in GitHub Desktop.
Shell command one-liner - To get latest or highest release from GitHub

One-liner to retrieve latest highest release (of a specific major version) from GitHub

Just retrieving the latest release may not what you want - An LTS v1.1.20 maybe released after another LTS v2.0.10

Get highest available version

Example to get highest version, not just latest - because an LTS bugfix 1.2.3 could be released after a new major 3.x version

curl https://api.github.com/repos/docker/compose/releases -s | jq -r .[].tag_name | grep '^v*[0-9]\.[0-9]*\.[0-9]*$' | sort -rV | head -n1

Requirements

sudo apt install jq curl grep coreutils

Using variables

optional_major_version=[0-9]* && \
optional_prefix=v && \
optional_results=100 && \
project=docker/compose && \
curl https://api.github.com/repos/${project}/releases?per_page${optional_results:-100} -s \
  | jq -r .[].tag_name \
  | grep "^${optional_prefix:-v}*${optional_major_version:-[0-9]*}\.[0-9]*\.[0-9]*\$" \
  | sort -rV \
  | head -n1
optional_major_version=[0-9]*
Optional: limit search to specific major version: "2" "[7-8]", default: [0-9]*
optional_prefix=v
Optional: inlcude an optional version prefix: "v", "b", "V", default: v
project=docker/compose
GitHub project, for which to retrieve release information
curl https://api.github.com/repos/${project}/releases -s
Fetch release info from GitHub project
-s - do not show progress bar or any other output
jq -r .[].tag_name
Extract all tag names from returned JSON
grep "^${optional_prefix:-v}*${optional_major_version:-[0-9]*}\.[0-9]*\.[0-9]*\$"
Show only version tags in the form 1.2.3 or v3.1.22
sort -rn
sort output reversed and by version
head -n1
display only the first row - the highest version number

or find latest version for a specific major version:

# find latest docker-compose v1 release
curl https://api.github.com/repos/docker/compose/releases -s | jq -r .[].tag_name | grep '^v*1\.[0-9]*\.[0-9]*$' -m1
# find latest docker compose v2 release
curl https://api.github.com/repos/docker/compose/releases -s | jq -r .[].tag_name | grep '^v*2\.[0-9]*\.[0-9]*$' -m1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment