Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gvenzl/1386755861fb42db492276d3864a378c to your computer and use it in GitHub Desktop.
Save gvenzl/1386755861fb42db492276d3864a378c to your computer and use it in GitHub Desktop.
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 "zipball_url" \
| awk '{ print $2 }' \
| sed 's/,$//'       \
| sed 's/"//g' )     \
; curl -L -o <OUTPUT FILE NAME> $LOCATION

for example:

LOCATION=$(curl -s https://api.github.com/repos/csv2db/csv2db/releases/latest \
| grep "zipball_url" \
| awk '{ print $2 }' \
| sed 's/,$//'       \
| sed 's/"//g' )     \
; 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/csv2db/csv2db/releases/latest

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

grep "zipball_url"

grabs the line for the zipball URL of the file --> "zipball_url": "https://api.github.com/repos/csv2db/csv2db/zipball/v1.5.1",
Note, there is also a tarball_url if you prefer the .tar.gz file instead.

awk '{ print $2 }'

prints just the URL part of the zipball_url line --> "https://api.github.com/repos/csv2db/csv2db/zipball/v1.5.1",

sed 's/,$//'

removes the comma , at the end of the line --> "https://api.github.com/repos/csv2db/csv2db/zipball/v1.5.1"

sed 's/"//g'

removes the double quotes " from the previous output --> https://api.github.com/repos/csv2db/csv2db/zipball/v1.5.1

Once all these commands are executed, the string is stored in the $LOCATION variable due to the surrounding $(...). The next step is to download the file from that location (or, at this stage you can do anything else you like with $LOCATION).

curl -L -o csv2db.zip $LOCATION

invokes cURL and downloads $LOCATION into a file called csv2db.zip. The -L parameter is important so that cURL follows the URL, i.e. redirects on the web page, in case that the URL gets forwarded to https or another location on the server. cURL also has an option to just store the file name under the remove file name. This can be done via -O (uppercase O) rather than -o. If you are using -O you are also advised to use -J which tells the -O option to use the server-specified Content-Disposition filename instead of extracting a filename from the URL.

@doctorfree
Copy link

/etc/os-release only needs to be readable, not executable, by the user. I believe all Linux distributions set permissions on this as 644 making it readable by all. But, to be sure, a test that /etc/os-release exists and is readable by the user could be performed along with an error if the test fails. Thanks!

@gvenzl
Copy link
Author

gvenzl commented Oct 9, 2022

Hi @doctorfree,

You are right, somehow I expected it to be a binary because there is a man page for it.
Looking at it, it indeed is only a text file with some variables.

Yeah, nice error messages is always a bonus for the end user :)

@Binly42
Copy link

Binly42 commented Nov 27, 2022

for multiple assets, might try:

curl --remote-name-all --location  $( \
    curl -s https://api.github.com/repos/jedisct1/minisign/releases/latest \
    | grep "browser_download_url.*\linux.*" \
    | cut -d : -f 2,3 \
    | tr -d \" )

@gvenzl
Copy link
Author

gvenzl commented Nov 27, 2022

Awesome, thanks @Binly42!

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