From eb4540ccfabef2c1a862436dd9fe1d2270bd4f30 Mon Sep 17 00:00:00 2001 From: JJ Eaton <58495321+oldbettie@users.noreply.github.com> Date: Sat, 31 Oct 2026 21:55:38 +0700 Subject: [PATCH] feat(ci): make nightly runs the full suite on main Pull requests run only what their change affects. make nightly (scripts/nightly.sh) runs make ci, make test-flaky (new: go test -race -count=5 -shuffle=on against Postgres) and make secret-scan ARGS=--all in a detached checkout of origin/main. Green, it attests the commit when nothing has, so staging can catch up to it. Red, it opens or comments on a "Nightly failed on main" issue and exits 1; it never posts a failed local-ci, which would be final for the commit over what may be a flake. It runs from cron on the dev box (docs/CI.md "Nightly"), so it needs no Actions. Co-Authored-By: Claude Opus 5.5 --- Makefile | 18 +++++++++++ docs/CI.md | 13 ++++++++ scripts/nightly.sh | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100755 scripts/nightly.sh diff --git a/Makefile b/Makefile index 345b398..f34d74f 100644 --- a/Makefile +++ b/Makefile @@ -107,6 +107,24 @@ else endif $(COVERAGE) +# The flake hunt make nightly runs: every test five times, in a shuffled +# order, with -race (no coverage or time budgets: five runs of each test). +FLAKY := $(GO) test -race -count=5 -shuffle=on $(TEST_PKGS) + +.PHONY: test-flaky +test-flaky: ## go test -race -count=5 -shuffle=on against this checkout's throwaway Postgres (make nightly runs it) +ifdef TEST_DATABASE_URL + $(FLAKY) +else + @$(MAKE) --no-print-directory test-db-up + @port=$$($(COMPOSE) port postgres-test 5432 | sed 's/.*://'); \ + TEST_DATABASE_URL="postgres://postgres:postgres@127.0.0.1:$$port/postgres?sslmode=disable" $(FLAKY) +endif + +.PHONY: nightly +nightly: ## The full suite on origin/main: make ci, test-flaky, secret-scan --all; attests green, opens an issue on red (docs/CI.md "Nightly") + ./scripts/nightly.sh + .PHONY: ci ci: ## Every check, on this checkout as it is (make ci-local then attests a PR head) $(GO) build ./... diff --git a/docs/CI.md b/docs/CI.md index 0bc17bb..fa198bb 100644 --- a/docs/CI.md +++ b/docs/CI.md @@ -43,6 +43,19 @@ The probes (`make lint-layering`, `make lint-tests`) prove the layering and test `scripts/vulncheck/allowlist.txt` is the only way past a finding: an advisory with no fix, or none its consumer can take, that we cannot reach, or reach only in dev tooling (expo CLI, eslint) or on one client device; the reason says which. Each line is ` ` (the GO- id for Go, the GHSA id for npm). The check fails on an entry past its review date, on an entry that no longer matches anything, and on a listed Go advisory that our code calls. Before adding a Go entry, prove the package is not imported (`go list -deps -test ./...` in each module). +### Nightly + +A pull request runs only what its change affects, so `make nightly` runs everything on `main` once a day: `make ci`, `make test-flaky` (`go test -race -count=5 -shuffle=on` against Postgres) and `make secret-scan ARGS=--all`, in a detached checkout of `origin/main` (`.cache/nightly`, scripts/nightly.sh). + +- **Green:** it attests the commit (`make ci-local COMMIT=`) when nothing has, so "Staging catch-up" can ship it. +- **Red:** it opens a "Nightly failed on main" issue (or comments on the open one) with the failing step and the log's tail, keeps the checkout for a look, and exits 1. It never posts a failed `local-ci`: that is final for the commit, and a flake would keep it off staging. A failure is fixed forward like any other on `main`. + +It runs from cron on the dev box (the machine with Docker, `gh` and the `CI_RUNNER` runner), not as a workflow, so it needs no Actions minutes and runs where Actions is off: + + 0 3 * * * cd ~/src/go-fast-stack && make nightly >> ~/.cache/nightly.log 2>&1 + +`NIGHTLY_REPORT=0 make nightly` runs it with no fetch and no GitHub calls (`NIGHTLY_COMMIT=` picks the commit). + ## Local verification `make ci-local` (`scripts/cilocal run`) runs no checks: it tells the PR you ran what CI would have checked, and says what that was. Run what the change needs ([What to run](#what-to-run)), commit, push, then attest: diff --git a/scripts/nightly.sh b/scripts/nightly.sh new file mode 100755 index 0000000..cb486d7 --- /dev/null +++ b/scripts/nightly.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +# The nightly full suite on main (`make nightly`, docs/CI.md "Nightly"): +# everything a pull request may have skipped by running only what it +# affected. In a detached checkout of origin/main (.cache/nightly) it runs +# make ci, make test-flaky (-race -count=5 -shuffle=on) and +# make secret-scan ARGS=--all. +# +# Green: attests the commit (make ci-local COMMIT=) when nothing has yet, +# so staging can catch up to it. Red: opens a "Nightly failed on main" issue, +# or comments on the open one, keeps the checkout for a look, and exits 1. It +# never posts a failed local-ci: that is final for the commit and would keep +# it off staging over what may be a flake. +# +# Run it from cron on a machine with Docker and gh (docs/CI.md "Nightly"). +# NIGHTLY_COMMIT= checks another commit; NIGHTLY_REPORT=0 skips the fetch +# and every GitHub call (attest, issue). +set -euo pipefail +# shellcheck disable=SC2046 # one name per word +unset $(git rev-parse --local-env-vars) +cd "$(dirname "$0")/.." +root=$PWD +report=${NIGHTLY_REPORT:-1} +title="Nightly failed on main" + +if [ "$report" != 0 ]; then git fetch -q origin main; fi +sha=$(git rev-parse --verify "${NIGHTLY_COMMIT:-origin/main}^{commit}") +dir=$root/.cache/nightly +log=$root/.cache/nightly.log +mkdir -p "$root/.cache" +if [ -e "$dir" ]; then git worktree remove --force "$dir"; fi +git worktree prune +git worktree add -q --detach "$dir" "$sha" +: >"$log" +echo "nightly: $sha in $dir (log: $log)" + +steps=("make ci" "make test-flaky" "make secret-scan ARGS=--all") +failed="" +start=$(date +%s) +for step in "${steps[@]}"; do + echo "== $step" | tee -a "$log" + if ! (cd "$dir" && bash -c "$step") >>"$log" 2>&1; then + failed=$step + break + fi +done +took=$(($(date +%s) - start)) + +if [ -z "$failed" ]; then + echo "nightly: ok in ${took}s" + if [ "$report" != 0 ]; then + state=$(gh api "repos/{owner}/{repo}/commits/$sha/status" --jq '[.statuses[] | select(.context == "local-ci")][0].state // ""') + if [ -z "$state" ]; then + (cd "$dir" && make --no-print-directory ci-local COMMIT="$sha" RAN="make nightly: $(printf '%s; ' "${steps[@]}" | sed 's/; $//')") + else + echo "nightly: $sha already has local-ci $state; nothing to attest" + fi + fi + git worktree remove --force "$dir" + exit 0 +fi + +echo "nightly: FAIL at '$failed' after ${took}s; the checkout stays at $dir" +tail -40 "$log" +if [ "$report" != 0 ]; then + body=$(printf '`%s` failed on %s (`make nightly`, %ss).\n\nLast lines of the log (%s on %s):\n\n```\n%s\n```\n' \ + "$failed" "$sha" "$took" "$log" "$(hostname)" "$(tail -60 "$log")") + issue=$(gh issue list --state open --search "in:title \"$title\"" --json number,title \ + --jq "[.[] | select(.title == \"$title\")][0].number // \"\"") + if [ -n "$issue" ]; then + gh issue comment "$issue" --body "$body" + else + gh issue create --title "$title" --body "$body" + fi +fi +exit 1