Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Download assets from private Github releases
#!/usr/bin/env bash
#
# gh-dl-release! It works!
#
# This script downloads an asset from latest or specific Github release of a
# private repo. Feel free to extract more of the variables into command line
# parameters.
#
# PREREQUISITES
#
# curl, wget, jq
#
# USAGE
#
# Set all the variables inside the script, make sure you chmod +x it, then
# to download specific version to my_app.tar.gz:
#
# gh-dl-release 2.1.1 my_app.tar.gz
#
# to download latest version:
#
# gh-dl-release latest latest.tar.gz
#
# If your version/tag doesn't match, the script will exit with error.
TOKEN="<github_access_token>"
REPO="<user_or_org>/<repo_name>"
FILE="<name_of_asset_file>" # the name of your release asset file, e.g. build.tar.gz
VERSION=$1 # tag name or the word "latest"
GITHUB="https://api.github.com"
alias errcho='>&2 echo'
function gh_curl() {
curl -H "Authorization: token $TOKEN" \
-H "Accept: application/vnd.github.v3.raw" \
$@
}
if [ "$VERSION" = "latest" ]; then
# Github should return the latest release first.
parser=".[0].assets | map(select(.name == \"$FILE\"))[0].id"
else
parser=". | map(select(.tag_name == \"$VERSION\"))[0].assets | map(select(.name == \"$FILE\"))[0].id"
fi;
asset_id=`gh_curl -s $GITHUB/repos/$REPO/releases | jq "$parser"`
if [ "$asset_id" = "null" ]; then
errcho "ERROR: version not found $VERSION"
exit 1
fi;
wget -q --auth-no-challenge --header='Accept:application/octet-stream' \
https://$TOKEN:@api.github.com/repos/$REPO/releases/assets/$asset_id \
-O $2
@maxenglander

This comment has been minimized.

Copy link

commented Aug 13, 2016

Exactly what I needed - TY!

@nneves

This comment has been minimized.

Copy link

commented Jan 16, 2017

Thanks, this worked! Cheers!

@nneves

This comment has been minimized.

Copy link

commented Jan 18, 2017

sorry, line 47 should be:

asset_id=`gh_curl -L -s $GITHUB/repos/$REPO/releases | jq "$parser"`
@nneves

This comment has been minimized.

Copy link

commented Jan 18, 2017

Please ignore that, someone in my organization moved the REPO around and broke the script! All good, nice work!

@leoforney

This comment has been minimized.

Copy link

commented Feb 5, 2017

Can you give an example usage of this?

@saaguero

This comment has been minimized.

Copy link

commented Jun 28, 2017

This is a great script, thanks!

There is one problem though that I've been facing. If the release artifact is not in the first page (ie: you have to paginate until you get the desired release) this won't work.

So I ended up with a modified version to directly consume a particular tag, adapt it to your needs following the github documentation.

TOKEN="<github_access_token>"
REPO="<user_or_org>/<repo_name>"
FILE="<name_of_asset_file>"      # the name of your release asset file, e.g. build.tar.gz
VERSION=$1                       # tag name or the word "latest"
GITHUB="https://api.github.com"

alias errcho='>&2 echo'

function gh_curl() {
  curl -H "Authorization: token $TOKEN" \
       -H "Accept: application/vnd.github.v3.raw" \
       $@
}

parser=".assets[0].id"
asset_id=$(gh_curl -s $GITHUB/api/v3/repos/$REPO/releases/tags/$VERSION | jq $parser)

if [ "$asset_id" = "null" ]; then
  errcho "ERROR: version not found $VERSION"
  exit 1
fi;

wget -q --auth-no-challenge --header='Accept:application/octet-stream' \
  https://$TOKEN:@api.github.com/repos/$REPO/releases/assets/$asset_id \
  -O $2
@dankhan-adl

This comment has been minimized.

Copy link

commented Jul 16, 2017

I am getting

parse error: Invalid numeric literal at line 1, column 9
@dedelala

This comment has been minimized.

Copy link

commented Jun 4, 2018

This can be done without using a token in the URL, but the token cannot be in the header sent to the redirect link.

