Limiting GitLab CI runner to specific branches or events

Updated . Posted . Visible to the public.

Use rules to include or exclude jobs in pipelines.

Rules are evaluated in order until the first match. When a match is found, the job is either included or excluded from the pipeline, depending on the configuration. The job can also have certain attributes added to it.

rules replaces only/except and they can’t be used together in the same job. If you configure one job to use both keywords, the linter returns a key may not be used with rules error.

GitLab 12.3 introduced rules. You can use them in your .gitlab-ci.yml in your project.

To limit test runs to merge requests and the master branch, you can write this:

tests:
  script: bundle exec rake tests
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
Florian Leinsinger
Last edit
Michael Leimstädtner
License
Source code in this card is licensed under the MIT License.
Posted by Florian Leinsinger to makandra dev (2021-04-19 07:44)