Sub topic 6
What to Run at Merge Time and on Main
This chapter covers the second stage of the pipeline: the checks that run after a pull request is approved, when a candidate commit is built for main, and after the commit lands. It assumes you know the basics of Git and pull requests. It also assumes you have used a CI system such as GitHub Actions and know the difference between unit and integration tests.
Why a merge stage exists
Pull request checks answer one question: "Does this change look safe on its own?" They run against the branch, usually in a few minutes. That speed has a cost. The branch may be days behind main, and the fast suite skips slow checks that need databases, containers, or other services.
The merge stage answers a different question: "Is this change safe combined with everything else going into main?" It tolerates more runtime than a pull request because it runs less often. It runs once per merge, not once per push to a branch.
Two situations show why the stage is needed:
- Two pull requests each pass on their own. One renames a function and the other adds a new call to the old name. Neither branch sees the other, so both are green. After both merge,
mainis broken. - A pull request passes unit tests, but the integration tests that would catch a bad database migration take 20 minutes. Running them on every push to every branch would waste runners and slow every author.
The merge stage runs the heavier checks on a commit that is as close as possible to what main will become.
Two places to run merge-time checks
There are two common setups. They differ in when the check runs relative to the merge.
| Setup | When it runs | What it protects | Trade-off |
|---|---|---|---|
| Merge queue (`merge_group`) | Before the change lands, on a temporary commit that combines the pull request with the queue ahead of it | `main` never receives a commit that failed these checks | Needs a merge queue feature, and each merge waits for the queue |
| Post-merge on `main` (`push`) | After the change lands | Detects breakage quickly and records the state of `main` | `main` can be red for a short time, so you need a rollback or fix-forward habit |
You can use both. The queue acts as a gate. The push run on main then confirms the final result and can do work that only makes sense after merging, such as publishing artifacts or deploying to a staging environment.
What belongs at merge time
A check belongs at the merge stage when it meets most of these conditions:
- It takes too long to run on every pull request push, typically more than 10 to 15 minutes.
- It is more likely to fail because of how changes combine than because of a single change.
- It needs resources that are costly or limited, such as a shared test database, a Docker Compose stack, or paid third-party sandbox accounts.
- Its failure signal is clear enough that a developer can act on it without a long investigation.
Typical candidates:
- Integration tests. They exercise several components together, often against real services started in containers.
- Contract and API compatibility tests. They check that your service still matches what its consumers expect.
- Database migration checks. They apply migrations to a fresh database, and sometimes to a copy of a recent schema, and verify the result.
- Full builds of all targets. Examples are every platform, every package in a monorepo, and the production Docker image.
- Security scans that need a full build. Image scanning and dependency audits on the produced artifacts fall here.
Fast checks should not be dropped from the merge stage just because they ran on the pull request. If the candidate commit differs from the branch, a lint error or a compile error can appear only in the combination. Many teams rerun a minimal build and the unit tests in the queue. They do this because those checks are cheap and they protect main.
Configuring the merge workflow
A merge queue sends a merge_group event to your workflows. A workflow that does not listen for this event does not run in the queue. The queue then waits for checks that never report, and the merge stalls.
This workflow runs integration tests in both places:
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 trigger takes no branch filter in the way pull_request does. The push trigger with branches: [main] runs after every merge, including direct pushes by administrators.
Note that the job name integration is what branch protection refers to. If you rename the job later, the required check no longer matches and merges can block. The next chapters cover required checks in more detail.
Making the pull request workflow queue-aware
If the pull request workflow also has jobs you want required in the queue, add the merge_group trigger to that workflow. A common pattern is to share the same fast jobs between both events:
name: pr
on:
pull_request:
branches: [main]
merge_group:
jobs:
fast-checks:
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 lint
- run: npm testNow fast-checks reports on the pull request and again on the candidate commit. The check has the same name in both places, so one required-check rule covers both.
Failure modes at merge time
A stalled queue. A required check that never runs blocks every entry behind it. The usual cause is a missing merge_group trigger, or a path filter that skips the workflow. For required checks, avoid path filters on the workflow itself. Skipped workflows do not report a status, and a required check with no status stays pending.
A flaky integration test. A test that fails at random removes good changes from the queue and forces re-queuing. Queues amplify flakiness, because one random failure can invalidate the candidate commits stacked behind it. Quarantine flaky tests quickly, and track them by name so that they do not stay quarantined forever.
Red main. If post-merge checks fail on main, the cause is often something the queue could not see, such as an environment difference or a dependency that changed upstream. Agree on a rule in advance. A common one is that the author of the most recent merge reverts or fixes forward, and that no other merges happen until main is green again. Without a rule, a red main is ignored until someone gets blocked.
Duplicate work. Running the identical suite on the pull request, in the queue, and again on main triples the cost for little gain. Differentiate the stages. The pull request runs the fast subset. The queue runs the integration suite. The main push runs checks that need the final commit, such as publishing and deploying.
Handling post-merge failures
When a post-merge check fails, the signal is different from a pull request failure. The author has often moved on. Make the failure easy to route:
- Post a message to a team channel with the commit, the author, and a link to the run.
- Use the commit author as the default owner. If a failure comes from a dependency change or an infrastructure fault, hand it to the person on call.
- Set a time limit. For example, if
mainis red for more than 30 minutes, revert the last merge.
Cancelling superseded runs on main is usually a mistake. Every commit on main should keep its own result, so that you can identify which commit broke it. This differs from pull request runs, where cancelling old runs when a new push arrives saves resources.
name: main-post-merge
on:
push:
branches: [main]
concurrency:
group: main-${{ github.sha }}
cancel-in-progress: false
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:integrationGrouping by commit SHA means each commit has its own group, so no run is cancelled by a later one.
Choosing between queue and post-merge only
| Question | Favors a merge queue | Favors post-merge only |
|---|---|---|
| How many merges per day? | Many, with frequent conflicts | Few |
| How costly is a broken `main`? | High, because deploys come from `main` | Low, because rollback is quick |
| How long are integration tests? | Under about 30 minutes | Longer than a queue can reasonably wait |
| Are tests stable? | Yes | Not yet |
If your suite is slow or unstable, start with post-merge checks only. Moving to a queue later is simple once the suite is reliable.
Exercise
A team requires a check named integration on main. They enable a merge queue, but every queued pull request waits forever. The integration workflow has on: push: branches: [main] and nothing else. What is wrong, and what is the fix?
Show answer
The workflow does not listen for the merge_group event, so the queue's candidate commit never produces an integration status, and the required check stays pending. Add merge_group: to the workflow's on block. Keep the job name unchanged so the required check still matches.
Exercise
Your integration suite takes 25 minutes. It currently runs on every pull request push, and authors complain about wait times. Where would you move it, and what would you leave on the pull request?
Show answer
Move the integration suite to the merge stage (merge_group and push to main). Leave lint and unit tests on the pull request so authors still get feedback in a few minutes. Optionally keep a small smoke test subset on the pull request if integration breakage is common.