diff options
author | typebrook <typebrook@gmail.com> | 2019-05-30 16:29:13 +0800 |
---|---|---|
committer | typebrook <typebrook@gmail.com> | 2019-05-30 16:29:13 +0800 |
commit | 3dcc891f4c3f68c3ce39cdf013bc4f011bf48a76 (patch) | |
tree | 4fad05d5b92d36d9a014631e8e2d218d07319609 | |
parent | 9602a9799d72d9dca5a901bc37a906e378b7e9cf (diff) |
update
-rw-r--r-- | scripts/upload-github-release-asset.sh | 64 |
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. | ||
24 | set -e | ||
25 | xargs=$(which gxargs || which xargs) | ||
26 | |||
27 | # Validate settings. | ||
28 | [ "$TRACE" ] && set -x | ||
29 | |||
30 | CONFIG=$@ | ||
31 | |||
32 | for line in $CONFIG; do | ||
33 | eval "$line" | ||
34 | done | ||
35 | |||
36 | # Define variables. | ||
37 | GH_API="https://api.github.com" | ||
38 | GH_REPO="$GH_API/repos/$owner/$repo" | ||
39 | GH_TAGS="$GH_REPO/releases/tags/$tag" | ||
40 | AUTH="Authorization: token $github_api_token" | ||
41 | WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie" | ||
42 | CURL_ARGS="-LJO#" | ||
43 | |||
44 | if [[ "$tag" == 'LATEST' ]]; then | ||
45 | GH_TAGS="$GH_REPO/releases/latest" | ||
46 | fi | ||
47 | |||
48 | # Validate token. | ||
49 | curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; } | ||
50 | |||
51 | # Read asset tags. | ||
52 | response=$(curl -sH "$AUTH" $GH_TAGS) | ||
53 | |||
54 | # Get ID of the asset based on given filename. | ||
55 | eval $(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 | ||
59 | echo "Uploading asset... " | ||
60 | |||
61 | # Construct url | ||
62 | GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename $filename)" | ||
63 | |||
64 | curl "$GITHUB_OAUTH_BASIC" --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "Content-Type: application/octet-stream" $GH_ASSET | ||