Add script to deploy a release on GitHub

This commit is contained in:
su-ex 2021-01-13 21:46:39 +01:00
parent cdd8ec775c
commit ed9740a4c2
No known key found for this signature in database
GPG Key ID: D743C50C8B61984C
3 changed files with 80 additions and 78 deletions

80
deploy/create-github-release.sh Executable file
View File

@ -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

View File

@ -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

View File

@ -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