aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rw-r--r--scripts/upload-github-release-asset.sh64
1 files changed, 64 insertions, 0 deletions
diff --git a/scripts/upload-github-release-asset.sh b/scripts/upload-github-release-asset.sh
new file mode 100644
index 0000000..206e3cd
--- /dev/null
+++ b/scripts/upload-github-release-asset.sh
@@ -0,0 +1,64 @@
1#!/usr/bin/env bash
2#
3# Author: Stefan Buck
4# License: MIT
5# https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447
6#
7#
8# This script accepts the following parameters:
9#
10# * owner
11# * repo
12# * tag
13# * filename
14# * github_api_token
15#
16# Script to upload a release asset using the GitHub API v3.
17#
18# Example:
19#
20# upload-github-release-asset.sh github_api_token=TOKEN owner=stefanbuck repo=playground tag=v0.1.0 filename=./build.zip
21#
22
23# Check dependencies.
24set -e
25xargs=$(which gxargs || which xargs)
26
27# Validate settings.
28[ "$TRACE" ] && set -x
29
30CONFIG=$@
31
32for line in $CONFIG; do
33 eval "$line"
34done
35
36# Define variables.
37GH_API="https://api.github.com"
38GH_REPO="$GH_API/repos/$owner/$repo"
39GH_TAGS="$GH_REPO/releases/tags/$tag"
40AUTH="Authorization: token $github_api_token"
41WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie"
42CURL_ARGS="-LJO#"
43
44if [[ "$tag" == 'LATEST' ]]; then
45 GH_TAGS="$GH_REPO/releases/latest"
46fi
47
48# Validate token.
49curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; }
50
51# Read asset tags.
52response=$(curl -sH "$AUTH" $GH_TAGS)
53
54# Get ID of the asset based on given filename.
55eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=')
56[ "$id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; }
57
58# Upload asset
59echo "Uploading asset... "
60
61# Construct url
62GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename $filename)"
63
64curl "$GITHUB_OAUTH_BASIC" --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "Content-Type: application/octet-stream" $GH_ASSET