GitLab CI/CD pipeline admission and job rules: decide what exists before it runs
Trace GitLab pipeline sources through workflow rules and job rules. Prevent duplicate branch and merge-request pipelines while keeping required checks present.
TL;DR:
workflow:rulesdetermines whether a pipeline is created. A job'srulesthen determines whether that job belongs to it. Runners execute the resulting jobs; extra runner capacity cannot repair a pipeline excluded during creation.
Two decisions happen before a runner is involved
A push to a branch with an open merge request can lead to both a branch pipeline and a merge-request pipeline when configuration permits both. Those are separate pipelines for different contexts. Removing a job from one does not necessarily prevent the other pipeline from existing.
Define the permitted pipeline sources first. Then define each job's membership. GitLab evaluates rules in order and uses the first match; values produced by a job script arrive too late to decide that job's inclusion. The job-rules reference documents these evaluation boundaries.
Make the source policy explicit
This illustrative .gitlab-ci.yml admits merge-request pipelines and default-branch pushes. It intentionally excludes schedules, tags and other sources. A project needing those sources must add deliberate cases; a broad final when: always would change the policy.
workflow:
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
- when: never
verify:
script:
- printf 'verification fixture\n'
The example prints a harmless marker. It is an admission exercise, not an application test. In an authorized test project, compare the observed pipelines with this matrix, recording pipeline ID and source rather than only counting green jobs.
| Event | Pipeline expected | Reason |
|---|---|---|
| Feature push without merge request | No | Neither admitted source condition matches |
| Merge-request update | Yes | Merge-request source is admitted |
| Default-branch push | Yes | Push and default branch both match |
| Scheduled run | No | No schedule rule exists |
| Tag push | No | The branch condition does not match a tag |
The workflow reference also describes switching from branch pipelines to merge-request pipelines when a merge request opens. That policy differs from the example, which deliberately skips ordinary feature-branch pushes. Choose based on the team's required feedback, then test the event matrix.
Required checks must survive selection
Suppose deploy is included on the default branch while scan appears only in merge-request pipelines. Adding needs: [scan] exposes an inconsistent dependency graph. Making the need optional can let a deployment proceed without the scan, which is wrong when scanning is a release requirement.
Align the release job's rules with its mandatory prerequisites. For a genuinely optional documentation preview, optional dependencies can be appropriate. Describe why absence is safe. The GitLab dependency concept develops that distinction.
A manual job is also not automatically a mandatory gate. Its failure/optional behavior depends on configuration, including where when: manual is declared. Review allow_failure explicitly for an approval that must block subsequent work.
Self-check: a job sets RUN_SECURITY=true in its script and another job's rules reads that variable. Can it decide whether the second job is created? No. Script execution happens after membership is decided. Use an input available during pipeline creation, or design a separate child-pipeline generation flow with its own reviewed contract.
Rehearse the event diagnosis in duplicate GitLab pipelines.