How to handle merge queue problem in monorepo
If you have a problem with merge queue in your monorepo, in this article I will show you how you can handle it on Github, even if you don’t have monorepo — this article shows how to automate process of merging branches.
Github configuration
Recently GitHub introduced new “auto-merge” functionality, you can read more about it here. You can find this option in repository settings. When you turn on this feature you should see new button Enable auto-merge
. That’s really cool thing and almost solves merge queue problem… But only almost.
If you already experimented with this feature you noticed that this is cool and works but when we have restriction for Pull Request to be up to date with base branch — your PR won’t be automatically merged:
GitHub Actions come to the rescue
GitHub actions are great to automate processes via workflows, in other words you can specify jobs which has to be done and run that workflow on particular event like pull_request
, commit
etc. If you’d like to learn more please visit official documentation.
Other than that, we can search for actions in great marketplace. At Brainly we released action called autoupdate-branch
and you can find it here.
How this solution works? Only what it requires from developers is to add label to your PR and based on this label autoupdate-branch
action will take Pull Request and automatically merge base branch to PR. It’s up to you what name of label you would like to specify. You can do this in action configuration (dump below is example how to do this). E.g
As you can see on above screenshot, GitHub action bot is automatically merging master to your branch with label (in our case) AUTOMERGE
.
Implementation
First of all we need to specify workflow in our repository, let’s create .github/workflows
directories in repo and put there file autoupdate-branch.yml
. In this file we need to specify implementation:
name: autoupdate-branch
on:
push:
branches:
- masterjobs:
update-branches:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: autoupdate-branch
uses: brainly/action-autoupdate-branch@1.0.0
id: updateBranches
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
label: 'AUTOMERGE'
Code is really readable, at the beginning we are saying that we’d like to run action on each push
event to branch master. So if master will be updated we would like to run some jobs.
We specified one job update-branches
— we’d like to run it on ubuntu image and do particular steps. In last part with
you can see input to action, so we’re saying that we would like to update branches only with label AUTOMERGE and we need to pass repo-token
which is available globally in your repository under secrets.GITHUB_TOKEN
key — we need this token to authorise repo to be able to do actions like getting Pull Requests list and merge branches with each others.
And that’s it! Your action should work properly — each branch with specified label will be automatically updated on each master change. To see how your actions works you can go to Actions
tab on GitHub and take a look on execution:
Boost up communication
This is really cool solution, but you may think, what if my Pull Request is conflicted? We also found solution for that. First of all autoupdate-branch
gives us output with detail of branch when wasn’t able to do update — i.e when branch is conflicted. So we could specify particular behaviour in that case. Output given by action has exact interface and returns as JSON:
{
title: string,
url: string,
user: {
login: string,
url: string,
avatarUrl: string
}
}
We could use those data in next step in our workflow, at Brainly we decided to send slack notification on our dedicated channel, so we already prepared second action which takes that information and sending slack notification, so let’s add next step with second action:
- name: Slack Notify About Conflicts
if: steps.updateBranches.outputs.hasConflicts
uses: brainly/action-slack-notificator@1.0.1
with:
webhookUrl: ${{ secrets.SLACK_WEBHOOK }}
pullRequestData: ${{steps.updateBranches.outputs.conflictedPullRequestJSON}}
message: 'Additional Message here - can be JSON with
slack elements'
This steps uses brainly/action-slack-notificator@1.0.1
action. We need to specify our SLACK_WEBHOOK which you can prepare with this guide. Example message looks like this:
Conclusion
Combining GitHub Enable Auto-merge feature with GitHub Actions is great idea to automate process of merging PRs, especially in Monorepo where lot’s of developers contributes — it’s solves problem od merge queue.
Additionally Github Actions gives a lot flexibility to create communication process and setup additional steps, rules etc.
I hope that you found this article helpful and introduced solution will solve your problems and helps automate processes!