Trigger Github Actions only when PR is merged
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you want a GitHub Actions workflow to run only after a pull request is actually merged, the trigger needs two parts. First, listen for the PR being closed. Second, check whether that closure happened because of a merge rather than a manual close.
Using the pull_request Closed Event
The right event is pull_request with types: [closed]. GitHub emits that event for both merged pull requests and pull requests that were simply closed without merging, so the workflow still needs a condition.
The if expression is the key line. Without it, the job would run for any closed pull request, including abandoned ones.
Restricting the Workflow to a Target Branch
Many repositories only want this workflow when a PR is merged into a specific branch such as main. Add a branch filter in the event configuration and keep the merged check at the job level.
This workflow runs only when the base branch of the pull request is main and the PR was merged.
Choosing Between pull_request and push
A common question is whether push to main is enough. Sometimes it is. If every change to main arrives through merged pull requests, a push workflow can effectively act as a post-merge workflow.
However, push has different semantics:
- It also runs for direct pushes.
- It does not expose pull request metadata in the same way.
- It is harder to distinguish merge commits from other pushes cleanly.
If your logic depends on PR details such as labels, reviewers, or the merged flag itself, use pull_request with types: [closed].
Accessing Merge Metadata
Once the workflow is triggered from a merged PR, you can use fields from the event payload. For example, the merge commit SHA and PR title are often useful.
This is helpful for release notes, deployment notifications, or branch-specific automation after code review has completed.
Common Pitfalls
The most common mistake is putting the merged check under on: instead of under a job or step if: condition. Event filters decide when the workflow starts, but merged is part of the event payload and must be evaluated after the event fires.
Another issue is forgetting that closed does not mean merged. A PR closed by hand still emits the same event type.
It is also easy to create duplicate automation by using both a pull_request merged workflow and a push workflow on the target branch without coordinating their responsibilities. If both run the same deployment, you will get double execution.
Summary
- Trigger on
pull_requestwithtypes: [closed]to capture merge completion. - Add
if: github.event.pull_request.merged == trueso the job runs only for merged PRs. - Use branch filters if the workflow should only apply to merges into a specific branch.
- Prefer
pull_requestoverpushwhen you need PR metadata in the workflow. - Watch for duplicate runs if you also have
pushautomation on the destination branch.
Related reading
- Triggering Azure DevOps builds based on changes to sub folders
- Trying to start the kubernetes in Docker-Desktop but it's stuck
- Two clusters on EKS, how to switch between them
- Two conflicting long lived process managers
- Troubleshooting misplaced .git directory nothing to commit
- Trying to git clone via SSH but getting broken pipe error
- UDP send and receive in kubernetes
- Unable to access NGINX nodePort service in K8 cluster running on RPI

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.