How can I get the latest pre-release release for my github repo - bash

Viewed 1890

To get the latest release I can run:

curl --silent "https://api.github.com/repos/maxisme/notifi/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'

But I want the latest release tag_name that is a draft/pre-release?

1 Answers

We can use the following route to get all the releases:

https://api.github.com/repos/maxisme/notifi/releases

Using a Json tool like we can easily filter all the object to only show those where prerelease: true, then, extract the tag_name of that latest release like so:

jq -r 'map(select(.prerelease)) | first | .tag_name'

Where :
JqPlay demo

  1. select(.prerelease) filters to items where prerelease: true
  2. first get the first object in the array of releases
  3. .tag_name shows the value of the tag_name key

Combining this to a bash one-liner:

jq -r 'map(select(.prerelease)) | first | .tag_name' <<< $(curl --silent https://api.github.com/repos/maxisme/notifi/releases)

Prints:

0.9.9

If you're not sure there will be a prerelease on the project, we can add a fallback to the call:

map(select(.prerelease)) | first | .tag_name // "Not found"

This will now show Not found if there's no tag_name on a prelease.

Related