Skip to content
Latchkey

How to Trigger a Workflow Only on a Merge to Main in GitHub Actions

A merge is a closed pull request where merged == true, so gate the closed event on that flag instead of every close.

Add pull_request with types: [closed] and branches: [main], then gate the job on github.event.pull_request.merged == true so closing without merging does nothing.

Steps

  • Add pull_request: with types: [closed] and branches: [main].
  • Gate the deploy job on github.event.pull_request.merged == true.
  • A PR closed without merging is skipped.

Workflow

.github/workflows/deploy.yml
on:
  pull_request:
    types: [closed]
    branches: [main]
jobs:
  deploy:
    if: github.event.pull_request.merged == true
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: ./deploy.sh

Gotchas

  • A plain push to main also lands merges from the CLI; add on.push if you want those too.
  • Squash and rebase merges still set merged == true, so the flag is the reliable signal.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →