Lesson 4 of 9 · 27m
Ephemeral Preview Environments That Do Not Become Pets
Provision short-lived review environments with deterministic lifecycle, data policy, cost controls, and cleanup that actually runs.
Preview environments are one of the highest-leverage CI/CD features and one of the easiest ways to build a cloud-cost swamp. The first version always impresses people: open a pull request, receive a URL, click the real feature. The second month reveals unmanaged databases, leaked fixtures, orphaned DNS records, and three environments named pr-7-final2. Treat previews as disposable infrastructure with a lifecycle, not as demo snowflakes.
Define the lifecycle contract
Every environment needs clear transitions:
opened create namespace and deploy commit
updated redeploy same identity with new commit
idle scale down expensive components
closed destroy compute, data, DNS, secrets
expired destroy even if the pull request stayed openThe environment identity should be stable, such as pr-184, while the deployed revision changes. Do not create a new namespace for every push unless your isolation model requires it; unstable URLs destroy review continuity.
A practical deployment job
preview-deploy:
runs-on: ubuntu-24.04
environment: preview
steps:
- uses: actions/checkout@v4
- run: |
helm upgrade --install "pr-${{ github.event.number }}" ./chart --namespace "pr-${{ github.event.number }}" --create-namespace --set image.tag="${{ github.sha }}" --set ingress.host="pr-${{ github.event.number }}.preview.internal" --wait --timeout 5mThis is only safe when the runner identity is constrained to preview namespaces. CI should not hold production cluster administrator rights merely because YAML made it convenient.
Data is the hard part
Production data does not belong in preview environments. Use synthetic fixtures, anonymized snapshots, or small seeded datasets generated at deployment time. Be explicit about external integrations. Stripe test mode is acceptable; a live payment key in a preview pod is a breach report waiting for its date.
Cleanup is a first-class job
Closing the pull request should trigger destruction, but webhooks fail. Add scheduled reconciliation:
kubectl get namespaces -l app=preview -o json |
jq -r '.items[] | select(.metadata.creationTimestamp < (now - 604800 | strftime("%Y-%m-%dT%H:%M:%SZ"))) | .metadata.name'Then compare live namespaces with open pull requests and destroy orphans. Keep cleanup logs auditable. Nobody trusts automation that silently deletes evidence.
Control cost and abuse
Set resource quotas, maximum lifetimes, scale-to-zero rules, and namespace count limits. Public forks may need approval before provisioning because previews can become crypto-mining or phishing infrastructure. Add a robots policy, authentication, or restricted ingress when content is sensitive.
Field notes
- Do not print environment URLs containing signed tokens into public pull-request comments.
- Migrations against shared databases defeat isolation; prefer per-environment databases with seeded schemas.
- Make teardown idempotent. The second cleanup run should be boring.
- Preview readiness is product evidence: seed users, data, and feature flags so reviewers can actually exercise the change.
Was this lesson helpful?