#!/usr/bin/env bash # Publish dist/cst-agent-VERSION-windows-x86_64.zip (+ .sha256) to the Gitea # release vVERSION, creating the release (and its tag at HEAD) when missing. # GITEA_TOKEN=... scripts/release_windows.sh [--build] # --build runs scripts/bootstrap_windows.sh first. The token is only read from # the environment and handed to curl on stdin: it never appears in argv, on # disk or in the output. Assets of the same name are replaced. set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" SERVER="${CST_GITEA:-https://git.whcself.xyz}" REPO="${CST_REPO:-WHC/CST-agent}" API="$SERVER/api/v1/repos/$REPO" [[ -n "${GITEA_TOKEN:-}" ]] || { echo "set GITEA_TOKEN (a Gitea token with write:repository)" >&2 exit 1 } command -v python3 >/dev/null || { echo "missing python3" >&2; exit 1; } cd "$ROOT" [[ "${1:-}" == --build ]] && scripts/bootstrap_windows.sh "$ROOT" ver="$(sed -n 's/^#define CST_VERSION_\(MAJOR\|MINOR\|PATCH\) \([0-9]*\)$/\2/p' include/cst_version.h | paste -sd.)" tag="v$ver" zip="dist/cst-agent-$ver-windows-x86_64.zip" for f in "$zip" "$zip.sha256"; do [[ -f "$f" ]] || { echo "missing $f (scripts/package_windows.sh, or --build)" >&2; exit 1; } done api() { # METHOD PATH [curl args...] local m="$1" p="$2" shift 2 printf 'Authorization: token %s\n' "$GITEA_TOKEN" | curl -fsS -X "$m" -H @- "$API$p" "$@" } json() { # CODE [ARGS...]: d = the JSON on stdin, ARGS in sys.argv[1:] local code="$1" shift python3 -c "import json,sys; d=json.load(sys.stdin); $code" "$@" } if rel="$(api GET "/releases/tags/$tag" 2>/dev/null)"; then echo "==> release $tag exists" else head="$(git rev-parse HEAD)" if ! git branch -r --contains "$head" 2>/dev/null | grep -q .; then echo "HEAD $head is not on any remote branch; push it first" >&2 exit 1 fi echo "==> creating release $tag at $head" body="$(python3 -c 'import json,sys; print(json.dumps({"tag_name": sys.argv[1], "target_commitish": sys.argv[2], "name": "CST-Agent " + sys.argv[1], "body": "Windows x86_64: unzip, or install with scripts/install_windows.ps1"}))' "$tag" "$head")" rel="$(api POST /releases -H 'Content-Type: application/json' -d "$body")" fi id="$(json 'print(d["id"])' <<<"$rel")" for f in "$zip" "$zip.sha256"; do name="$(basename "$f")" old="$(json "print(' '.join(str(a['id']) for a in d.get('assets') or [] if a['name'] == sys.argv[1]))" "$name" <<<"$rel")" for aid in $old; do echo "==> replacing $name" api DELETE "/releases/$id/assets/$aid" >/dev/null done echo "==> upload $name" api POST "/releases/$id/assets?name=$name" -F "attachment=@$f" >/dev/null done echo "$SERVER/$REPO/releases/tag/$tag"