redir=$(curl -H "Authorization: token $TOKEN" -H "Accept: application/octet-stream" \
  -w "%{redirect_url}" -o "$dest" "$url")

# redir will be a non-empty string if the response was a 302, and dest will be an empty file
[[ -n $redir ]] && curl -o "$dest" "$url"
@deevus

This comment has been minimized.

Copy link

commented Jan 15, 2019

Update using getopts instead of hard coded variables https://gist.github.com/deevus/af29b590e53a8249df5a852a7f7c5b14

@alexivkin

This comment has been minimized.

Copy link

commented Feb 6, 2019

wget can be replaced with
curl -sL --header "Authorization: token $TOKEN" --header 'Accept: application/octet-stream' https://api.github....

@wyozi

This comment has been minimized.

Copy link

commented Mar 6, 2019

I made a Github action partially based on this: https://github.com/wyozi/download-gh-release-asset

@jessp01

This comment has been minimized.

Copy link

commented Aug 5, 2019

Thanks for this. Quite useful. Here's a somewhat more minimal version that doesn't require wget and accepts the needed params as CLI args:

if [ $# -lt 4 ] ;then
    echo "Usage: <github token> <org/repo> <filename> <version or 'latest'>"
    exit 1
fi

TOKEN="$1"
REPO="$2"
FILE="$3"      # the name of your release asset file, e.g. build.tar.gz
VERSION=$4                       # tag name or the word "latest"
GITHUB_API_ENDPOINT="api.github.com"

alias errcho='>&2 echo'

function gh_curl() {
  curl -sL -H "Authorization: token $TOKEN" \
       -H "Accept: application/vnd.github.v3.raw" \
       $@
}

if [ "$VERSION" = "latest" ]; then
  # Github should return the latest release first.
  PARSER=".[0].assets | map(select(.name == \"$FILE\"))[0].id"
else
  PARSER=". | map(select(.tag_name == \"$VERSION\"))[0].assets | map(select(.name == \"$FILE\"))[0].id"
fi

ASSET_ID=`gh_curl https://$GITHUB_API_ENDPOINT/repos/$REPO/releases | jq "$PARSER"`
if [ "$ASSET_ID" = "null" ]; then
  errcho "ERROR: version not found $VERSION"
  exit 1
fi

curl -sL --header "Authorization: token $TOKEN" --header 'Accept: application/octet-stream' https://$TOKEN:@$GITHUB_API_ENDPOINT/repos/$REPO/releases/assets/$ASSET_ID > $FILE
@germanattanasio

This comment has been minimized.

Copy link

commented Aug 5, 2019

@jessp01 to run this I had to remove the gh_curl and do it in inline

@jessp01

This comment has been minimized.

Copy link

commented Aug 5, 2019

Hi @germanattanasio,

I'm not sure I understand what you mean. Both the call to gh_curl():

ASSET_ID=`gh_curl https://$GITHUB_API_ENDPOINT/repos/$REPO/releases | jq "$PARSER"`

and:

curl -sL --header "Authorization: token $TOKEN" --header 'Accept: application/octet-stream' https://$TOKEN:@$GITHUB_API_ENDPOINT/repos/$REPO/releases/assets/$ASSET_ID > $FILE

Are needed. One fetches the asset ID using the API and the other fetches the actual asset/file.
The reason why both requests cannot be made by calling gh_curl() as it is now is because it sets the Accept: application/vnd.github.v3.raw headers, which is needed for the first request but hinders the latter. You could refactor the function so that it sets either header based on the request (fetching the ASSET_ID or downloading the actual asset). To me, it felt like too much trouble for this sort of script :)

@germanattanasio

This comment has been minimized.

Copy link

commented Aug 6, 2019

I was trying to say that when I run the script with the gh_curl function in Travis I get an error. I ended up doing:

ASSET_ID=`curl -sL -H "Authorization: token $TOKEN" -H "Accept: application/vnd.github.v3.raw" https://$GITHUB_API_ENDPOINT/repos/$REPO/releases | jq "$PARSER"`
@kavinda1995

This comment has been minimized.

Copy link

commented Aug 15, 2019

Maaaan. You save my day and me ❤️ 😍
Trying to find a solution to fix 404 error in whole day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.