Sub topic 6
What to run at merge time and on main
This chapter assumes you know how pull request checks work in a CI system such as GitHub Actions, and that you can tell unit tests from integration tests. It covers the second stage of the pipeline: the checks that run when a change is about to land, and after it has landed on main.
Why a second stage exists
Pull request checks test your branch as it was when you pushed it. Other pull requests may have merged since then. Two changes can each pass alone and still break each other once combined. A merge-time stage tests the combined result.
This stage can afford to be slower than the pull request stage. Developers are not waiting on it in the same way, and it runs once per merge, not once per push.
Two places to run merge-time checks
| Place | Trigger | What it tests | Blocks the merge? |
|---|---|---|---|
| Merge queue | `merge_group` | A candidate commit: your change on top of `main` and any queued changes | Yes, if the check is required |
| Main branch | `push` to `main` | The commit that has already landed | No, the code is already in |
A merge queue is a GitHub feature that builds each pull request on top of the latest main and the changes ahead of it. It merges only if the checks pass. Without a queue, a check on main can only tell you after the fact that something broke.
What to run here
Good candidates have three traits. They take several minutes, not seconds. They need more setup than a unit test, such as a database or a built artifact. They catch problems that only appear when changes combine.
- Integration tests that exercise several components or a real database.
- Builds of deployable artifacts, such as container images.
- Contract tests between services.
- A repeat of the fast checks, when the merge changes what they see.
Keep out anything that takes hours. A queue holds every waiting change behind the one being tested, so a 90-minute check makes the whole team wait. Those checks belong in the nightly stage.
Configuring the trigger
This workflow runs integration tests for the queue and for pushes to main:
name: merge
on:
merge_group:
push:
branches: [main]
jobs:
integration:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run test:integrationThe merge_group event fires when the queue builds a candidate commit. The push event with branches: [main] fires after each merge. One job serves both triggers, so the same command runs in both places.
Why run again on main
If the queue already passed, a second run can look wasteful. It still has uses. Merges made without the queue, such as an administrator bypass, skip the candidate test. A run on main also gives you a record of the state of main for each commit, and a known-good point to deploy from or roll back to.
If you use a queue and trust it fully, you can run a lighter set on main. Choose based on how often bypass merges happen and how costly a broken main is for you.
When a main run fails
A failure on main means the branch is broken for everyone. Treat it as urgent. Revert the offending commit first, then investigate. Do not leave main red while you debug, because every new pull request will inherit the failure.
Exercise
Your team adds a 15-minute integration suite. It is required on pull requests but does not list merge_group in its workflow triggers. After you enable a merge queue, every queued pull request hangs. Why, and what is the fix?
Show answer
The queue waits for the required check on the candidate commit, but the workflow never starts for merge_group events, so no result is reported. Add merge_group: under on: in the workflow so the check runs on the candidate commit.