Skip to content

Instantly share code, notes, and snippets.

@Lauszus
Forked from maxim/gh-dl-release
Created June 6, 2019 18:53
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 Lauszus/6ba6fb08d49189009bdfea3eddac069d to your computer and use it in GitHub Desktop.
Save Lauszus/6ba6fb08d49189009bdfea3eddac069d to your computer and use it in GitHub Desktop.
Download assets from private Github releases
#!/bin/bash -e
#
# 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.sh lauszus blhost 1.1.0 blhost-linux-armv7l
#
# to download latest version:
#
# $ ./gh-dl-release.sh lauszus blhost latest blhost-linux-armv7l
#
# To download all asserts for a release:
#
# $ ./gh-dl-release.sh lauszus blhost 1.1.0
# $ ./gh-dl-release.sh lauszus blhost latest
#
# If your version/tag doesn't match, the script will exit with an error.
#
# Original version: https://gist.github.com/maxim/6e15aa45ba010ab030c4
# Modified by Kristian Sloth Lauszus
TOKEN="$GITHUB_OAUTH_TOKEN" # https://github.com/settings/tokens
OWNER="$1"
REPO="$2"
VERSION="$3" # tag name or the word "latest"
FILE="$4" # the name of your release asset file, e.g. build.tar.gz - this is optional
function gh_curl() {
API_GITHUB="https://api.github.com"
curl ${TOKEN:+-H "Authorization: token $TOKEN"} \
-H "Accept: application/vnd.github.v3.raw" \
-s $API_GITHUB$@
}
function gh_get_file() {
file="$1"
version="$2"
asset_id=`gh_curl /repos/$OWNER/$REPO/releases/tags/$version | jq ".assets | map(select(.name == \"$file\"))[0].id"`
if [ "$asset_id" = "null" ]; then
>&2 echo -e "\033[31mERROR: \"$file\" not found at \"$version\"\033[0m"
return 1
fi
wget -q --show-progress --auth-no-challenge --header='Accept:application/octet-stream' \
https://${TOKEN:+$TOKEN:@}api.github.com/repos/$OWNER/$REPO/releases/assets/$asset_id \
-O $file
return $?
}
if [ -z "$TOKEN" ]; then
>&2 echo -e "\033[33mWARNING: \$TOKEN is empty\033[0m"
fi
if [ "$VERSION" = "latest" ]; then
VERSION=`gh_curl /repos/$OWNER/$REPO/releases/latest | jq --raw-output ".tag_name"`
echo "Latest version is \"$VERSION\""
fi
mkdir -p $REPO-$VERSION
pushd $REPO-$VERSION > /dev/null
# Start by getting the checksums if they are available.
if ! gh_get_file SHA256SUMS $VERSION; then
>&2 echo -e "\033[33mWARNING: Checksums will NOT be computed\033[0m"
fi
if ! [ -z "$FILE" ]; then
if ! gh_get_file $FILE $VERSION; then
exit 1
fi
# Verify the sha256 sum for this file only.
grep "$FILE" SHA256SUMS | sha256sum --check
else
release_id=`gh_curl /repos/$OWNER/$REPO/releases/tags/$VERSION | jq ".id"`
assets=`gh_curl /repos/$OWNER/$REPO/releases/$release_id/assets | jq --raw-output '.[].name'`
for asset in $assets; do
if ! [ "$asset" = "SHA256SUMS" ]; then
if ! gh_get_file $asset $VERSION; then
exit 1
fi
fi
done
# Verify the sha256 sum for all files.
sha256sum --check SHA256SUMS
fi
popd > /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment