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:withtypes: [closed]andbranches: [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.shGotchas
- A plain push to main also lands merges from the CLI; add
on.pushif you want those too. - Squash and rebase merges still set
merged == true, so the flag is the reliable signal.
Related guides
How to Trigger a Workflow on Push to Specific Branches in GitHub ActionsRun a GitHub Actions workflow only when commits are pushed to named branches using on.push.branches, so featu…
How to Trigger a Workflow Only for a Specific Actor in GitHub ActionsRestrict a GitHub Actions job to a specific user or bot with an if check on github.actor, so only trusted act…