Sub topic 6
Running workflows on merge_group
This chapter assumes you can read GitHub Actions workflow syntax, know what a required status check is, and have a branch protection rule or ruleset on your base branch. You should also know the idea of a merge queue from earlier chapters: the queue builds a temporary branch that holds the base branch plus the pull requests ahead of yours, then waits for checks on that branch.
Those checks only run if your workflows are told to run on the queue's branch. That is the job of the merge_group trigger.
Why the trigger is needed
A workflow with on: pull_request runs when a pull request is opened, updated, or reopened. The merge queue does not do any of those things. It creates a new commit on a temporary branch, and no pull request event fires for that commit.
If your required check only listens to pull_request, this happens:
- You add a pull request to the queue.
- The queue creates the temporary merge group branch.
- GitHub waits for the required check to report on that branch.
- No workflow starts, so no status ever arrives.
- The queue waits until the check response timeout and then removes the pull request.
The fix is to add merge_group to the on: section of every workflow that produces a required check.
The minimal workflow
This workflow runs the same job on a pull request and on a merge group.
name: CI
on:
pull_request:
branches: [main]
merge_group:
types: [checks_requested]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run tests
run: make testThe job is named test and appears as the check test. It reports a status on the pull request commit and again on the merge group commit. Branch protection matches required checks by name, so the same name must be reported in both places. Because the job is defined once, the names match automatically.
checks_requested is the only activity type that merge_group supports. You can therefore leave out types, and this is equivalent:
name: Lint
on:
pull_request:
merge_group:
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run linter
run: make lintWriting types: [checks_requested] is more explicit. Omitting it is shorter. Pick one style and use it consistently across the repository.
What the workflow sees on a merge group
A merge_group run behaves differently from a pull_request run in several ways. Knowing them prevents most surprises.
The checked-out commit
For pull_request, actions/checkout checks out a synthetic merge of the pull request into its base by default (refs/pull/N/merge). For merge_group, GITHUB_SHA is the merge group commit itself, and the default checkout gives you that commit. It already contains the base branch plus every queued change up to and including yours. You do not need extra checkout options.
Ref and context values
The workflow runs against a ref of the form refs/heads/gh-readonly-queue/<base>/pr-<number>-<sha>. The event payload sits under github.event.merge_group. Useful fields include:
| Context value | Meaning |
|---|---|
| `github.event.merge_group.head_sha` | The SHA of the merge group commit |
| `github.event.merge_group.head_ref` | The full ref of the temporary branch |
| `github.event.merge_group.base_sha` | The SHA of the base branch the group was built on |
| `github.event.merge_group.base_ref` | The base branch ref, such as `refs/heads/main` |
There is no github.event.pull_request object in a merge group run. Any expression that reads github.event.pull_request.number or github.event.pull_request.head.ref evaluates to an empty string. Steps that depend on those values, such as posting a comment or labeling a pull request, will fail or do nothing.
Branch and path filters
The merge_group event does not support paths filters. It supports branches filters, and these match the base branch name in the ref. Be careful here: a filter written for pull_request does not carry over. If you restrict pull_request to branches: [main], add the matching filter to merge_group only if you need one. Without a filter, merge_group runs for every queue in the repository.
This matters for monorepos. A workflow that uses paths: on pull_request to skip unrelated changes will not run for those changes, so its required check never reports on the pull request. That problem already exists without a queue. With a queue, the merge group event ignores the path filter, so the workflow runs there regardless.
Skipping work that only makes sense on pull requests
Some jobs should run only on pull requests, such as a job that checks the pull request title or posts a preview link. Gate them with an if condition on the event name.
name: PR hygiene
on:
pull_request:
types: [opened, edited, synchronize]
merge_group:
jobs:
title-check:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Require a conventional title
env:
TITLE: ${{ github.event.pull_request.title }}
run: |
echo "$TITLE" | grep -Eq '^(feat|fix|chore|docs)(\(.+\))?: .+'The title is passed through an environment variable and quoted in the shell. This avoids interpolating untrusted text into the script.
There is a trap here. If title-check is a required check and the job is skipped on merge_group, GitHub treats a skipped job as a success for required checks, so the queue proceeds. This is usually what you want. It becomes a problem when the whole workflow does not run at all: a workflow that has no merge_group trigger never reports, and the check stays pending until the timeout.
A workflow that aggregates required checks
Many teams do not list each job as a required check. Instead they add one final job that depends on the others and require only that job. This keeps the branch protection settings stable when jobs are renamed or added.
name: CI
on:
pull_request:
merge_group:
jobs:
unit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make unit
integration:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make integration
ci-ok:
if: always()
needs: [unit, integration]
runs-on: ubuntu-latest
steps:
- name: Fail if any dependency did not succeed
run: |
if [ "${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}" = "true" ]; then
exit 1
fiMark ci-ok as the only required check. The if: always() line makes the job run even when a dependency fails, so it can report a failure instead of being skipped. Without it, a failed dependency makes ci-ok skip, and a skipped required job counts as passing. That would let broken code through.
Workflows that use other triggers
Not every workflow needs merge_group. Add it only to workflows that produce a required check. Others can stay unchanged:
- A scheduled nightly job does not take part in the queue.
- A deploy workflow on
pushtomainruns after the merge, as before. - A labeler on
pull_request_targethas nothing to do with merge groups.
If you use reusable workflows, add merge_group to the caller. The called workflow runs in the context of the caller's event, so the trigger is read from the top-level workflow only.
Checking that it works
Before relying on the queue, verify the setup on a low-risk pull request.
- Confirm that every required check name appears in a workflow with
merge_groupin itson:section. - Add a small pull request to the queue.
- Open the Actions tab and filter by event
merge_group. You should see a run on a branch namedgh-readonly-queue/main/pr-.... - Check that each required check reports a result for that run.
If the run does not appear, the workflow file on the base branch probably lacks the trigger. GitHub reads the workflow definition from the merge group commit, but it must already be correct there, so merge the trigger change into main before testing.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Pull request leaves the queue after the timeout with a pending check | Workflow has no `merge_group` trigger | Add `merge_group` to the workflow |
| Check reports on the pull request but not in the queue | Required check lives in a different workflow without the trigger | Add the trigger to that workflow |
| Step fails with an empty pull request number | Step reads `github.event.pull_request` | Guard with `github.event_name` or use `github.sha` |
| Job name differs between events | Matrix or conditional names change the check name | Use a fixed `name:` on the job |
| Broken code merges | Aggregate job lacks `if: always()` | Add `if: always()` and test the `needs` results |
Exercise
A repository requires a check named build. The workflow that defines build has on: pull_request only. A developer adds a green pull request to the merge queue. What happens, and what is the fix?
Show answer
The queue creates a merge group branch and waits for build, but no workflow runs because the merge_group event is not a trigger. The check never reports, so after the check response timeout the queue removes the pull request. The fix is to add merge_group: (optionally with types: [checks_requested]) to the workflow's on: section and merge that change into the base branch.
Exercise
A workflow has a job that posts a comment using github.event.pull_request.number. The workflow now also runs on merge_group. How do you keep that job from failing in the queue without making the required check hang?
Show answer
Add if: github.event_name == 'pull_request' to the job. On a merge group event the job is skipped, and a skipped job counts as passing for required checks, so the queue continues. The workflow still runs on merge_group, so other required jobs report normally.