Last active
December 28, 2020 09:46
-
-
Save xoascf/525b58c632818bfaf705f373ce6be8a5 to your computer and use it in GitHub Desktop.
Upload an asset to GitHub with basic Linux utilities
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/sh | |
# NAME: upload-github-asset.sh | |
# DESC: Uploads an asset to GitHub | |
# DATE: Dec 27, 2020. Modified Dec 27, 2020. | |
# ORIGINAL: https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447 | |
# Get args | |
owner=$1 | |
repo=$2 | |
tag=$3 | |
github_api_token=$4 | |
filename=$5 | |
filename_out=$6 | |
content_type=$7 | |
# GitHub vars | |
GH_API="https://api.github.com" | |
GH_REPO="$GH_API/repos/$owner/$repo" | |
GH_TAGS="$GH_REPO/releases/tags/$tag" | |
AUTH="Authorization: token $github_api_token" | |
CONTENT="Content-Type: $content_type" | |
if [ "$tag" = 'LATEST' ]; then | |
GH_TAGS="$GH_REPO/releases/latest" | |
fi | |
# Validate token | |
curl -o /dev/null -sH "$AUTH" "$GH_REPO" || { echo "Error: Invalid repo, token or network issue!"; exit 1; } | |
# Read asset tags | |
response=$(curl -sH "$AUTH" "$GH_TAGS") | |
# Get ID of the asset based on given filename | |
eval "$(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=')" | |
[ "$id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; } | |
# Upload asset | |
echo "Uploading asset... " | |
GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename "$filename_out")" | |
curl "$GITHUB_OAUTH_BASIC" --data-binary @"$filename" -H "$AUTH" -H "$CONTENT" "$GH_ASSET" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment