Mergify’s automatic merging is a powerful feature designed to help maintainers save time and keep their repositories up-to-date efficiently. It automates the process of merging pull requests (PRs) once they meet your specified conditions, thereby reducing the need for manual intervention.
This capability is particularly beneficial in scenarios where numerous PRs are regularly opened, such as in open-source projects or in teams practicing continuous integration. By utilizing automatic merging, you can ensure that PRs get merged promptly once they’re ready, helping to maintain a smooth and fast-paced development workflow.
Pre-requisitesSection titled Pre-requisites
Before setting up automatic merges, ensure you meet the following requirements:
Mergify Integration: Your repository must be configured with Mergify.
Mergify Configuration File: Automatic merging is configured using a configuration file in your repository. This file contains the rules and actions Mergify will follow, including the conditions for automatic merging.
Branch Protection Rules: Review any branch protection rules that might prevent automatic merging. For example, make sure that required status checks are configured correctly and that they pass before Mergify tries to merge your PRs.
Setting Up Automatic MergingSection titled Setting Up Automatic Merging
To enable automatic merging in a Mergify-powered repository, follow these steps:
Create or Edit the Mergify Configuration File: Navigate to your repository and create or open the Mergify configuration file. This file is where you define all your Mergify rules.
Define a Pull Request Rule for Merging: In the configuration file, you will need to create a rule that tells Mergify when to automatically merge a pull request. Here’s an example of a basic rule:
In this example, a pull request is automatically merged when it has at least one approved review and the CI status check passes.
Customize Your Merge Action: The
mergeaction in the rule can be customized further. You can specify the
methodto use for merging (
squash), or specify commit message options. Check the merge action documentation for more information on these options.
Save and Commit Your Changes: Once you’ve added and customized your merge rule, commit the changes to your configuration file and push it to your repository. We recommend merging it via a pull request so Mergify can validate the configuration file.
With these settings, Mergify will automatically merge eligible pull requests based on the conditions you’ve specified.
Validating All Status ChecksSection titled Validating All Status Checks
Each pull request can have its own custom list of status checks. This means the list of “status check” does not exist and is not common to all pull requests.
On creation, or when a new commit is pushed, a pull request has no status check.
A status check might not be reported by a service (CI) (e.g., because it’s broken) and therefore be absent.
Those three facts make it mandatory to write explicitly the checks that are expected for your condition to be valid. Therefore you must list explicitly every status check that is expected, e.g.:
Do not use conditions such as:
#check-failure=0, because this will be true as soon as the pull request is created and before any service report its status (see point 2. above).
check-success~=buildwhile expecting this to wait for “all” status checks that have
buildin their name (see point 1. and 2.above).
Such conditions won’t do what you want them to do.
Restricting Automatic MergesSection titled Restricting Automatic Merges
In some cases, you might want to restrict automatic merges based on certain conditions. This can be useful to enforce code quality standards, ensure all necessary checks have passed, or prevent merging of pull requests that do not meet certain criteria. Here’s how you can set this up:
Specify the Restrictive Conditions: In your pull request rule, you can specify conditions that a pull request must meet before it can be merged. This can include anything from passing status checks to having a certain number of approved reviews.
Here’s an example that restricts automatic merging to pull requests that have at least two approved reviews and all status checks passing:
Use Negative Conditions: You can also use negative conditions to prevent automatic merging. For instance, you might want to prevent merging if certain labels are present. Here’s an example:
In this case, the
label!=do-not-mergecondition prevents automatic merging if the
do-not-mergelabel is attached to the pull request.
Remember, conditions are highly flexible and can be adapted to suit your project’s specific needs. For a full list of available conditions, refer to the conditions documentation.
You can also check custom branch protection rules examples.
Automatic Merge and QueuesSection titled Automatic Merge and Queues
When automatic merging is enabled, pull requests are merged as soon as all the conditions specified in your rule are met. In the context of Mergify’s Merge Queue, automatic merge works in tandem with the prioritization and organization that queues offer. You can leverage the queue action to put pull requests in the merge queue rather than merging directly.
Here’s an example of how this might look in your Mergify configuration file:
In this case, when a pull request meets the conditions
check-success=CI), it is added to the merge
queue. Once it’s at the front of the queue, Mergify will automatically merge it
into the target branch and then move on to the next pull request in the queue.
This combination of queues and automatic merging allows for a more efficient and organized way of managing your pull requests, ensuring that pull requests are merged in a timely and prioritized manner.