738 words, 4 min read

You set up a workflow to approve and auto-merge safe Dependabot updates. Approval works, but the merge step fails with:

GraphQL: Pull request User is not authorized for this protected branch (enablePullRequestAutoMerge)

The YAML is correct. The problem is a mismatch between the identity running the workflow and your branch protection rules.

The setup

A typical workflow looks like this:

on:
  pull_request:
    types: [opened, synchronize, reopened]

permissions:
  contents: write
  pull-requests: write

jobs:
  dependabot:
    runs-on: ubuntu-latest
    if: github.actor == 'dependabot[bot]'
    steps:
      - uses: dependabot/fetch-metadata@v2
        id: metadata
        with:
          github-token: "${{ secrets.GITHUB_TOKEN }}"

      - name: Approve patch and minor updates
        if: steps.metadata.outputs.update-type == 'version-update:semver-patch' || steps.metadata.outputs.update-type == 'version-update:semver-minor'
        run: gh pr review "$PR_URL" --approve
        env:
          PR_URL: ${{ github.event.pull_request.html_url }}
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Enable auto-merge
        if: steps.metadata.outputs.update-type == 'version-update:semver-patch' || steps.metadata.outputs.update-type == 'version-update:semver-minor'
        run: gh pr merge --auto --merge "$PR_URL"
        env:
          PR_URL: ${{ github.event.pull_request.html_url }}
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Both gh calls authenticate with the built-in GITHUB_TOKEN, which acts as the github-actions bot.

What goes wrong

Auto-merge is performed on behalf of whoever enables it. If the target branch has "Restrict who can push to matching branches" enabled, that actor must be on the allow-list, or GitHub refuses the request with the error above.

The review step works because approving a PR isn't a push. The merge step fails because merging is.

You can confirm this with the API:

gh api repos/<owner>/<repo>/branches/<branch>/protection

Look for a non-empty restrictions object. If it lists a few users and no apps, that's your answer.

The obvious fix doesn't work

The natural move is to add "GitHub Actions" to the allow-list. You can't. The allow-list search only finds apps installed on the repository or organization. The built-in Actions identity behind GITHUB_TOKEN isn't an installable app, so it won't appear in the search.

Your options are:

  1. Drop the push restriction. The review and status-check rules still protect the branch, but you widen who can push directly. Usually not worth it.
  2. Use a personal access token from an allow-listed user. This works, but ties the automation to one person's account and the token's lifetime.
  3. Use a GitHub App token. The app is installable, so it can be added to the allow-list, and it isn't tied to a person.

Option 3 is the best fit.

The fix: a GitHub App token for the merge step

  1. Create a GitHub App with repository permissions Contents: Read & write and Pull requests: Read & write. No webhook is needed.
  2. Install it on the repository.
  3. Add the app to the branch's push allow-list. It will now appear in the search.
  4. Store the app ID and the private key as secrets.

Then generate a short-lived token in the workflow and use it only for the merge step:

- name: Generate app token
  id: app-token
  uses: actions/create-github-app-token@v2
  with:
    app-id: ${{ secrets.AUTOMERGE_APP_ID }}
    private-key: ${{ secrets.AUTOMERGE_APP_PRIVATE_KEY }}

- name: Enable auto-merge
  if: steps.metadata.outputs.update-type == 'version-update:semver-patch' || steps.metadata.outputs.update-type == 'version-update:semver-minor'
  run: gh pr merge --auto --merge "$PR_URL"
  env:
    PR_URL: ${{ github.event.pull_request.html_url }}
    GH_TOKEN: ${{ steps.app-token.outputs.token }}

The approval step keeps using GITHUB_TOKEN. That's deliberate: the approver and merger are different identities, so the app never approves its own PRs, and a separate actor satisfies the required-review rule.

Side benefits

  • Downstream workflows run. Events caused by GITHUB_TOKEN don't trigger other workflows. Merges made with an app token do, so post-merge pipelines run as expected.
  • Scoped and short-lived. The token is minted per run and expires, unlike a long-lived PAT.
  • No dependence on a person. The automation survives people leaving or changing roles.

What auto-merge still won't do

Auto-merge waits for your required status checks and review rules. It only helps if at least one check is required; otherwise, there is nothing to wait for. Keep your branch protection as strict as before: the app only gets permission to perform the merge once everything is green.

Takeaways

  • "Not authorized for this protected branch" on auto-merge usually means the actor isn't on the push allow-list, not that the workflow is wrong.
  • GITHUB_TOKEN can't be added to that list, so use a GitHub App token for the merge step.
  • Keep approval and merging on different identities.