Skip to content

Instantly share code, notes, and snippets.

@jairov4
Forked from maxim/gh-dl-release
Last active April 21, 2020 15:15
Show Gist options
  • Save jairov4/12a746229c801b32b98dedbbeaac3397 to your computer and use it in GitHub Desktop.
Save jairov4/12a746229c801b32b98dedbbeaac3397 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:
#
# export GH_TOKEN=347583947589798791237487934857
# gh-dl-release org/repo my_app.tar.gz 2.1.1
#
# to download version:
#
# gh-dl-release <org_repo> <filename> <version_tag>
#
# Version tag can be 'latest'.
# If your version/tag doesn't match, the script will exit with error.
set -e
TOKEN="$GH_TOKEN"
REPO="$1"
FILE="$2" # the name of your release asset file, e.g. build.tar.gz
VERSION="$3" # 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 --auth-no-challenge --header='Accept:application/octet-stream' \
https://$TOKEN:@api.github.com/repos/$REPO/releases/assets/$asset_id \
-O $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment