diff --git a/scripts/publish-release.sh b/scripts/publish-release.sh new file mode 100755 index 0000000..277e970 --- /dev/null +++ b/scripts/publish-release.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: GPL-3.0-or-later +# +# Publish the Forgejo release of a motorino version: create the release for the tag +# vVERSION (semantic-release has already pushed it) and upload dist/motorino-linux-amd64 +# and its .sha256 file as assets. +# +# Usage: scripts/publish-release.sh VERSION +# Set PUBLISH_DRY_RUN=1 to print what would be done without calling the server. +# +# The Forgejo password is read from Bitwarden and handed to curl on its standard input, +# so it never shows in the process list or in the output. +set -euo pipefail + +version="${1:?usage: publish-release.sh VERSION}" +api="https://git.kmalloc.org/api/v1/repos/squid/motorino" +bw_item="341a1b64-4984-4c33-8a31-b50000bd2a76" # Bitwarden "Forgejo git.kmalloc.org (squid)" +binary="dist/motorino-linux-amd64" + +for file in "$binary" "$binary.sha256"; do + [ -s "$file" ] || { echo "publish-release: $file is missing, run 'make dist VERSION=$version'" >&2; exit 1; } +done + +if [ "${PUBLISH_DRY_RUN:-}" = 1 ]; then + echo "would create the release v$version on $api" + echo "would upload $binary and $binary.sha256" + exit 0 +fi + +password="$(bw get password "$bw_item" --session "$(cat "$HOME/.bwsession")")" + +# call runs curl against the API with the credentials taken from standard input. +call() { + printf 'user = "squid:%s"\n' "$password" | curl --fail-with-body --silent --show-error -K - "$@" +} + +# id_of prints the id of the release JSON read from standard input. +id_of() { + python3 -c 'import json, sys; print(json.load(sys.stdin)["id"])' +} + +body="motorino $version, linux/amd64. The SHA-256 of the binary is in the .sha256 file." +payload="$(python3 -c 'import json, sys; print(json.dumps({"tag_name": "v" + sys.argv[1], "name": "v" + sys.argv[1], "body": sys.argv[2]}))' "$version" "$body")" + +if release="$(call -H 'Content-Type: application/json' -X POST "$api/releases" -d "$payload" 2>/dev/null)"; then + id="$(id_of <<<"$release")" +else + # The release may exist already (a second run): reuse it. + id="$(call "$api/releases/tags/v$version" | id_of)" +fi + +for file in "$binary" "$binary.sha256"; do + call -X POST "$api/releases/$id/assets?name=$(basename "$file")" -F "attachment=@$file" >/dev/null + echo "uploaded $(basename "$file")" +done +echo "release v$version published"