Last active
December 23, 2019 14:32
-
-
Save deka108/334d074ca0daaa0f991be52fe2ec5e94 to your computer and use it in GitHub Desktop.
Download and Uploading Latest Release Assets
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 | |
# First, store your PERSONAL GITHUB TOKEN as secret file somewhere. | |
# You can generate your personal github token here: https://github.com/settings/tokens. Check the | |
# "Full control of private repositories" scope. | |
# In the example below, token is stored as a json file "github_token.json" under secrets directory in the form of: | |
# { "token": "PERSONAL_GITHUB_TOKEN" } | |
# Check Secrets | |
[[ -f secrets/github_token.json ]] || { echo "Error: github token is unavailable!" >&2; exit 1; } | |
GITHUB_API_TOKEN=`cat secrets/github_token.json | jq -r '.token'` | |
AUTH="Authorization: token $GITHUB_API_TOKEN" | |
# Read user inputs | |
read -p "Enter your github repo (eg: USER/REPO_NAME or ORG/REPO_NAME): " GITHUB_REPO && [[ -n $GITHUB_REPO ]] || { | |
echo "GITHUB_REPO must be set!" >&2; exit 1; | |
} | |
read -p "Enter asset filename: " ASSET_FILE && [[ -n $ASSET_FILE ]] || { | |
echo "ASSET_FILE must be set!" >&2; exit 1; | |
} | |
read -p "Upload/Download? (u/d): " operation && [[ $operation == [uUdD] ]] || { | |
echo "Operation must be either u (upload) or d (download)!" >&2; exit 1; | |
} | |
# Github related APIs | |
GITHUB_API="https://api.github.com" | |
GITHUB_REPO_API="$GITHUB_API/repos/$GITHUB_REPO" | |
GITHUB_LATEST_RELEASE_API="$GITHUB_REPO_API/releases/latest" | |
get_latest_release(){ | |
response=`curl -sH "$AUTH" "$GITHUB_LATEST_RELEASE_API"` | |
echo "$response" | |
} | |
get_asset_url(){ | |
asset_url=$(echo $1 | jq -r --arg name "$ASSET_FILE" '.assets[] | select(.name == $name).url') | |
echo "$asset_url" | |
} | |
get_upload_url(){ | |
upload_url=$(echo $1 | jq -r '.upload_url') | |
echo "$upload_url" | |
} | |
upload_asset(){ | |
response=`get_latest_release` | |
# Construct asset url | |
asset_url=`get_asset_url "$response"` | |
echo "Asset URL: $asset_url" | |
# Construct upload url | |
upload_url=`get_upload_url "$response"` | |
GITHUB_UPLOAD_ASSET_API=`echo $upload_url | sed "s/{?name,label}/?name=${ASSET_FILE}/g"` | |
echo "Upload URL: $GITHUB_UPLOAD_ASSET_API" | |
# Delete resource first if exist | |
[[ -n "$asset_url" ]] && { | |
echo "Deleting old resource" | |
curl -X DELETE -L# -H "$AUTH" $asset_url | |
} | |
echo "Uploading new resource" | |
curl --request PATCH -L# --data-binary @"$ASSET_FILE" -H "$AUTH" -H "Content-Type: application/octet-stream" $GITHUB_UPLOAD_ASSET_API | |
} | |
download_asset(){ | |
response=`curl -sH "$AUTH" "$GITHUB_REPO_API/releases/latest"` | |
asset_url=$(echo "$response" | jq -r --arg name "$ASSET_FILE" '.assets[] | select(.name == $name).url') | |
echo "Downloading $ASSET_FILE... from $asset_url" | |
curl -L# -H 'Accept: application/octet-stream' "$asset_url?access_token=$GITHUB_API_TOKEN" -o ${ASSET_FILE} | |
echo "Downloading $ASSET_FILE done!" | |
echo "Extracting ${ASSET_FILE}..." | |
if [[ -f ${ASSET_FILE} ]]; then | |
tar -xvzf ${ASSET_FILE}; | |
else | |
echo "Error: failure to download $ASSET_FILE!" >&2; | |
exit 1; | |
fi | |
} | |
# Check auth | |
curl -o /dev/null -sH "$AUTH" "$GITHUB_REPO_API" || { echo "Error: Invalid repo, token or network issue!" >&2; exit 1; } | |
if [[ $operation == [dD] ]]; then download_asset; else upload_asset; fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment