Files
ai-for-dummies/.husky/pre-push
T
Marcos Paulo dc6cb5a0a3
verify-and-publish / gate (push) Successful in 22m13s
verify-and-publish / publish (push) Has been skipped
ci: publish to pages from a pre-push hook
Pushing main now rebuilds the site and force-pushes dist/ to pages.

.agents/scripts/publish-pages.sh does the work. It never checks pages
out: it writes a tree straight from dist/ with write-tree and
commit-tree, so the working tree is untouched and a failure halfway
through leaves nothing behind. The commit is parented on the current
pages tip, so the branch keeps its history and a rollback is one
force-push to an earlier commit -- which the script prints before it
pushes.

It refuses to publish when the working tree is dirty, when HEAD is not
main, when HEAD is not the commit being pushed, or when any of the ten
routes is missing or empty in dist/. A build can succeed and still emit a
stub; that is exactly how this site would go down.

The hook guards three ways. AF_PUBLISHING short-circuits it so the
publisher's own push does not re-enter it forever. AF_NO_PUBLISH=1 lets
you push main without publishing. And because git has no post-push hook,
the publish necessarily runs before main lands -- so it first checks that
the remote tip is an ancestor of what is being pushed, and skips
publishing when the push could still be rejected as a non-fast-forward.

Also rewrites the operations guide's rollback section, which still
described merging main into pages with --ff-only. That has not been true
since pages started carrying build output.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-06 09:06:21 +00:00

41 lines
1.6 KiB
Plaintext

# Tier 2: the real gate. Whole project. Budget < 90s.
# Takes a cross-worktree lock so parallel agents queue instead of thrashing.
# The publish step below re-enters git push. Without this, that inner push would
# fire this hook again, run the gate again, and publish again, forever.
if [ "${AF_PUBLISHING:-0}" = '1' ]; then
exit 0
fi
.agents/scripts/gate.sh || exit 1
# Publishing to `pages` overwrites the live site. It happens here, on a push of
# main to origin, and nowhere else.
#
# Set AF_NO_PUBLISH=1 to push main without republishing:
# AF_NO_PUBLISH=1 git push
[ "${AF_NO_PUBLISH:-0}" = '1' ] && exit 0
remote_name=$1
[ "$remote_name" = 'origin' ] || exit 0
# stdin gives one line per ref being pushed:
# <local ref> <local sha> <remote ref> <remote sha>
zero='0000000000000000000000000000000000000000'
while read -r local_ref local_sha remote_ref remote_sha; do
[ "$remote_ref" = 'refs/heads/main' ] || continue
# A deletion has no build to publish.
[ "$local_sha" = "$zero" ] && continue
# This hook runs before the push lands, so `pages` would go live ahead of
# `main` if the push then failed. Publish only when the push cannot be
# rejected as a non-fast-forward: the remote tip must already be an ancestor.
if [ "$remote_sha" != "$zero" ] && ! git merge-base --is-ancestor "$remote_sha" "$local_sha"; then
echo "pre-push: main is not a fast-forward; not publishing." >&2
echo " Push main first, then run .agents/scripts/publish-pages.sh" >&2
continue
fi
.agents/scripts/publish-pages.sh --pending "$local_sha" || exit 1
done