Skip to content

Instantly share code, notes, and snippets.

@Illizian
Forked from maxim/gh-dl-release
Last active September 30, 2016 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Illizian/9e4854f93498db1a9058d06e87eeafed to your computer and use it in GitHub Desktop.
Save Illizian/9e4854f93498db1a9058d06e87eeafed to your computer and use it in GitHub Desktop.
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 githubtraining/hellogitworld 2.1.1 my_app.tar.gz
#
# to download latest version:
#
# gh-dl-release githubtraining/hellogitworld latest latest.tar.gz
#
# If your version/tag doesn't match, the script will exit with error.
GITHUB="https://api.github.com"
TOKEN=${GH_ACCESS_TOKEN}
REPO=$1
VERSION=$2 # tag name or the word "latest"
FILE=$3 # the name of your release asset file, e.g. build.tar.gz
if [ -z "$REPO" ] || [ -z "$VERSION" ] || [ -z "$FILE" ]; then
>&2 echo "Usage: gh-dl-release ORG/REPO VERSION FILENAME"
exit 1;
fi;
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[0].id"
else
parser=". | map(select(.tag_name == \"$VERSION\"))[0].assets[0].id"
fi;
asset_id=`gh_curl -s $GITHUB/repos/$REPO/releases | jq "$parser"`
if [ "$asset_id" = "null" ]; then
>&2 echo "ERROR: version not found $VERSION"
exit 1
fi;
echo "Downloading $VERSION [$asset_id]..."
wget -q --auth-no-challenge --header='Accept:application/octet-stream' \
https://$TOKEN:@api.github.com/repos/$REPO/releases/assets/$asset_id \
-O $FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment