github.com/theQRL/go-zond@v0.1.1/hack/upload-github-release-asset.sh (about) 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 # skipcq: SH-2034 26 export 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 # skipcq: SH-2034 43 export WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie" 44 # skipcq: SH-2034 45 export CURL_ARGS="-LJO#" 46 47 if [[ "$tag" == 'LATEST' ]]; then 48 GH_TAGS="$GH_REPO/releases/latest" 49 fi 50 51 # Validate token. 52 curl -o /dev/null -sH "$AUTH" "$GH_REPO" || { echo "Error: Invalid repo, token or network issue!"; exit 1; } 53 54 # Read asset tags. 55 response=$(curl -sH "$AUTH" "$GH_TAGS") 56 57 # Get ID of the asset based on given filename. 58 eval "$(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=')" 59 [ "$id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; } 60 61 # Upload asset 62 echo "Uploading asset... " 63 64 # Construct url 65 GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename "$filename")" 66 67 echo "$GH_ASSET" 68 69 curl "$GITHUB_OAUTH_BASIC" --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "Content-Type: application/octet-stream" "$GH_ASSET"