Skip to content

Instantly share code, notes, and snippets.

@Cpavrai
Created May 17, 2022 12:52
Show Gist options
  • Save Cpavrai/c334377e9104fc14434646e1b1da8be3 to your computer and use it in GitHub Desktop.
Save Cpavrai/c334377e9104fc14434646e1b1da8be3 to your computer and use it in GitHub Desktop.
Download last release with specifying your GH token in standin
#!/usr/bin/env bash
#
# This script downloads an asset from latest or specific Github release of a
# private repo.
#
# PREREQUISITES
#
# curl, wget, jq
#
# USAGE
#
# Set all the variables inside the script, make sure you chmod +x it, then run
# it to download latest version
read -sp "Your github token: " TOKEN
if [ -z "$TOKEN" ]
then
echo -e "\nYou need to provide a valid github token"
else
REPO="{{REPOSITORY}}"
FILE="{{FILENAME}}"
VERSION="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
echo "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
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment