Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save stupidloud/783b6f8aadcdd0b408d6b032a71af557 to your computer and use it in GitHub Desktop.

Select an option

Save stupidloud/783b6f8aadcdd0b408d6b032a71af557 to your computer and use it in GitHub Desktop.
Script to upload a release asset using the GitHub API v3.
#!/usr/bin/env bash
#
# Author: Stefan Buck
# License: MIT
# https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447
#
#
# This script accepts the following parameters:
#
# * owner
# * repo
# * tag
# * filename
# * github_api_token
#
# Script to upload a release asset using the GitHub API v3.
#
# Example:
#
# upload-github-release-asset.sh github_api_token=TOKEN owner=stefanbuck repo=playground tag=v0.1.0 filename=./build.zip
#
set -e
CONFIG=$@
for line in $CONFIG; do
eval "$line"
done
REPO_PATH="github.com/repos/$owner/$repo/releases"
AUTH="Authorization: token $github_api_token"
ret=$(curl -sH "$AUTH" "https://api.$REPO_PATH/tags/$tag")
#echo $ret | jq -r '.assets[] | select(.name == "'$filename").id' | \
echo $ret | jq -r '.assets[] | select(.name | contains ("'$filename'")).id' | \
xargs -n1 -i curl -X DELETE -H "$AUTH" "https://api.$REPO_PATH/assets/{}"
eval $(echo "$ret" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=')
curl --data-binary @$filename -H "$AUTH" -H "Content-Type: application/octet-stream" "https://uploads.$REPO_PATH/$id/assets?name=$filename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment