From b705b7ab7e46b56338f913ed5339b800d585d7ff Mon Sep 17 00:00:00 2001 From: claude-bot <10+claude-bot@noreply.localhost> Date: Fri, 23 Oct 2026 23:51:07 +0800 Subject: [PATCH] CI: reuse unchanged work so a code-only deploy takes about 2 minutes instead of 8 The data build (download, ingest, work types: about 5 of the 8 minutes), npm ci, the Next.js compiler cache and the Postgres load are now cached under /opt/llm-survey/cache and redone only when their inputs change. Raw data is refreshed by a nightly run or by a manual run with refresh_data. Docs-only pushes and pushes already superseded by a newer commit on main skip the deploy. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01FbMbJPadPzVwvt9rU9GBNo --- .gitea/workflows/deploy.yml | 30 ++++++++++++++++++--- scripts/deploy.sh | 52 +++++++++++++++++++++++++++++++------ 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 8690f75..e051308 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -2,7 +2,17 @@ name: Deploy on: push: branches: [main] - workflow_dispatch: {} + paths-ignore: # documentation-only pushes do not change the site + - '**/*.md' + - 'docs/**' + schedule: + - cron: '17 19 * * *' # 03:17 Asia/Shanghai: re-download the raw paper data and rebuild it + workflow_dispatch: + inputs: + refresh_data: + description: 'Re-download the raw paper data and rebuild it (otherwise the cached export is reused)' + type: boolean + default: false jobs: deploy: @@ -10,12 +20,24 @@ jobs: timeout-minutes: 60 steps: - name: Checkout + id: checkout run: | - git init . + git init -q . git remote add origin "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git" - git -c http.extraHeader="Authorization: token ${{ github.token }}" fetch --depth 1 origin "$GITHUB_SHA" - git checkout --detach FETCH_HEAD + AUTH="Authorization: token ${{ github.token }}" + git -c http.extraHeader="$AUTH" fetch -q --depth 1 origin "$GITHUB_SHA" + git checkout -q --detach FETCH_HEAD + # a push that is already superseded by a newer one is not worth deploying: the newer run deploys everything + if [ "$GITHUB_EVENT_NAME" = push ]; then + LATEST=$(git -c http.extraHeader="$AUTH" ls-remote origin refs/heads/main | cut -f1) + if [ -n "$LATEST" ] && [ "$LATEST" != "$GITHUB_SHA" ]; then + echo "main has moved on to $LATEST; skipping the deploy of $GITHUB_SHA" + echo "skip=1" >> "$GITHUB_OUTPUT" + fi + fi - name: Deploy (systemd service, permanent) + if: steps.checkout.outputs.skip != '1' env: APP_PORT: ${{ vars.APP_PORT }} + REFRESH_DATA: ${{ github.event_name == 'schedule' || github.event.inputs.refresh_data == 'true' }} run: sh scripts/deploy.sh "$GITHUB_SHA" diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 8ce9023..f050f69 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -5,6 +5,12 @@ # 3. build the site, load the data into Postgres (schema "next" -> swapped in as "live") # 4. switch /opt/llm-survey/current, (re)start systemd unit `llm-survey`, health-check # Port is never 3000 or 18131 (taken). +# Caches under $ROOT/cache (a deploy that changes neither the data scripts nor the lockfile takes about 2 minutes, not 8): +# export/ the JSON-lines export, keyed by a hash of the data scripts + data/labels (rebuilt when they change, +# or when REFRESH_DATA=true: nightly run / manual run, which also re-download the raw sources) +# loaded id of the export now in Postgres schema "live" (the load is skipped when it matches) +# next Next.js compiler cache, linked into .next/cache +# node_modules is reused from the current release when package-lock.json and the node version are unchanged. set -eu SHA="${1:?usage: deploy.sh }" APP=llm-survey @@ -54,22 +60,52 @@ fi # ---- 2. release dir + data build ------------------------------------------------------ REL="$ROOT/releases/$SHA" -mkdir -p "$ROOT/releases" "$ROOT/cache/raw" +mkdir -p "$ROOT/releases" "$ROOT/cache/raw" "$ROOT/cache/export" "$ROOT/cache/next" rm -rf "$REL"; mkdir -p "$REL" tar --exclude=.git --exclude=node_modules --exclude=.next -cf - . | tar -xf - -C "$REL" cd "$REL" mkdir -p build data ln -sfn "$ROOT/cache/raw" data/raw # raw downloads survive between releases -[ -d data/raw/acl-anthology ] && git -C data/raw/acl-anthology pull --ff-only -q || true -bash scripts/fetch_data.sh -python3 scripts/ingest.py --raw data/raw --out build/papers.db -python3 scripts/build_worktypes.py --db build/papers.db -python3 scripts/export_jsonl.py --db build/papers.db --out build/export + +# the export only depends on the data scripts, the hand labels and the raw sources +KEY=$( { cat scripts/fetch_data.sh scripts/ingest.py scripts/taxonomy.py scripts/build_worktypes.py scripts/export_jsonl.py scripts/worktypes.json + find data/labels -type f | sort | xargs cat; } | sha256sum | cut -c1-16) +EXPORT="$ROOT/cache/export/$KEY" +if [ "${REFRESH_DATA:-false}" != true ] && [ -f "$EXPORT/.complete" ]; then + echo "data: reusing cached export $KEY (no data script changed; REFRESH_DATA is not set)" +else + echo "data: building export $KEY (refresh=${REFRESH_DATA:-false})" + [ -d data/raw/acl-anthology ] && git -C data/raw/acl-anthology pull --ff-only -q || true + bash scripts/fetch_data.sh + python3 scripts/ingest.py --raw data/raw --out build/papers.db + python3 scripts/build_worktypes.py --db build/papers.db + python3 scripts/export_jsonl.py --db build/papers.db --out build/export + rm -rf "$EXPORT"; cp -a build/export "$EXPORT"; touch "$EXPORT/.complete" + ls -1dt "$ROOT"/cache/export/* | tail -n +3 | xargs -r rm -rf # keep the two newest +fi +EXPORT_ID=$(find "$EXPORT" -type f ! -name .complete | sort | xargs cat | sha256sum | cut -c1-16) # ---- 3. site build + load into Postgres ---------------------------------------------- -npm ci --no-audit --no-fund +LOCK=$( { cat package-lock.json; node -v; } | sha256sum | cut -c1-16) +PREV=$(readlink -f "$ROOT/current" 2>/dev/null || true) +if [ -n "$PREV" ] && [ "$PREV" != "$REL" ] && [ "$(cat "$PREV/node_modules/.lockhash" 2>/dev/null)" = "$LOCK" ]; then + echo "node_modules: reusing the current release's (lockfile unchanged)" + cp -al "$PREV/node_modules" node_modules # hard links: seconds, no extra disk +else + npm ci --no-audit --no-fund + echo "$LOCK" > node_modules/.lockhash +fi +mkdir -p .next; ln -sfn "$ROOT/cache/next" .next/cache # compiler cache outlives the release +export NEXT_TELEMETRY_DISABLED=1 npm run build -node scripts/load-pg.js --dir build/export + +LIVE=$(PGPASSWORD=$PW psql -h 127.0.0.1 -p "$PGPORT" -U "$DB" -d "$DB" -qtAc "select 1 from information_schema.schemata where schema_name='live'" 2>/dev/null || true) +if [ "$LIVE" = 1 ] && [ "$(cat "$ROOT/cache/loaded" 2>/dev/null)" = "$EXPORT_ID" ]; then + echo "postgres: schema live already holds export $EXPORT_ID, load skipped" +else + node scripts/load-pg.js --dir "$EXPORT" + echo "$EXPORT_ID" > "$ROOT/cache/loaded" +fi rm -rf build # ---- 4. switch + restart --------------------------------------------------------------