This action performs a status check to prevent merging pull requests into the main branch when it is in a broken state.
When multiple developers are simultaneously working on the same repository, it can occur that developer A breaks the main branch, but developers B and C continue to merge their PRs into the same branch. This can result in developers B and C being unaware if they have introduced additional bugs, since the main branch is already broken by developer A.
To prevent the above scenario, it may be useful to implement a code freeze on the main branch, allowing developer A to resolve the issue without encountering additional merge conflicts or bugs introduced by developers B and C.
This GitHub action represents such code freeze by using a status check that blocks all pull request merges unless they comply with a specific prefix (i.e. "fix").
When the latest commit on your main branch has a failed status check, it is considered to be in a broken state.
- Install this action from the GitHub Marketplace
- Add branch protection rule to your GitHub repository
- Require status checks to pass before merging
It's as simple as creating this file:
.github/workflows/stop-merging.yml
name: 'Check Mergeability'
on:
pull_request:
# With "edited" it runs on PR title updates
types: [opened, edited, synchronize, reopened]
jobs:
check-main:
runs-on: ubuntu-latest
steps:
- uses: bennycode/[email protected]
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GIT_BRANCH: 'main'
BYPASS_PREFIX: 'fix'
Equip your base branch with branch protection rules:
Enable "Require status checks to pass before merging" and make sure to have at least two status checks:
- A test suite to determine if your branch is broken (i.e. "test")
- The "Stop Merging" check (i.e. "check-main")
It is also recommended to enable "Require branches to be up to date before merging". This will handle the following scenario:
- Main branch is okay
- Coder A creates a PR that passes the "Stop Merging" check
- Coder B breaks the main branch
- Coder A's merge would still be possible if there was no requirement to keep the branch up to date
Screenshot:
When configured as a status check, this action will make your open pull requests fail when your configured main branch is broken. Only pull requests matching a specific title (for example "fix: Main branch") will be allowed to be merged in order to recover the health of your main branch. Once your main branch is green again, you can rerun the failing Stop Merging status checks to also become green again or update your branch which will automatically trigger this action.
The following parameters can be configured:
BYPASS_PREFIX
: Prefix for commit messages that can bypass the status check (defaults to fix)GIT_BRANCH
: Git branch to check for status (defaults to main)GITHUB_TOKEN
: Token to be used by the Stop Merging action to authenticate with your repositoryINTERVAL_RETRIES
: How many times should this action perform a check for a suite to see if it's finished, within a 1-minute interval? (defaults to Infinity)
Example:
steps:
- uses: bennycode/stop-merging
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GIT_BRANCH: 'main'
BYPASS_PREFIX: 'important'