Files
Jyotisha/deploy/branch-flow.sh
T
ClaudeandClaude Opus 5.5 dcec206060
Independent Staging Quality Gate / validate-frontend (push) Successful in 6m34s
Independent Staging Quality Gate / validate-python (push) Successful in 8m3s
Independent Staging Quality Gate / publish (push) Successful in 3m17s
ci(release): branch release flow — next, release/x.y, hotfix, main; 封板 and 发版 workflows (BUG-2415)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
2026-11-26 11:53:14 +08:00

167 lines
6.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# BUG-2415: helpers for the branch release flow (next / release/x.y / hotfix/x.y.z / main).
#
# branch-flow.sh validate-version <x.y|x.y.z> exit 1 unless the value is a version
# branch-flow.sh bug-ids-ok stdin: commit messages separated by NUL;
# exit 1 naming each message without BUG-NNNN
# (merge commits and [release-bump] are exempt)
# branch-flow.sh commit-messages <base> <head> NUL-separated messages of base..head (Gitea compare API)
# branch-flow.sh ref-sha <branch> the branch head SHA, or empty when it does not exist
# branch-flow.sh tag-sha <tag> the tagged commit SHA, or empty when the tag does not exist
# branch-flow.sh prepare-git init a work tree whose origin pushes with RELEASE_PUSH_TOKEN
# branch-flow.sh fetch <ref>... fetch refs (runner mirror first, then origin)
# branch-flow.sh dispatch <workflow> <ref> <json-inputs> prints the new run id
# branch-flow.sh wait-run <run-id> <minutes> exit 0 on success, 1 on failure/timeout
# branch-flow.sh release-gate-passed <sha> exit 0 when a release-quality-gate run for sha succeeded
# branch-flow.sh migrations-changed <base> <head> exit 0 when base..head touches frontend/supabase/migrations/
#
# Network calls use GITEA_API_URL / GITEA_REPOSITORY / GITEA_TOKEN. Pushes use
# RELEASE_PUSH_TOKEN (a personal access token), because pushes made with the
# workflow's own token do not start other workflows.
set -euo pipefail
api_url="${GITEA_API_URL:-https://git.copse.top/api/v1}"
repository="${GITEA_REPOSITORY:-root/Jyotisha}"
clone_url="${GITEA_CLONE_URL:-https://git.copse.top/root/Jyotisha.git}"
mirror="${JYOTISHA_MIRROR:-/root/.cache/jyotisha-mirror.git}"
api() {
curl --fail --silent --show-error --connect-timeout 15 --max-time 60 --retry 3 --retry-all-errors \
--header "Authorization: token ${GITEA_TOKEN:?GITEA_TOKEN is required}" "$@"
}
validate_version() {
[[ "${1:-}" =~ ^[1-9][0-9]*\.[0-9]+(\.[0-9]+)?$ ]] || { echo "not a version: ${1:-}" >&2; return 1; }
}
bug_ids_ok() {
local bad=0 message first
while IFS= read -r -d '' message || [ -n "$message" ]; do
[ -n "$message" ] || continue
first="${message%%$'\n'*}"
case "$first" in
"Merge "*) continue ;;
esac
if [[ "$message" == *"[release-bump]"* ]]; then continue; fi
if ! grep -Eq 'BUG-[0-9]{3,5}' <<<"$message"; then
echo "missing BUG number: $first" >&2
bad=1
fi
done
return "$bad"
}
commit_messages() {
local base="$1" head="$2"
local response
response="$(api "$api_url/repos/$repository/compare/$base...$head")"
jq -e '(.total_commits // 0) == ((.commits // []) | length)' <<<"$response" >/dev/null || {
echo "compare $base...$head returned a partial commit list; cannot check BUG numbers" >&2
return 2
}
jq -j '.commits[]? | .commit.message + "\u0000"' <<<"$response"
}
ref_sha() {
local branch="$1" out
out="$(curl --silent --connect-timeout 15 --max-time 60 --retry 3 \
--header "Authorization: token ${GITEA_TOKEN:?}" \
--write-out '\n%{http_code}' "$api_url/repos/$repository/branches/$branch")"
case "${out##*$'\n'}" in
200) jq -r '.commit.id' <<<"${out%$'\n'*}" ;;
404) printf '' ;;
*) echo "could not read branch $branch" >&2; return 2 ;;
esac
}
tag_sha() {
local tag="$1" out
out="$(curl --silent --connect-timeout 15 --max-time 60 --retry 3 \
--header "Authorization: token ${GITEA_TOKEN:?}" \
--write-out '\n%{http_code}' "$api_url/repos/$repository/tags/$tag")"
case "${out##*$'\n'}" in
200) jq -r '.commit.sha' <<<"${out%$'\n'*}" ;;
404) printf '' ;;
*) echo "could not read tag $tag" >&2; return 2 ;;
esac
}
prepare_git() {
: "${RELEASE_PUSH_TOKEN:?RELEASE_PUSH_TOKEN secret is missing: see docs/operations/branch-flow.md}"
git init -q .
git remote remove origin 2>/dev/null || true
git remote add origin "$clone_url"
git config user.name "Jyotisha release bot"
git config user.email "release-bot@jyotisha.invalid"
git config http.extraHeader "Authorization: Basic $(printf 'release-bot:%s' "$RELEASE_PUSH_TOKEN" | base64 -w0)"
}
fetch_refs() {
local ref
if [ -d "$mirror" ]; then
timeout 300 git fetch -q --no-tags "$mirror" '+refs/heads/*:refs/remotes/mirror/*' || true
fi
for ref in "$@"; do
timeout 300 git fetch -q --no-tags origin "+refs/heads/$ref:refs/remotes/origin/$ref"
done
}
dispatch() {
local workflow="$1" ref="$2" inputs="$3" response run_id
response="$(api --request POST --header "Content-Type: application/json" \
--data "$(jq -cn --arg ref "$ref" --argjson inputs "$inputs" '{ref:$ref,inputs:$inputs}')" \
"$api_url/repos/$repository/actions/workflows/$workflow/dispatches?return_run_details=true")"
run_id="$(jq -er '.workflow_run_id | select(type == "number" and . > 0)' <<<"$response")"
printf '%s\n' "$run_id"
}
wait_run() {
local run_id="$1" minutes="$2" deadline state
deadline=$(( $(date +%s) + minutes * 60 ))
while [ "$(date +%s)" -lt "$deadline" ]; do
state="$(api "$api_url/repos/$repository/actions/runs/$run_id" | jq -r '[.status, (.conclusion // "")] | join(" ")')"
case "$state" in
"completed success") return 0 ;;
completed*) echo "run $run_id ended: $state" >&2; return 1 ;;
esac
sleep 30
done
echo "run $run_id did not finish within $minutes minutes" >&2
return 1
}
release_gate_passed() {
local sha="$1"
api "$api_url/repos/$repository/actions/runs?head_sha=$sha&limit=50" |
jq -e --arg sha "$sha" '[.workflow_runs[]? | select(.head_sha == $sha)
| select((.path | split("@")[0]) | endswith("release-quality-gate.yml"))
| select(.status == "completed" and .conclusion == "success")] | length > 0' >/dev/null
}
migrations_changed() {
local base="$1" head="$2"
# Gitea lists changed files per commit; a truncated listing counts as "changed" so the
# release never skips a migration it could not see.
api "$api_url/repos/$repository/compare/$base...$head" |
jq -e '((.total_commits // 0) != ((.commits // []) | length))
or any(.commits[]?; (.files == null)
or any(.files[]?; ((.filename // "") + " " + (.previous_filename // "")) | test("frontend/supabase/migrations/")))' >/dev/null
}
command="${1:-}"
shift || true
case "$command" in
validate-version) validate_version "$@" ;;
bug-ids-ok) bug_ids_ok ;;
commit-messages) commit_messages "$@" ;;
ref-sha) ref_sha "$@" ;;
tag-sha) tag_sha "$@" ;;
prepare-git) prepare_git ;;
fetch) fetch_refs "$@" ;;
dispatch) dispatch "$@" ;;
wait-run) wait_run "$@" ;;
release-gate-passed) release_gate_passed "$@" ;;
migrations-changed) migrations_changed "$@" ;;
*) echo "usage: branch-flow.sh <command> …" >&2; exit 2 ;;
esac