peppa/scripts/ci.sh
Асель 56a7756e71 PEP-22: scripts/ci.sh, PR status peppa/ci, mock-act-runner
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 15:00:28 +05:00

77 lines
3.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# CI for Peppa itself (PEP-22): typecheck, web build, e2e on mocks.
#
# scripts/ci.sh checks of this checkout (also `pnpm run ci`)
# scripts/ci.sh --pr <N> checks of PR head N in peppa-bot/peppa in a temp worktree,
# with commit status `peppa/ci` in Gitea (pending → success/failure)
# scripts/ci.sh --repeat <N> run the e2e suite N times in a row (flake hunting)
# scripts/ci.sh --skip-build skip `next build`
#
# Every run uses its own database peppa_test_ci_<pid> (dropped at exit), so it can run next
# to other worktrees' suites. Logs of --pr runs: ~/.peppa/ci/<sha>.log.
set -uo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
PR="" REPEAT=1 BUILD=1
while [ $# -gt 0 ]; do
case "$1" in
--pr) PR=$2; shift 2 ;;
--repeat) REPEAT=$2; shift 2 ;;
--skip-build) BUILD=0; shift ;;
*) echo "unknown argument: $1" >&2; exit 2 ;;
esac
done
if [ -n "$PR" ]; then
. "$ROOT/scripts/lib/ci-env.sh"
. "$ROOT/scripts/gitea-lib.sh"
REPO="${PEPPA_CI_REPO:-peppa-bot/peppa}"
CONTEXT="peppa/ci"
SHA=$(gitea_api "$GITEA_URL/api/v1/repos/$REPO/pulls/$PR" | python3 -c 'import json,sys;print(json.load(sys.stdin)["head"]["sha"])') \
|| { echo "PR $PR not found in $REPO" >&2; exit 2; }
status() { # <state> <description>
gitea_api -X POST "$GITEA_URL/api/v1/repos/$REPO/statuses/$SHA" \
-d "{\"state\":\"$1\",\"context\":\"$CONTEXT\",\"description\":\"$2\"}" >/dev/null \
|| echo "warning: could not set commit status" >&2
}
mkdir -p "$HOME/.peppa/ci"
LOG="$HOME/.peppa/ci/$SHA.log"
WT="$(mktemp -d)/peppa-ci"
git -C "$ROOT" fetch -q origin "refs/pull/$PR/head" || { echo "cannot fetch PR $PR" >&2; exit 2; }
git -C "$ROOT" worktree add -q --detach "$WT" "$SHA" || exit 2
trap 'git -C "$ROOT" worktree remove --force "$WT" >/dev/null 2>&1' EXIT
ln -s "$ENV_FILE" "$WT/.env" 2>/dev/null
status pending "running typecheck, build, e2e"
start=$(date +%s)
args=(--repeat "$REPEAT"); [ "$BUILD" = 0 ] && args+=(--skip-build)
(cd "$WT" && pnpm install --frozen-lockfile --prefer-offline >/dev/null && bash scripts/ci.sh "${args[@]}") >"$LOG" 2>&1
rc=$?
took=$(( ($(date +%s) - start) / 60 ))
if [ $rc = 0 ]; then
status success "passed in ${took} min"
else
status failure "$(grep -E '^ci: (FAIL|ok)' "$LOG" | tail -1 | cut -c5-) — log $LOG"
fi
tail -20 "$LOG"
echo "PR $PR @ ${SHA:0:8}: $([ $rc = 0 ] && echo success || echo failure), log $LOG"
exit $rc
fi
cd "$ROOT"
DB="peppa_test_ci_$$"
cleanup() { docker exec peppa-postgres dropdb -U peppa --if-exists "$DB" >/dev/null 2>&1; }
trap cleanup EXIT
step() { # <name> <command...>
local name=$1; shift
echo "ci: ▶ $name"
local t=$(date +%s)
if "$@"; then echo "ci: ok $name ($(( $(date +%s) - t ))s)"; else echo "ci: FAIL $name"; exit 1; fi
}
step typecheck pnpm -r typecheck
[ "$BUILD" = 1 ] && step "web build" pnpm --filter @peppa/web build
for i in $(seq 1 "$REPEAT"); do
step "e2e $i/$REPEAT" env PEPPA_TEST_DB="$DB" pnpm --filter @peppa/testkit test
done
echo "ci: all green"