Skip to content

Instantly share code, notes, and snippets.

@No3x
Forked from maxim/gh-dl-release
Last active June 15, 2023 03:49
Show Gist options
  • Save No3x/2f4f71faf10e9cf2eb82483209169d85 to your computer and use it in GitHub Desktop.
Save No3x/2f4f71faf10e9cf2eb82483209169d85 to your computer and use it in GitHub Desktop.
Download assets from private Github releases
#!/usr/bin/env sh
# execute before running the script:
# export GITHUB_TOKEN='<mytoken>'
# usage:
# <org/repo> <filename> <version or 'latest'> <savefilename>
# e.g.
# <org/repo> file.pdf latest latest_version_file.pdf
if [ $# -lt 4 ] ;then
echo "Usage: <org/repo> <filename> <version or 'latest'> <savefilename>"
exit 1
fi
if [ -n "$GITHUB_TOKEN" ]; then
echo "GITHUB_TOKEN is set"
else
echo "GITHUB_TOKEN not set"
exit
fi
TOKEN="${GITHUB_TOKEN}"
REPO="$1"
ORG=$(echo $REPO | tr "/" "\n")
FILE="$2" # the name of your release asset file, e.g. build.tar.gz
VERSION=$3 # tag name or the word "latest"
SAVEFILENAME=$4
GITHUB_API_ENDPOINT="api.github.com"
alias errcho='>&2 echo'
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
RESPONSE=`curl -sL -H "Authorization: token $TOKEN" -H "Accept: application/vnd.github.v3.raw" https://$GITHUB_API_ENDPOINT/repos/$REPO/releases`
# echo "Response: $RESPONSE"
ASSET_ID=`echo "$RESPONSE" | jq "$PARSER"`
# echo "Asset ID: $ASSET_ID"
if [ "$ASSET_ID" = "null" ]; then
errcho "ERROR: version not found $VERSION"
exit 1
fi
# echo "Getting asset $ASSET_ID"
curl -sL -u "$ORG:$GITHUB_TOKEN" --header "Accept: application/octet-stream" https://api.github.com/repos/$REPO/releases/assets/$ASSET_ID > $SAVEFILENAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment