diff options
Diffstat (limited to 'scripts/upload-github-release-asset.sh')
| -rwxr-xr-x | scripts/upload-github-release-asset.sh | 84 |
1 files changed, 0 insertions, 84 deletions
diff --git a/scripts/upload-github-release-asset.sh b/scripts/upload-github-release-asset.sh deleted file mode 100755 index 0325530..0000000 --- a/scripts/upload-github-release-asset.sh +++ /dev/null | |||
| @@ -1,84 +0,0 @@ | |||
| 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. | ||
| 25 | set -e | ||
| 26 | xargs=$(which gxargs || which xargs) | ||
| 27 | |||
| 28 | # Validate settings. | ||
| 29 | [ "$TRACE" ] && set -x | ||
| 30 | |||
| 31 | CONFIG=$@ | ||
| 32 | |||
| 33 | for line in $CONFIG; do | ||
| 34 | eval "$line" | ||
| 35 | done | ||
| 36 | |||
| 37 | # Define variables. | ||
| 38 | GH_API="https://api.github.com" | ||
| 39 | GH_REPO="$GH_API/repos/$owner/$repo" | ||
| 40 | GH_TAGS="$GH_REPO/releases/tags/$tag" | ||
| 41 | AUTH="Authorization: token $github_api_token" | ||
| 42 | WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie" | ||
| 43 | CURL_ARGS="-LJO#" | ||
| 44 | |||
| 45 | if [[ "$tag" == 'LATEST' ]]; then | ||
| 46 | GH_TAGS="$GH_REPO/releases/latest" | ||
| 47 | fi | ||
| 48 | |||
| 49 | # Validate token. | ||
| 50 | curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; } | ||
| 51 | |||
| 52 | # Read asset tags. | ||
| 53 | response=$(curl -sH "$AUTH" $GH_TAGS) | ||
| 54 | |||
| 55 | # Get ID of the release. | ||
| 56 | eval $(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. | ||
| 61 | eval $(echo "$response" | grep -C2 "\"name\":.\+$(basename $filename)" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=' | sed 's/id/asset_id/') | ||
| 62 | if [ "$asset_id" = "" ]; then | ||
| 63 | echo "No need to overwrite asset" | ||
| 64 | else | ||
| 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 | ||
| 76 | fi | ||
| 77 | |||
| 78 | # Upload asset | ||
| 79 | echo "Uploading asset... " | ||
| 80 | |||
| 81 | # Construct url | ||
| 82 | GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$release_id/assets?name=$(basename $filename)" | ||
| 83 | |||
| 84 | curl --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "Content-Type: application/octet-stream" $GH_ASSET | ||