A .gitlab-ci.yml whose jobs carry no rules gives a merge request no pipeline of its own and runs publish on every branch, so each job in this file states which pipelines it joins. rules is the keyword GitLab 19.6 uses for that, while only and except still run and are deprecated.
A rule on every job
Here is .gitlab-ci.yml for a solution with an API project:
stages: [build, test, publish]
build:
stage: build
image: mcr.microsoft.com/dotnet/sdk:10.0
script:
- dotnet restore App.sln
- dotnet build App.sln --no-restore -c Release
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH
test:
stage: test
image: mcr.microsoft.com/dotnet/sdk:10.0
script:
- dotnet test App.sln -c Release --logger "junit;LogFilePath=$CI_PROJECT_DIR/test-results/{assembly}.xml"
artifacts:
reports:
junit: test-results/*.xml
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH
publish:
stage: publish
image: mcr.microsoft.com/dotnet/sdk:10.0
needs: [test]
script:
- dotnet publish Api/Api.csproj -c Release -t:PublishContainer
-p:ContainerRegistry=registry.example.com
-p:ContainerImageTag=$CI_COMMIT_SHORT_SHA
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
build and test each have two rules, and GitLab adds a job when any one of them matches. The first, $CI_PIPELINE_SOURCE == "merge_request_event", puts the job in the pipeline GitLab creates for a merge request, and the second, if: $CI_COMMIT_BRANCH, puts it in the pipeline for a push to any branch. publish has one rule that compares the branch with $CI_DEFAULT_BRANCH, so it runs after test only on the default branch. The Image Tag Is the Commit explains the tag it pushes.
One pipeline per push
The two rules exist because a job with no rules is added to branch and tag pipelines but never to a merge request pipeline. In a file whose jobs have no rules, opening a merge request therefore produces nothing new, and the only pipeline the merge request can show is the branch pipeline from the last push.
With the rules in place, a push to a branch that has an open merge request now creates both pipelines, so build and test run twice for the same commit. A workflow block at the top of .gitlab-ci.yml keeps one of them:
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS
when: never
- if: $CI_COMMIT_BRANCH
- if: $CI_COMMIT_TAG
workflow:rules decides whether a pipeline is created at all, and GitLab uses the first rule that matches. A merge request event passes on the first line. A branch push whose branch has an open merge request stops on the second, because $CI_OPEN_MERGE_REQUESTS is set for it, and any other branch push passes on the third. The last line admits tag pipelines, which matters once a job has a tag rule.
The default branch by variable
publish names the default branch through $CI_DEFAULT_BRANCH because that variable follows the project setting. The older only: form names a branch literally, so a project whose default branch is renamed keeps a rule that matches a branch nobody pushes to.
That same comparison also matches a scheduled pipeline, because a schedule runs on a ref and the rule looks at the ref rather than at $CI_PIPELINE_SOURCE. A nightly schedule on the default branch would therefore publish. To stop it, put a schedule rule first in the rules of publish in .gitlab-ci.yml:
publish:
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
when: never
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
GitLab reads the rules in order, so a scheduled pipeline stops at when: never before it reaches the branch comparison, and a push to the default branch skips the first rule and matches the second.
Tags and test results
A tag pipeline has $CI_COMMIT_TAG set and $CI_COMMIT_BRANCH empty, so none of the branch rules above match it and publish skips tags. A release tag needs its own rule, if: $CI_COMMIT_TAG, on the publish job.
The test results come from two lines in test. The --logger "junit;…" option makes dotnet test write one JUnit XML file per test project, which needs the JunitXml.TestLogger package in every test project: without it, dotnet test stops with Could not find a test logger with AssemblyQualifiedName, URI or FriendlyName 'junit' before running a test. The log path is absolute because a relative one is resolved from each test project's own folder, and {assembly} keeps two projects from writing the same file. reports: junit: test-results/*.xml uploads them all, whether the job passed or failed, so the merge request's tests panel lists each test by name. Publish Only After the Tests Pass shows the run behind those details.
With the logger in place, every merge request and branch gets a build, a test run, and named test results, while publish waits for the default branch. Tags are the gap in this file: until the publish job carries an if: $CI_COMMIT_TAG rule, a release tag produces no image.