Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./...
Expand Down
13 changes: 13 additions & 0 deletions docs/CI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<id> <package> <review-by> <reason>` (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=<sha>`) 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=<rev>` 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:
Expand Down
75 changes: 75 additions & 0 deletions scripts/nightly.sh
Original file line number Diff line number Diff line change
@@ -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=<sha>) 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=<rev> 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
Loading