Skip to content

Instantly share code, notes, and snippets.

@ozkriff
Created September 16, 2017 21:08
Show Gist options
  • Save ozkriff/af1c11ab67bfd2fc8ec1c75dcef7c59b to your computer and use it in GitHub Desktop.
Save ozkriff/af1c11ab67bfd2fc8ec1c75dcef7c59b to your computer and use it in GitHub Desktop.
#!/bin/sh
# Script to upload a release asset using the GitHub API v3.
#
# Initial author: Stefan Buck - https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447
#
# Example:
# upload-github-release-asset.sh github_api_token=TOKEN owner=stefanbuck repo=playground tag=v0.1.0 filename=./build.zip
# Check dependencies.
set -e
CONFIG=$@
echo $CONFIG
for line in $CONFIG; do
eval "$line"
done
GH_REPO="https://api.github.com/repos/$owner/$repo"
GH_TAGS="$GH_REPO/releases/tags/$tag"
AUTH="Authorization: token $github_api_token"
# Validate token.
curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; }
# Create a release
RELEASE_URL="https://api.github.com/repos/$owner/$repo/releases?access_token=$github_api_token"
curl --data "{\"tag_name\": \"$tag\"}" $RELEASE_URL
# 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\n" >&2; exit 1; }
GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename $filename)"
# Upload the asset
curl --data-binary @"$filename" -H "$AUTH" -H "Content-Type: application/octet-stream" $GH_ASSET
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment