Killing Our Shared Staging Server: How We Built On-Demand Ephemeral Environments for Every PR

Killing Our Shared Staging Server: How We Built On-Demand Ephemeral Environments for Every PR

If you’ve ever worked on a mid-to-large engineering team, you know the pain of the shared staging environment.In theory, staging is supposed to be a pristine replica of production where developers and QA engineers verify changes before going live. In reality, it’s a digital warzone. Team A deploys a half-baked database migration, Team B pushes a breaking API change, and Team C’s feature branch gets stuck behind a three-day merge freeze because nobody knows whose commit caused the 502 Bad Gateway errors.Six months ago, our shared staging environment officially became our biggest engineering bottleneck. So, we killed it.Instead of one monolithic staging server that everyone fought over, we built an automated platform workflow that spins up an isolated, production-like ephemeral environment for every single Pull Request , and destroys it the moment the PR closes.Here is why we made the jump, how we architected it, and the painful lessons we learned when our AWS bill briefly tried to bankrupt us.Why We Did It: The Breaking PointBy early last year, we had scaled to six product squads pushing dozens of commits a day, all funneling into a single shared Kubernetes staging cluster and a shared RDS database.The symptoms of our broken workflow were impossible to ignore:Database Migration Collisions: If two engineers opened PRs that altered the same table schema, staging would enter a corrupted state that required a manual database restore.The "Staging Queue" Slack Channel: Developers literally had to post "Claiming staging for 2 hours, please don't merge!" in a dedicated Slack channel just to run end-to-end tests.False Positives in QA: QA engineers wasted hours filing bug reports against Feature X, only to realize the bug was actually caused by Feature Y deployed ten minutes earlier.We didn't have a testing problem; we had a concurrency problem. Shared environments fundamentally break down once your team scales past a certain velocity. We needed true isolation.How We Built It: The ArchitectureOur goal was simple: When a developer opens a PR, they get a dedicated URL (pr-142.preview.internal.dev) within 5 minutes, with zero manual setup.To avoid rebuilding our entire infrastructure from scratch, we stitched together tools we already trusted:1. Kubernetes Namespaces & Helm for ComputeInstead of provisioning a brand-new EKS cluster per PR (which takes 15+ minutes and costs a fortune), we spun up a dedicated Preview Cluster and isolated each PR into its own Kubernetes namespace (pr-).Using ArgoCD ApplicationSets with the Pull Request Generator, ArgoCD automatically watches our GitHub repositories.The moment a PR is labeled preview (or opened against main), ArgoCD renders our Helm charts into a fresh namespace, builds the PR's Docker image, and deploys the microservice alongside lightweight mocks or shared read-only dependencies.2. Dynamic DNS & TLS AutomationAn environment is useless if you have to port-forward to reach it. We configured ExternalDNS and cert-manager on the preview cluster. As soon as the Ingress resource spins up in pr-142, ExternalDNS automatically creates a Route53 CNAME record, and cert-manager provisions a wildcard TLS certificate.3. GitHub Actions as the OrchestratorWhen the environment is healthy, a GitHub Action posts a comment directly on the PR with:The live preview URLLinks to scoped Grafana logs and traces for that namespaceA one-click "Rebuild Environment" triggerHow We Overcame the Three Biggest HeadachesBuilding the happy-path prototype took two weeks. Making it survive real-world developer behavior took two months. Here is what went wrong—and how we fixed it.Problem 1: Database Seeding Took 25 Minutes Per PRSpinning up stateless containers took 90 seconds. Spinning up a fresh Postgres pod and running 400 historical migrations plus a 10GB anonymized seed script took nearly half an hour. Developers hated waiting, so they stopped using the previews.How We Fixed It: We stopped building databases from scratch on every PR. Instead, we set up a nightly cron job that builds a pre-migrated, pre-seeded golden volume snapshot in AWS EBS. When a new PR environment launches, it mounts a clone of that snapshot using copy-on-write storage. Database boot time dropped from 25 minutes to 45 seconds, and we only run the new delta migrations introduced in that specific PR.Problem 2: Our Cloud Bill Spiked by 40% in Month OneDevelopers are great at opening PRs and terrible at closing them. By week three, we had 85 active preview environments running 24/7—including over the weekend, for stale PRs nobody was actively reviewing.How We Fixed It: We built an Auto-Reaper CronJob and enforced aggressive lifecycle policies:Idle Scale-to-Zero: Using KEDA (Kubernetes Event-driven Autoscaling), if a preview environment receives zero HTTP traffic for 2 hours, its deployments scale down to 0 replicas. When someone clicks the PR link again, KEDA wakes the pods up in ~15 seconds.48-Hour TTL: Any preview environment attached to a PR with no new commits for 48 hours is automatically torn down. Adding a new commit or commenting /preview spins it right back up.Problem 3: Cross-Service Dependencies ("Where's the Auth Service?")Our architecture has 18 microservices. Spinning up all 18 services for every single PR would have melted our cluster nodes.How We Fixed It: Hybrid Routing with Service Mesh (Istio/Linkerd). By default, a PR environment only deploys the specific microservice modified in that PR. All outbound API calls to other services (like Auth, Billing, or Notifications) are transparently routed via header-based routing to a stable, shared baseline namespace running the latest main branch versions of those services. If a feature spans two repositories, developers simply pass a custom X-Preview-Env: pr-142 header to chain multiple preview services together.The ResultsSix months after decommissioning our shared staging server, the impact on engineering velocity has been night and day:Deployment Lead Time: Dropped by 62%, because PRs are thoroughly tested in isolation before merging to main, eliminating broken builds on the primary branch.Staging Bottlenecks: Reduced to zero. We archived the #staging-queue Slack channel.Cloud Spend: Once we implemented KEDA scale-to-zero and EBS snapshot cloning, our entire preview cluster cost 18% less than our old, oversized, always-on staging infrastructure.Killing your staging server feels terrifying at first. You lose that comforting sense of having "one canonical place" to look at pre-production code. But once your team experiences the freedom of testing every single pull request in a clean, isolated sandbox without stepping on anyone else's toes, you'll wonder how you ever shipped code without it.

Original Source

Read the full article at Hackernoon →

KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.