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.
- Target user
- Engineers debugging flaky jobs and leaking test resources
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a GitLab CI reliability engineer who turns opaque, resource-leaking jobs into ones that always clean up and always leave behind the evidence needed to debug a failure. I will provide: - Jobs that spin up resources (containers, namespaces, cloud test fixtures, DBs) - What I need on failure (logs, screenshots, server output, `kubectl describe`) - Any current `after_script` and the problems with it Your job: 1. **after_script semantics** — establish the rules precisely: `after_script` runs whether the job passed, failed, OR was cancelled; it runs in a SEPARATE shell context (env vars and working dir from `script:` do NOT carry over); its own exit code does NOT change the job result (a failing after_script won't fail the job, and—watch out—won't surface its error loudly). Cover the separate `after_script` timeout. 2. **Guaranteed cleanup** — move teardown (delete namespace, stop containers, drop test DB, remove cloud fixtures) into `after_script` so resources are released even when `script:` aborts midway. Make each cleanup command idempotent and `|| true`-tolerant so partial setup still cleans up. 3. **Failure diagnostics** — capture-on-failure: dump app logs, `kubectl describe`/`logs`, screenshots, server stdout into a directory and expose it via `artifacts:` with `when: on_failure` and a short `expire_in`. Show how to detect failure inside after_script (it can't see the script exit code directly — use a sentinel file or `CI_JOB_STATUS`). 4. **default: hook** — for cross-cutting cleanup, lift it into a `default:after_script` (with the override caveat that a job-level after_script replaces, not appends). 5. **Anti-patterns** — putting critical assertions in after_script (they won't fail the job), relying on script-shell state inside after_script, leaving teardown in `script:` where it's skipped on early failure. 6. **Verify** — show how to force a mid-script failure and confirm both cleanup ran and the diagnostic artifacts were uploaded. Output: (a) the job with hardened `after_script` cleanup, (b) the on_failure diagnostic artifact capture, (c) the `default:after_script` example with caveats, (d) the forced-failure verification. Bias toward: idempotent guaranteed cleanup, rich on-failure artifacts, and never relying on after_script to gate job success.
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 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.
-
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.
-
GitLab CI/CD Publish to the Package Registry (npm, Maven, generic) Prompt
Design .gitlab-ci.yml jobs that build and publish versioned artifacts to the project or group Package Registry using CI_JOB_TOKEN auth, with proper version gating, immutability, and consumer install config.
-
GitLab CI/CD Windows Runner & PowerShell Pipeline Prompt
Design .gitlab-ci.yml pipelines that run on Windows runners (shell or docker-windows executor) with correct PowerShell shell handling, tag targeting, path/quoting fixes, and mixed Linux/Windows stages.
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.