8. Wire it into CI
Two jobs: bring the preview up on demand, tear it down when the PR closes. Keep previews opt-in by label — most PRs don’t need one, and every live preview costs a database branch and a slice of a shared host.
name: preview
on: pull_request: types: [labeled, synchronize, closed]
jobs: up: # Deploy on the label, and on new commits once labelled. if: >- github.event.action != 'closed' && contains(github.event.pull_request.labels.*.name, 'preview') runs-on: [self-hosted, preview-host] concurrency: # never two lifecycle runs for one PR group: preview-${{ github.event.pull_request.number }} cancel-in-progress: false steps: - uses: actions/checkout@v4 # your repo: preview.yml, hooks, compose file - name: Install pstack run: | mkdir -p "$HOME/.local/bin" && echo "$HOME/.local/bin" >> "$GITHUB_PATH" curl -fsSL https://github.com/samishal1998/preview-stacks/releases/latest/download/install.sh \ | PSTACK_INSTALL_DIR="$HOME/.local/bin" sh
- name: Bring the preview up env: PR: ${{ github.event.pull_request.number }} GIT_SHA: ${{ github.event.pull_request.head.sha }} DB_API_TOKEN: ${{ secrets.DB_API_TOKEN }} run: pstack up -v
down: if: github.event.action == 'closed' runs-on: [self-hosted, preview-host] concurrency: group: preview-${{ github.event.pull_request.number }} cancel-in-progress: false steps: - uses: actions/checkout@v4 - name: Install pstack run: | mkdir -p "$HOME/.local/bin" && echo "$HOME/.local/bin" >> "$GITHUB_PATH" curl -fsSL https://github.com/samishal1998/preview-stacks/releases/latest/download/install.sh \ | PSTACK_INSTALL_DIR="$HOME/.local/bin" sh
- name: Tear the preview down env: PR: ${{ github.event.pull_request.number }} GIT_SHA: ${{ github.event.pull_request.head.sha }} DB_API_TOKEN: ${{ secrets.DB_API_TOKEN }} run: | set +e pstack down -v code=$? set -e case $code in 0) echo "preview torn down clean" ;; 2) echo "::error::preview LEAKED — resources survived teardown, fix the axis down hook" exit 1 ;; *) echo "::error::teardown failed (exit $code)" exit 1 ;; esacThe trap that eats exit codes. GitHub Actions runs run: blocks under bash -e, so a bare
pstack down returning 2 aborts the step before your case can read $?. Wrap the call in
set +e / set -e (or pstack down || code=$?). Get this wrong and every leak looks like a generic
step failure — which throws away the one distinction the exit codes exist to make.
Why the codes are worth branching on at all:
| Exit | Meaning | Who fixes it |
|---|---|---|
| 0 | torn down, nothing survived | nobody |
| 1 | teardown errored | whoever broke the hook or the runner |
| 2 | torn down but something survived | whoever owns that axis — and it needs cleaning up now, nothing else will |
| 3 | bad spec or usage | whoever edited preview.yml |
Notes on the rest of it:
if: always()on teardown, if the job does anything before it. A teardown that only runs on success is a leak generator.labeledfires for any label, so adding an unrelated label to a PR that already carriespreviewre-triggers the deploy. Harmless —uphooks are idempotent — but it is the answer to “why did my preview redeploy when I didn’t push anything”.concurrencyper PR mirrors the API’s one-job-per-stack lock. Without it, aclosedevent arriving during a redeploy races over the same resources. Do not setcancel-in-progress: trueon teardown — a cancelled teardown is a half-teardown.- Pass every variable the spec interpolates (
PR,GIT_SHA, …) and every secret the hooks need. A missing one is exit 3 at parse time, before anything is touched — which is the good failure. -vin CI, always. The log is the only forensics you get after the runner is gone.- Validate the spec on every PR — a cheap job on its own, needing neither Docker nor runner
privileges:
PR=0 pstack validate(addGIT_SHA=0000000or whatever else it interpolates). - The installer is the whole install — one static binary, no runtime — so this workflow runs on
a hosted runner too if your hooks and Docker do. Pin the version (
PSTACK_VERSION=<x.y.z>in front ofsh) if you want teardown to behave identically to the deploy that created the stack.