#!/usr/bin/env bash # # Publish a NEW Gitea release for this build and upload everything in builds/. # Never deletes existing releases/tags - each build gets its own tag. # # Config via env - in CI these come from the workflow's secrets: # GITEA_SERVER_URL, GITEA_REPOSITORY, PAT_GITEA # For a manual run, pass them inline rather than keeping a token on disk: # GITEA_SERVER_URL=... GITEA_REPOSITORY=... PAT_GITEA=... ./release.sh # Tag: $1 if given, else derived as v- # (a build suffix is appended if that tag already exists). # set -euo pipefail cd "$(dirname "$0")" : "${GITEA_SERVER_URL:?}" : "${GITEA_REPOSITORY:?}" : "${PAT_GITEA:?}" ROOT="$(git rev-parse --show-toplevel)" SHA="$(git rev-parse HEAD)" SHORT="$(git rev-parse --short HEAD)" OUT_DIR="$(pwd)/builds" REPO_API="${GITEA_SERVER_URL}/api/v1/repos/${GITEA_REPOSITORY}" AUTH=(-H "Authorization: token ${PAT_GITEA}") # Resolve a unique release tag: TAG="${1:-}" if [ -z "$TAG" ]; then VER="$(jq -r .version "$ROOT/httptoolkit-server/package.json")" TAG="v${VER}-${SHORT}" # If a release for this commit already exists (e.g. a scheduled rebuild of an # unchanged commit), keep the old one and make this tag unique instead. if curl -sf "${AUTH[@]}" "${REPO_API}/releases/tags/${TAG}" > /dev/null 2>&1; then TAG="${TAG}-${GITHUB_RUN_NUMBER:-$(date -u +%Y%m%d%H%M%S)}" fi fi echo "==> Creating release $TAG @ $SHORT" RESP=$(curl -s -X POST "${REPO_API}/releases" "${AUTH[@]}" \ -H "Content-Type: application/json" \ -d "{ \"tag_name\": \"${TAG}\", \"target_commitish\": \"${SHA}\", \"name\": \"HTTP Toolkit ${TAG}\", \"body\": \"Self-hosted single-file build from ${SHORT}.\", \"draft\": false, \"prerelease\": false }") RID=$(echo "$RESP" | jq -r '.id // empty') if [ -z "$RID" ]; then echo "!! Failed to create release:"; echo "$RESP"; exit 1 fi echo "==> Release id $RID" # Upload artifacts. shopt -s nullglob for f in "$OUT_DIR"/*; do [ -f "$f" ] || continue name=$(basename "$f") echo "==> Uploading $name" curl -s -X POST "${REPO_API}/releases/${RID}/assets?name=${name}" "${AUTH[@]}" \ -H "Content-Type: application/octet-stream" \ --data-binary "@${f}" > /dev/null done echo "==> Release complete."