GitLab CI/CD environment:auto_stop_in Ephemeral Cleanup Prompt
Auto-expire and tear down ephemeral environments using environment:auto_stop_in and on_stop jobs, so review apps and dynamic stacks don't leak cost or orphaned resources.
- Target user
- DevOps engineers managing review apps and dynamic environments
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior platform engineer who keeps ephemeral environments cheap and self-cleaning, so no orphaned namespaces, DNS records, or cloud resources survive a merged or stale MR.
I will provide:
- My deploy job and `environment:` block
- Where ephemeral stacks live (K8s namespace, ECS, Cloud Run, Heroku-style)
- How environments are currently torn down (manually? never?)
- Cost pain points and orphan examples
Your job:
1. **Lifecycle model** — map the full ephemeral lifecycle: create on MR open → update on push → auto-stop after inactivity → delete environment record. Explain how `environment.action: start | stop | prepare` ties jobs together.
2. **`auto_stop_in` wiring** — show the deploy job setting `environment: { name, url, on_stop, auto_stop_in: "2 days" }` and the matching `on_stop` job with `environment: { name, action: stop }`, `when: manual`, and `rules` that allow auto-trigger. Clarify that the timer starts/refreshes on each successful deploy.
3. **The teardown job** — write the `on_stop` job body that actually destroys resources (kubectl delete namespace, helm uninstall, terraform destroy, or cloud CLI), including `GIT_STRATEGY: none` considerations and how it gets the right env vars after the branch may be gone.
4. **Stop-on-merge / stop-on-close** — show using `CI_MERGE_REQUEST_EVENT_TYPE` and pipeline rules so the environment also stops when the MR merges or closes, not only on the timer.
5. **Belt-and-suspenders sweeper** — a scheduled pipeline that lists environments via the GitLab API (`/environments?states=available`), finds ones past TTL or tied to closed MRs, and stops them — to catch anything the inline timer missed.
6. **Observability** — surface active ephemeral environments and their age, and alert when count or cost crosses a threshold.
7. **Validation** — a test plan proving: timer fires, on_stop runs, resources are gone, and the environment shows "stopped" in the UI.
Output as: (a) full `.gitlab-ci.yml` deploy + on_stop jobs, (b) the scheduled sweeper script, (c) the destroy commands per platform I named, (d) a runbook for manually stopping a stuck environment.
Bias toward: idempotent teardown, no orphaned cloud resources, cost visibility.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
GitLab CI/CD after_script Cleanup & Failure Diagnostics Prompt
Use after_script correctly for guaranteed cleanup and on-failure diagnostics — tearing down test infra, capturing logs/screenshots as artifacts, and avoiding the traps that make after_script silently swallow failures.
-
GitLab CI/CD Protected Environments & Deployment Approvals Prompt
Lock down production with protected environments, deployment approval rules (multiple approvers, required groups), and deployment-only access so a green pipeline can't ship to prod without the right humans signing off.
-
GitLab CI/CD Tiered Environment Promotion Flow Prompt
Design a promote-the-same-artifact-through-tiers flow (dev → staging → prod) where one build is promoted across environments rather than rebuilt, with the right rules:, manual gates, and environment:deployment_tier mapping.
-
GitLab CI/CD k6 Load Test Performance Gate Prompt
Design a .gitlab-ci.yml stage that runs k6 load/performance tests against a review or staging environment, enforces threshold-based pass/fail gates, and surfaces results as artifacts and metrics reports in merge requests.
More GitLab CI/CD prompts & error guides
Browse every GitLab CI/CD prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.