aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/upload-github-release-asset.sh
diff options
context:
space:
mode:
Diffstat (limited to 'tools/upload-github-release-asset.sh')
-rwxr-xr-xtools/upload-github-release-asset.sh84
1 files changed, 84 insertions, 0 deletions
diff --git a/tools/upload-github-release-asset.sh b/tools/upload-github-release-asset.sh
new file mode 100755
index 0000000..0325530
--- /dev/null
+++ b/tools/upload-github-release-asset.sh
@@ -0,0 +1,84 @@
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# * overwrite (optional, could be ture, false, delete, default to be false)
16#
17# Script to upload a release asset using the GitHub API v3.
18#
19# Example:
20#
21# upload-github-release-asset.sh github_api_token=TOKEN owner=stefanbuck repo=playground tag=v0.1.0 filename=./build.zip overwrite=true
22#
23
24# Check dependencies.
25set -e
26xargs=$(which gxargs || which xargs)
27
28# Validate settings.
29[ "$TRACE" ] && set -x
30
31CONFIG=$@
32
33for line in $CONFIG; do
34 eval "$line"
35done
36
37# Define variables.
38GH_API="https://api.github.com"
39GH_REPO="$GH_API/repos/$owner/$repo"
40GH_TAGS="$GH_REPO/releases/tags/$tag"
41AUTH="Authorization: token $github_api_token"
42WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie"
43CURL_ARGS="-LJO#"
44
45if [[ "$tag" == 'LATEST' ]]; then
46 GH_TAGS="$GH_REPO/releases/latest"
47fi
48
49# Validate token.
50curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; }
51
52# Read asset tags.
53response=$(curl -sH "$AUTH" $GH_TAGS)
54
55# Get ID of the release.
56eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/release_id/')
57[ "$release_id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; }
58
59# Get ID of the asset based on given filename.
60# If exists, delete it.
61eval $(echo "$response" | grep -C2 "\"name\":.\+$(basename $filename)" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/asset_id/')
62if [ "$asset_id" = "" ]; then
63 echo "No need to overwrite asset"
64else
65 if [[ "$overwrite" == "true" ]] || [[ "$overwrite" == "delete" ]]; then
66 echo "Deleting asset($asset_id)... "
67 curl -X "DELETE" -H "Authorization: token $github_api_token" "https://api.github.com/repos/$owner/$repo/releases/assets/$asset_id"
68 if [[ "$overwrite" == "delete" ]]; then
69 exit 0
70 fi
71 else
72 echo "File already exists on tag $tag"
73 echo "If you want to overwrite it, set overwrite=true"
74 exit 1
75 fi
76fi
77
78# Upload asset
79echo "Uploading asset... "
80
81# Construct url
82GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$release_id/assets?name=$(basename $filename)"
83
84curl --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "Content-Type: application/octet-stream" $GH_ASSET