Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
One Liner to download the latest release from your GitHub repo
LOCATION=$(curl -s https://api.github.com/repos/<YOUR ORGANIZTION>/<YOUR REPO>/releases/latest \
| grep "tag_name" \
| awk '{print "https://github.com/<YOUR ORGANIZATION>/<YOUR REPO>/archive/" substr($2, 2, length($2)-3) ".zip"}') \
; curl -L -o <OUTPUT FILE NAME> $LOCATION

for example:

LOCATION=$(curl -s https://api.github.com/repos/gvenzl/csv2db/releases/latest \
| grep "tag_name" \
| awk '{print "https://github.com/gvenzl/csv2db/archive/" substr($2, 2, length($2)-3) ".zip"}') \
; curl -L -o csv2db.zip $LOCATION

Here is how it goes:

LOCATION=$(...)

stores the output of all the commands in the brackets in the variable $LOCATION

curl -s https://api.github.com/repos/gvenzl/csv2db/releases/latest

gets the latest release from your repository, in my case github.com/gvenzl/csv2db

grep "tag_name"

grabs the tag name of the latest release (e.g. v1.0.0)

awk '{print "https://github.com/gvenzl/csv2db/archive/" substr($2, 2, length($2)-3) ".zip"}'

prints "https://github.com/gvenzl/csv2db/archive/" v1.0.0 ".zip" --> "https://github.com/gvenzl/csv2db/archive/v1.0.0.zip"

Now the $LOCATION is set to that string.

curl -L -o csv2db.zip $LOCATION

invokes cURL and downloads $LOCATION into csv2db.zip. The -L parameter is important so that cURL follows the URL, i.e. redirect.

@alecthegeek

This comment has been minimized.

Copy link

@alecthegeek alecthegeek commented Mar 2, 2021

Awesome. Thanks for this. I shortened it to

latest_tag=$(curl -s https://api.github.com/repos/swagger-api/swagger-ui/releases/latest | sed -Ene '/^ *"tag_name": *"(v.+)",$/s//\1/p')
echo "Using version $latest_tag"
curl -JLO https://github.com/swagger-api/swagger-ui/archive/$latest_tag.tar.gz

Very handy for Docker files and CI/CD pipelines.

(you could of course make it one line, but I did want to display the version name)

@gvenzl

This comment has been minimized.

Copy link
Owner Author

@gvenzl gvenzl commented Mar 3, 2021

Very cool, thanks for adding your version to this! :)
I wasn't aware of the api.github.com part.
Looks like one could shoot straight for tarball_url or zipball_url as well if all that is wanted is the file itself:

location=$(curl -s https://api.github.com/repos/csv2db/csv2db/releases/latest | grep tarball_url | awk '{ print $2 }' | sed 's/,$//' | sed 's/"//g' );

There is probably still some further optimization that can be done on the awk and sed parts there :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment