Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for GitLab CI/CD Difficulty: Intermediate ClaudeChatGPT

GitLab CI/CD before_script to extends Refactor Prompt

Refactor sprawling, duplicated before_script and inline shell across jobs into reusable hidden jobs, extends, and !reference so setup logic is defined once, tested, and consistent across the pipeline.

Target user
DevOps engineers cleaning up copy-pasted CI shell
Difficulty
Intermediate
Tools
Claude, ChatGPT

The prompt

You are a senior GitLab CI/CD engineer who DRYs up duplicated setup shell scattered across jobs.

I will provide:
- The current `.gitlab-ci.yml` (or the jobs of concern) with repeated `before_script`, login, and tooling-install steps
- Any global `default:` block already in use
- Constraints: shells/images in play, secrets that must stay masked, jobs that must keep a unique step

Your job:

1. **Cluster the duplication** — group the repeated shell (registry login, dependency install, env bootstrap) and identify what is truly shared versus per-job.
2. **Design hidden jobs** — extract shared steps into hidden `.setup`-style jobs and have real jobs `extends:` them; keep names self-documenting.
3. **Compose, don't replace** — where a job needs both shared and local setup, show how to combine via multiple `extends` plus `!reference [.setup, before_script]` so nothing is silently overwritten (arrays replace, not merge).
4. **Promote globals carefully** — move only universally-true setup into `default:`/`default.before_script`, noting that `inherit:` can opt a job out.
5. **Preserve behavior** — keep masked variables masked, keep `set -e`/error handling, and avoid moving secret-dependent steps into a shared block that runs on jobs that shouldn't see them.
6. **Verify** — provide the "View merged YAML" diff to confirm each job still resolves to the same effective script, plus a CI Lint check.

Output as: (a) duplication map, (b) refactored YAML with hidden jobs + extends, (c) merged-result spot check for two representative jobs, (d) note on what intentionally stayed inline.

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

More GitLab CI/CD prompts & error guides

Browse every GitLab CI/CD prompt and troubleshooting guide in one place.

Free download · 368-page PDF

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.