From ed9740a4c200e6800cd3ccb6c471bba8a6e1afe3 Mon Sep 17 00:00:00 2001 From: su-ex Date: Wed, 13 Jan 2021 21:46:39 +0100 Subject: [PATCH] Add script to deploy a release on GitHub --- deploy/create-github-release.sh | 80 +++++++++++++++++++++++++++ deploy/upload-github-release-asset.sh | 64 --------------------- deploy/upload_github.sh | 14 ----- 3 files changed, 80 insertions(+), 78 deletions(-) create mode 100755 deploy/create-github-release.sh delete mode 100755 deploy/upload-github-release-asset.sh delete mode 100755 deploy/upload_github.sh diff --git a/deploy/create-github-release.sh b/deploy/create-github-release.sh new file mode 100755 index 0000000..b93f7d5 --- /dev/null +++ b/deploy/create-github-release.sh @@ -0,0 +1,80 @@ +#!/usr/bin/env bash +# +# Based upon https://hinty.io/ivictbor/publish-and-upload-release-to-github-with-bash-and-curl/ +# and https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447 +# + +set -e +# set -x + +version="$1" +releasepath="$2" + +github_api_token=`cat ~/githubtoken` +release_notes_file="/tmp/scrn.md" + +owner=SchildiChat +repo=schildichat-desktop +target=master + +# Define variables +GH_API="https://api.github.com" +GH_REPO="$GH_API/repos/$owner/$repo" +AUTH="Authorization: token $github_api_token" + +# Validate token +curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; } + +# Get release notes +$EDITOR "$release_notes_file" +release_notes=`cat "$release_notes_file"` + +# Create draft release +echo "Create GitHub draft release ..." +json_string=`jq -n --arg tag "v$version" --arg target "$target" --arg body "$release_notes" '{ + tag_name: $tag, + target_commitish: $target, + name: $tag, + body: $body, + draft: true, + prerelease: false +}'` +# echo "$json_string" +res=`echo "$json_string" | curl -sH "$AUTH" $GH_REPO/releases -d @-` +# echo "$res" | jq "." + +# Get release id +id=`echo $res | jq ".id"` +# echo "id: $id" + +# Upload assets +find "$releasepath" -type f | while read filename; do + echo "" + echo "Uploading $filename ..." + + # Construct url + GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename $filename)" + + # Upload + res=`curl --progress-bar --data-binary @"$filename" -H "$AUTH" -H "Content-Type: application/octet-stream" $GH_ASSET` + state=`echo $res | jq ".state"` + if [ "$state" == "\"uploaded\"" ]; then + echo "Success!" + else + echo "Error:" + echo $res | jq "." + exit -1 + fi +done + +# Publish draft +res=`curl -sH "$AUTH" $GH_REPO/releases/$id -d '{"draft": false}'` +draft=`echo $res | jq ".draft"` +echo "" +if [ "$draft" == "false" ]; then + echo "Release v$version published on GitHub!" +else + echo "Error:" + echo $res | jq "." + exit -1 +fi diff --git a/deploy/upload-github-release-asset.sh b/deploy/upload-github-release-asset.sh deleted file mode 100755 index 206e3cd..0000000 --- a/deploy/upload-github-release-asset.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env bash -# -# Author: Stefan Buck -# License: MIT -# https://gist.github.com/stefanbuck/ce788fee19ab6eb0b4447a85fc99f447 -# -# -# This script accepts the following parameters: -# -# * owner -# * repo -# * tag -# * filename -# * github_api_token -# -# Script to upload a release asset using the GitHub API v3. -# -# Example: -# -# upload-github-release-asset.sh github_api_token=TOKEN owner=stefanbuck repo=playground tag=v0.1.0 filename=./build.zip -# - -# Check dependencies. -set -e -xargs=$(which gxargs || which xargs) - -# Validate settings. -[ "$TRACE" ] && set -x - -CONFIG=$@ - -for line in $CONFIG; do - eval "$line" -done - -# Define variables. -GH_API="https://api.github.com" -GH_REPO="$GH_API/repos/$owner/$repo" -GH_TAGS="$GH_REPO/releases/tags/$tag" -AUTH="Authorization: token $github_api_token" -WGET_ARGS="--content-disposition --auth-no-challenge --no-cookie" -CURL_ARGS="-LJO#" - -if [[ "$tag" == 'LATEST' ]]; then - GH_TAGS="$GH_REPO/releases/latest" -fi - -# Validate token. -curl -o /dev/null -sH "$AUTH" $GH_REPO || { echo "Error: Invalid repo, token or network issue!"; exit 1; } - -# Read asset tags. -response=$(curl -sH "$AUTH" $GH_TAGS) - -# Get ID of the asset based on given filename. -eval $(echo "$response" | grep -m 1 "id.:" | grep -w id | tr : = | tr -cd '[[:alnum:]]=') -[ "$id" ] || { echo "Error: Failed to get release id for tag: $tag"; echo "$response" | awk 'length($0)<100' >&2; exit 1; } - -# Upload asset -echo "Uploading asset... " - -# Construct url -GH_ASSET="https://uploads.github.com/repos/$owner/$repo/releases/$id/assets?name=$(basename $filename)" - -curl "$GITHUB_OAUTH_BASIC" --data-binary @"$filename" -H "Authorization: token $github_api_token" -H "Content-Type: application/octet-stream" $GH_ASSET diff --git a/deploy/upload_github.sh b/deploy/upload_github.sh deleted file mode 100755 index f18bbcb..0000000 --- a/deploy/upload_github.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -set -e -set -x - -DEPLOY_ROOT="$(dirname "$(realpath "$0")")" - -version="$1" -releasepath="$2" - -find "$releasepath" -type f | while read p; do - echo "Uploading $p ..." - $DEPLOY_ROOT/upload-github-release-asset.sh github_api_token=$GITHUB_TOKEN owner=SchildiChat repo=schildichat-desktop tag=v$version filename=$p -done \ No newline at end of file