Skip to content

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 ;;
esac

The 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.
  • labeled fires for any label, so adding an unrelated label to a PR that already carries preview re-triggers the deploy. Harmless — up hooks are idempotent — but it is the answer to “why did my preview redeploy when I didn’t push anything”.
  • concurrency per PR mirrors the API’s one-job-per-stack lock. Without it, a closed event arriving during a redeploy races over the same resources. Do not set cancel-in-progress: true on 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.
  • -v in 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 (add GIT_SHA=0000000 or 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 of sh) if you want teardown to behave identically to the deploy that created the stack.