Files
httptoolkit/.build/prod/release.sh
T
shihaam 762b25e510
Build & Release binary / build (push) Canceled after 0s
ci: cron
2026-08-15 02:43:35 +05:00

69 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Publish a NEW Gitea release for this build and upload everything in out/.
# Never deletes existing releases/tags - each build gets its own tag.
#
# Config via env (or a local .env next to this script):
# GITEA_SERVER_URL, GITEA_REPOSITORY, PAT_GITEA
# Tag: $1 if given, else derived as v<server-version>-<shortsha>
# (a build suffix is appended if that tag already exists).
#
set -euo pipefail
cd "$(dirname "$0")"
# Optional: load credentials from a local .env for manual runs.
[ -f .env ] && source ./.env
: "${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)/out"
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."