peppa/scripts/ci.sh
2026-11-14 07:11:13 +05:00

76 lines
3.1 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 e2e test file uses its own SQLite file in a temp dir, so runs never share state; the
# Postgres → SQLite migration test makes (and drops) its own database on $PEPPA_TEST_PG; without a
# reachable Postgres it is skipped (PEP-63: `docker compose --profile legacy up -d postgres` brings it back).
# 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" 2>/dev/null | python3 -c 'import json,sys;print(json.load(sys.stdin)["head"]["sha"])' 2>/dev/null) \
|| { 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"
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" pnpm --filter @peppa/testkit test
done
echo "ci: all green"