-
-
Save anupash147/6cf9e9b8a31c6ec1dfc759172a751421 to your computer and use it in GitHub Desktop.
Download assets from private Github releases
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 tagged version :\"$version\"\033[0m" | |
>&2 echo -e "Below are available versions in the format" | |
>&2 echo -e "[tags, executable1, executable 2, ....]" | |
gh_curl /repos/$OWNER/$REPO/releases | jq -c '.[] | [.tag_name, .assets[].name]' | |
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 $? | |
} | |
function remakedir() | |
{ | |
[[ -n "$1" ]] && rm -rf "$1" && mkdir -p "$1" | |
} | |
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 | |
remakedir dist | |
pushd dist | |
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