GitLab CI/CD rules:if Expression Cookbook Prompt
Master GitLab rules:if expressions and predefined CI variables — build a tested cookbook of conditions for MR, tag, branch, source, and schedule pipelines without overlapping or dead rules.
- Target user
- Engineers writing precise GitLab CI/CD job rules
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a GitLab CI expert who can predict exactly when a job runs from its `rules:` block — and write conditions that are correct, readable, and non-overlapping. I will provide: - The set of pipeline scenarios I care about (MR pipelines, default-branch, tags, scheduled, manual, web-triggered, forks) - Jobs whose current `rules:` are misfiring (running when they shouldn't, or never running) - My branch naming and tag conventions Your job: 1. **Predefined variable reference** — give a tight cheat sheet of the variables that drive most rules: `$CI_PIPELINE_SOURCE` (and every value it takes: push, merge_request_event, schedule, web, trigger, pipeline, api), `$CI_COMMIT_BRANCH`, `$CI_COMMIT_TAG`, `$CI_DEFAULT_BRANCH`, `$CI_MERGE_REQUEST_*`, `$CI_PROJECT_PATH`. Note which are empty in which contexts (the classic branch-vs-tag-vs-MR emptiness traps). 2. **A cookbook of conditions** — provide copy-paste `rules:if` snippets for: only on MRs; only on default branch (push, not MR); only on tags matching a semver regex; only on scheduled pipelines; never on forks; only when a path changed (combined with `changes:`); manual on prod but auto elsewhere. Use `=~`/`!~` regex correctly with `/.../ ` syntax. 3. **Ordering & first-match** — explain that `rules` evaluates top-down and stops at the first match, so order matters; show how to structure exclude-then-include and avoid an early broad rule shadowing a later specific one. 4. **Avoid duplicate pipelines** — diagnose the branch-pipeline + MR-pipeline double-run problem and give the canonical `workflow:rules` / job-level guard that runs exactly one. 5. **`when`, `allow_failure`, and variables in rules** — show setting `when: manual`, `allow_failure: true`, and per-rule `variables:` so one job behaves differently per context. 6. **Test harness** — give a method to validate rules: a table of (scenario → expected jobs) and how to use the pipeline editor / `$CI_PIPELINE_SOURCE` echo job to confirm reality matches intent. For each misfiring job I gave, output the corrected `rules:` and a one-line explanation of what it now does. Output as: (a) the variable cheat sheet, (b) the labeled snippet cookbook, (c) the duplicate-pipeline fix, (d) the scenario→expected-jobs test table.
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 workflow:rules Pipeline-Type Gating Prompt
Design a single top-level workflow:rules block that decides which pipeline types run (merge request, branch, tag, schedule, web) and kills duplicate/detached pipelines before a single job evaluates.
-
GitLab CI/CD inherit: Keyword Variable & Default Control Prompt
Use the inherit: keyword to precisely control which global variables and default: settings a job receives, eliminating leaked env vars and surprise image/before_script inheritance.
-
GitLab CI/CD rules:exists Glob Job Gating Prompt
Gate jobs on the presence of files using rules:exists globs — run linters, build steps, and deploys only when the relevant files actually exist in the repo or MR.
-
GitLab CI/CD Variable Masking & Protected Hardening Prompt
Audit and harden GitLab CI/CD variables — masking, protected/expanded flags, scoping, and leak prevention — so secrets never appear in job logs or untrusted branch pipelines.
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.