Skip to content

3. Add your first axis

An axis is one stateful resource Compose knows nothing about. Four hooks, and the asymmetry between them is the whole product:

Hook Contract On failure
up provision. Must be idempotent — it re-runs on every redeploy. fatal, stops the run
assert_live exit 0 ⇒ the resource exists fatal — catches an up that lied
down destroy recorded, never fatal
assert_gone exit 0 ⇒ the resource is gone fatal for verify → exit 2

up fails fast because a half-provisioned stack should not proceed to deploy. down never aborts because stopping halfway leaves more garbage than continuing. verify is strict because a teardown that silently half-worked is the failure this tool exists to catch.

Delete-and-recreate rather than reuse: a reused branch still carries the previous commit’s migrations, which is how preview schemas silently drift from main.

version: 1
stack: pr-${PR}
compose:
file: docker-compose.preview.yml
profiles: [backend, frontend]
axes:
- name: database
up: |
./hooks/db-branch.sh recreate "$STACK"
echo "DATABASE_URL=$(./hooks/db-branch.sh url "$STACK")"
assert_live: ./hooks/db-branch.sh count "$STACK" | grep -qx 1
down: ./hooks/db-branch.sh delete "$STACK"
assert_gone: |
./hooks/db-branch.sh ping || exit 1
[ "$(./hooks/db-branch.sh count "$STACK")" = "0" ]

Two mechanics to notice:

  • $STACK is exported to every hook. A hook never reconstructs pr-${PR} itself.
  • KEY=VALUE on up‘s stdout is captured and passed to Compose (and to later axes’ hooks) as an env var. That’s how a freshly created database hands over its connection string with no temp file. Only SHOUT_CASE keys are captured, so ordinary log chatter on stdout is ignored.
Terminal window
$ PR=123 pstack validate
stack: pr-123
✓ spec parses — kind: isolated, 1 axis/axes, stack "pr-123"
compose: docker-compose.preview.yml [backend, frontend]
- database: up, down, assert_gone, assert_live

Each axis line lists the hooks it actually defines. A missing hook here is a gap in coverage — read this line as a checklist.

validate warns about the three ways an axis lies

Section titled “validate warns about the three ways an axis lies”

These are the mistakes that produce a green teardown over a leaking host. validate still exits 0 — they are warnings, not errors — but treat them as a to-do list.

axes:
- name: dns-record # (1) no assert_gone
up: ./hooks/dns.sh create "$STACK"
down: ./hooks/dns.sh delete "$STACK"
- name: queue-namespace # (2) bare `! <probe>`
up: ./hooks/queue.sh create "$STACK"
down: ./hooks/queue.sh delete "$STACK"
assert_gone: '! ./hooks/queue.sh describe "$STACK"'
- name: images # (3) `|| true` in an assert
down: ./hooks/images.sh prune "$STACK"
assert_gone: |
docker info >/dev/null 2>&1 || exit 1
! docker images | grep -q "$STACK" || true
Terminal window
$ PR=123 pstack validate
stack: pr-123
✓ spec parses — kind: isolated, 3 axis/axes, stack "pr-123"
- dns-record: up, down
- queue-namespace: up, down, assert_gone
- images: down, assert_gone
! axis "dns-record" has `up` but no `assert_gone` — `pstack verify` cannot prove it was cleaned up.
! axis "queue-namespace": `assert_gone` is a bare `! <probe>` with no reachability guard — it will report "gone" if the probe itself fails. Fail closed instead:
<probe-is-usable> || exit 1
! <probe-for-this-resource>
! axis "images": `assert_gone` contains `|| true`, which makes it always pass. Tolerate failure in `down`, never in an assert.

(1) up without assert_gone — the axis can be created but never proven gone. verify reports it unverifiable rather than passing it silently (section 4).

(2) A bare ! <probe> fails open, and this is the subtle one. ! inverts the exit code, so the assert passes whenever the probe fails for any reason — CLI not installed, token expired, API unreachable. “I could not tell” becomes “it is gone”. Watch it happen: the hook below does not even exist, and verify still reports the axis clean.

Terminal window
$ PR=123 pstack verify -f lie.yml # assert_gone: '! ./hooks/does-not-exist.sh describe "$STACK"'
stack: pr-123
→ verify (asserting resources are gone)
✓ assert_gone queue-namespace
$ echo $?
0

Fail closed instead — prove the probe can answer, then assert absence:

assert_gone: |
./hooks/queue.sh ping >/dev/null 2>&1 || exit 1 # can't tell ⇒ FAIL, don't guess
! ./hooks/queue.sh describe "$STACK"

Same broken hook, honest answer:

Terminal window
$ PR=123 pstack verify -f closed.yml
stack: pr-123
→ verify (asserting resources are gone)
✗ assert_gone queue-namespace — LEAKED: resource still present after teardown
1 leaked resource(s)
$ echo $?
2

The message says “still present”; read it as “could not be proven gone”, which covers both cases. Erring toward “leaked” is the correct bias — a false alarm costs you a look, a false all-clear costs you the host.

(3) || true forces exit 0, so the assert can never fail. It belongs on down (best-effort) and never on an assert.

Warning (2) is deliberately narrow, so a guarded assert does not trip it: a multi-line script, or a single line containing exit, || or &&, is assumed to handle its own failure modes. Warnings (1) and (3) have no such escape — (1) is purely “is there an assert_gone at all”, and (3) matches || true anywhere in the script.

Worth wiring up even when it feels redundant. A multi-line up exits with the status of its last command, so a failure mid-block is invisible unless you add || exit 1 — which is exactly the shape assert_live catches:

Terminal window
$ PR=6 pstack up -f faillive.yml
stack: pr-6
→ up: database
✓ up database
✗ assert_live database — provisioned but assert_live failed — resource missing?
$ echo $?
1

Without it, that stack deploys and surfaces as an opaque connection error inside the app ten minutes later.

../examples/preview.yml is a fully-commented four-axis stack — database, queue namespace, images, ingress — with the fail-closed pattern applied to each. Two of those axes exist because Compose won’t do the job:

  • images — docker compose down -v removes containers, volumes and networks but never images, so per-PR layers accumulate until the disk fills, and disk pressure then evicts the warm build cache that is your real per-PR speed lever.
  • ingress — nothing to provision (routing comes from labels, TLS from a wildcard cert), so the axis is assert_gone only: it exists purely to prove the hostname stopped answering.

Axes are provisioned top to bottom and destroyed bottom to top. Declare a dependency before whatever depends on it.