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.

Related prompts

Newsletter

Free: the DevOps AI Incident-Triage Cheat Sheet

Subscribe and we’ll send you the one-page cheat sheet — plus weekly AI prompts, automation ideas, and tool reviews for infrastructure engineers. One email a week. No spam, unsubscribe anytime.

  • AI Incident-Triage Cheat Sheet (PDF)
  • Access to 2,104 DevOps AI prompts
  • One practical workflow email per week