Last active
November 11, 2024 09:33
-
-
Save maxim/6e15aa45ba010ab030c4 to your computer and use it in GitHub Desktop.
Download assets from private Github releases
This file contains 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
#!/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: | |
# | |
# gh-dl-release 2.1.1 my_app.tar.gz | |
# | |
# to download latest version: | |
# | |
# gh-dl-release latest latest.tar.gz | |
# | |
# If your version/tag doesn't match, the script will exit with error. | |
TOKEN="<github_access_token>" | |
REPO="<user_or_org>/<repo_name>" | |
FILE="<name_of_asset_file>" # the name of your release asset file, e.g. build.tar.gz | |
VERSION=$1 # 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 -q --auth-no-challenge --header='Accept:application/octet-stream' \ | |
https://$TOKEN:@api.github.com/repos/$REPO/releases/assets/$asset_id \ | |
-O $2 |
Here are the official docs for anyone else who stumbles on this thread which say to do (note octet-stream
here bc I was downloading a binary):
curl -L \
-H "Accept: application/octet-stream" \
-H "Authorization: Bearer <YOUR-TOKEN>"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/releases/assets/ASSET_ID
If (like me) you're confused about the best way to find out ASSET_ID
I'd suggest looking at the json output for the latest release, see the docs here and the code here:
curl -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <YOUR-TOKEN>"\
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/OWNER/REPO/releases/latest
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For releases, uses
gh release download
instead.for example.