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.
You cannot use dotenv variables created in job scripts in rules, because rules are evaluated before any jobs run.
Future keyword improvements are being discussed in our epic for improving rules , where anyone can add suggestions or requests.
The following example uses if to define that the job runs in only two specific cases:
Alternatively, you can define a set of rules to exclude jobs in a few cases, but run them in all other cases:
If you use a when clause as the final rule (not including when: never ), two simultaneous pipelines may start. Both push pipelines and merge request pipelines can be triggered by the same event (a push to the source branch for an open merge request). See how to prevent duplicate pipelines for more details.
You can configure a job to be executed only when the pipeline has been scheduled. For example:
In this example, make world runs in scheduled pipelines, and make build runs in branch and tag pipelines.
For example, in a project with main as the default branch:
The rule for this job compares all files and paths in the current branch recursively ( **/* ) against the main branch. The rule matches and the job runs only when there are changes to the files in the branch.
The following example runs the job as a manual job in scheduled pipelines or in push pipelines (to branches or tags), with when: on_success (default). It does not add the job to any other pipeline type.
The following example runs the job as a when: on_success job in merge request pipelines and scheduled pipelines. It does not run in any other pipeline type.
You can use predefined CI/CD variables with rules to choose which pipeline types jobs should run for.
For example, to configure a job to run for merge request pipelines and scheduled pipelines, but not branch or tag pipelines:
CI_PIPELINE_SOURCE predefined variable
Use the CI_PIPELINE_SOURCE variable to control when to add jobs for these pipeline types:
These values are the same as returned for the source parameter when using the pipelines API endpoint.
You can use all rules keywords, like if , changes , and exists , in the same rule. The rule evaluates to true only when all included keywords evaluate to true.
If the Dockerfile file or any file in /docker/scripts has changed and $VAR == “string value”, then the job runs manually and is allowed to fail.
You can use parentheses with && and || to build more complicated variable expressions.
If a job uses rules , a single action, like pushing a commit to a branch, can trigger multiple pipelines. You don’t have to explicitly configure rules for multiple types of pipeline to trigger them accidentally.
Some configurations that have the potential to cause duplicate pipelines cause a pipeline warning to be displayed.
This job does not run when $CUSTOM_VARIABLE is false, but it does run in all other pipelines, including both push (branch) and merge request pipelines. With this configuration, every push to an open merge request’s source branch causes duplicated pipelines.
Rewrite the rules to run the job only in very specific cases, and avoid a final when rule:
You can also avoid duplicate pipelines by changing the job rules to avoid either push (branch) pipelines or merge request pipelines. However, if you use a - when: always rule without workflow: rules , GitLab still displays a pipeline warning.
For example, the following does not trigger double pipelines, but is not recommended without workflow: rules :
You should not include both push and merge request pipelines in the same job without workflow:rules that prevent duplicate pipelines:
Also, do not mix only/except jobs with rules jobs in the same pipeline. It may not cause YAML errors, but the different default behaviors of only/except and rules can cause issues that are difficult to troubleshoot:
For every change pushed to the branch, duplicate pipelines run. One branch pipeline runs a single job ( job-with-no-rules ), and one merge request pipeline runs the other job ( job-with-rules ). Jobs with no rules default to except: merge_requests , so job-with-no-rules runs in all cases except merge requests.
Use !reference tags to reuse rules in different jobs. You can combine !reference rules with regular job-defined rules. For example:
Use variable expressions with rules:if to control when jobs should be added to a pipeline.
You can do regular expression matching on variable values with the =~ and !~ operators. Variable pattern matching with regular expressions uses the RE2 regular expression syntax.
Variables on the right side of =~ and !~ expressions are evaluated as regular expressions. The regular expression must be enclosed in forward slashes ( / ). For example:
Variables in a regular expression are not resolved. For example:
The precedence of operators follows the Ruby 2.5 standard, so && evaluates before || .
You can use parentheses to group expressions together. Parentheses take precedence over && and || , so expressions enclosed in parentheses evaluate first, and the result is used for the rest of the expression.
When using the =~ character, make sure the right side of the comparison always contains a valid regular expression.
If the right side of the comparison is not a valid regular expression enclosed with / characters, the expression evaluates in an unexpected way. In that case, the comparison checks if the left side is a substring of the right side. For example, "23" =~ "1234" evaluates to true, which is the opposite of "23" =~ /1234/ , which evaluates to false.
You should not configure your pipeline to rely on this behavior.
Edit this page to fix an error or add an improvement in a merge request.
Create an issue to suggest an improvement to this page.
Create an issue if there's something you don't like about this feature.
Propose functionality by submitting a feature request.
Join First Look to help shape new features.
View pricing to see all GitLab tiers and features, or to upgrade.
Try GitLab for free with access to all features for 30 days.
If you didn't find what you were looking for, search the docs.
If you want help with something specific and could use community support, post on the GitLab forum.
For problems setting up or using this feature (depending on your GitLab subscription).