GitLab provides two major decision layers: workflow rules determine whether a pipeline is created, while job rules determine which jobs are added and how they behave. Older only and except syntax still appears in existing configurations, but new designs should prefer rules.
Decision layers
| Mechanism | Scope | Typical question |
|---|---|---|
| workflow: rules | Entire pipeline | Should this pipeline exist for this event? |
| Job rules | One job | Should this job be added, and with which behavior? |
| only / except | Legacy job selection | Which references or events include or exclude the job? |
Rules are evaluated in order
The first matching rule determines the job’s inclusion and relevant attributes. Put specific conditions before broad fallbacks, and make the final behavior explicit. A permissive last rule can create jobs in pipeline sources you did not intend.
Common conditions
- Branch or tag context.
- Merge-request pipeline source.
- Scheduled, triggered, or web-created pipeline.
- File changes relevant to a component.
- Existence of files in the repository.
- Variable values for approved modes.
- Manual action for controlled deployment.
Avoid duplicate pipelines
A branch push associated with a merge request can produce more than one pipeline if workflow and job rules are not coordinated. Decide which pipeline types the project needs, then encode that policy at workflow level before defining job-specific behavior.
Manual jobs and approvals
A manual job is a control point, not a complete authorization system. Protect environments and branches, restrict who can run deployment jobs, use least-privilege credentials, and record the deployed artifact and approver.
Migrate from only and except
- Document existing behavior for branches, tags, schedules, and merge requests.
- Create representative test cases for every pipeline source.
- Translate one job at a time to rules.
- Inspect the merged configuration and simulated outcomes.
- Run on non-production references.
- Remove old syntax only after behavior matches.
Review checklist
- Every intended pipeline source is covered.
- Untrusted pipelines cannot access protected secrets.
- Change-based rules handle new and deleted files.
- Schedule conditions are separate from push behavior.
- A clear fallback prevents unexpected inclusion.
- Production jobs have explicit authorization and environment protection.
Example: merge-request pipelines and default-branch builds
This example creates a merge-request pipeline for review, avoids an additional branch pipeline when an open merge request exists, and permits a push pipeline on the default branch. It also demonstrates how a job can use rules independently of workflow:rules.
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS'
when: never
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
- when: never
stages:
- test
unit_tests:
stage: test
image: alpine:3.20
script:
- echo 'Run project tests here'
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
- when: neverThis is a policy example, not a universal configuration: it intentionally suppresses feature-branch push pipelines without a merge request. If your project requires those pipelines, add a specific branch rule before the fallback. The script is a placeholder to replace with your real test command; no test execution is claimed.
How rules evaluate: first match wins
| Event | Workflow decision | unit_tests |
|---|---|---|
| Merge request pipeline | Create | Include |
| Push to default branch | Create | Include |
| Push to branch with open merge request | Skip duplicate branch pipeline | Not created |
| Scheduled pipeline | Skip in this example | Not created |
| Push to other feature branch without merge request | Skip in this example | Not created |
Example: run a manual deployment only on the default branch
deploy_production:
stage: deploy
script:
- ./scripts/deploy.sh
environment:
name: production
rules:
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
when: manual
allow_failure: false
- when: neverA manual job alone is not access control. Restrict production environment deployment permissions and tokens, and replace ./scripts/deploy.sh with your reviewed deployment process. Add the deploy stage to your stages list before using this standalone fragment.
Validate before modifying production pipelines
- Copy the proposal into GitLab CI Lint and inspect its simulation for merge requests, pushes and scheduled events.
- Run on a temporary test branch in your project; inspect which pipeline types appear and which jobs are included.
- Check that no untrusted merge-request job can access protected secrets or deployment credentials.
- Verify the actual job command; placeholder echo lines are not tests.
- After merging, review any duplicate pipeline warnings and compare expected versus actual pipeline results.
Further reading: https://docs.gitlab.com/ci/jobs/job_rules/ and https://docs.gitlab.com/ci/yaml/workflow/ . Melochip: https://melochip.com/posts/gitlab-artifacts-vs-cache-explained and https://melochip.com/posts/gitlab-ci-cd-troubleshooting-guide .
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
8 min read · Updated Nov 16, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Updated Nov 16, 2026Related articles
Learn Docker registries, image tags, Docker Hub, private registries, login, push, pull, naming rules, CI/CD us...
3 min read · Updated Nov 16, 2026
Learn Kubernetes monitoring and logs with kubectl logs, events, metrics-server, top commands, Prometheus, Graf...
6 min read · Updated Nov 16, 2026
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
8 min read · Updated Nov 16, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.