Developers want a preview environment for every pull request. How would you build it?
AdvancedScenarioMulti-env and multi-account
Answer
Situation read → Previews speed up review, but without automatic cleanup and cost limits they become hundreds of forgotten environments.
What I'd do →
- One namespace per PR in a shared non-prod cluster, created by an Argo CD ApplicationSet with the pull request generator.
generators:
- pullRequest:
github:
owner: acme
repo: shop
labels: ["preview"]
requeueAfterSeconds: 300
template:
metadata:
name: 'shop-pr-{{number}}'
spec:
destination:
namespace: 'pr-{{number}}'
- Each preview gets a hostname like
pr-123.preview.acme.devvia wildcard DNS and a wildcard certificate. - Dependencies: lightweight in-namespace databases with seed data, or shared staging backends for heavy services.
- Lifecycle: deleted automatically when the PR closes (the generator removes the Application), and a TTL for stale ones.
- Limits: ResourceQuota per namespace, Spot nodes, and scale to zero after inactivity.
Why → The pull request generator ties the environment's lifetime to the PR, so cleanup is automatic.
Interview tip → Lead with cleanup and cost; that's where preview environments usually fail.
More on Multi-env and multi-account
Design the AWS account structure for a 50-engineer startup that's growing fast.IntermediateHow do you promote the same build from dev to staging to prod?BeginnerBugs keep passing staging and appearing in production. How do you make staging closer to prod?IntermediateHow do you structure Terraform for dev, staging and prod? Workspaces, directories or Terragrunt?IntermediateHelm values files or Kustomize overlays for per-environment config?BeginnerCI runs in a shared tools account and must deploy to dev, staging and prod accounts. How do you set up access?Intermediate