Trigger workflow only on pull request MERGE #26724
-
I saw many posts about people asking if there is a way to trigger the workflow only on MERGE, which to me seems the most useful. However I did not see any answer, so I would like to find a solution. At the moment I am stuck with:
Which is not ideal, as it would trigger the entire process even if I dismiss or close the PR without merging and I cannot see any reason on why this should happen. I saw also:
but as my understanding this would run only for a particular action not on the entire workflow. So my question is: how can I trigger a workflow only on pull_request merge? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 26 comments 24 replies
-
same question |
Beta Was this translation helpful? Give feedback.
-
There’s no way to specify that a workflow should be triggered when a pull request is merged. However, because a merged pull request always results in a push, you can use the push event to accomplish your goal. For example, let’s say that you want to run a workflow whenever a pull request is merged to your master branch. You can do something like this:
This workflow will run when a PR is merged or when a commit is made directly to the master branch. You can list multiple branches if you’d like:
Or use a more sophisticated filter pattern. |
Beta Was this translation helpful? Give feedback.
-
For the event on.pull_request.closed , it is designed that whenever the PR is closed the workflow will be triggered. And currently, there is not a specialized event type for PR merged, such as on.pull_request.merged. You mentioned the value of the property github.event.pull_request.merged is true when you close the PR without merged. I tried both manual and the GitHub REST API Update a pull request to close the PR without merged, the value of the property merged always is false on my side. I also tried the API Get a single pull request, from the response body, the value of property “ merged ” is consistent with that in github context. What methods did you use to dismiss or close the PR? Maybe you also can try using the API Get a single pull request to check whether the value of property “ merged ” is consistent with that in github context. If the value is back to correct, as a workaround, you can use this property in the if conditionals to run all the jobs in the workflow only when the PR is closed with merged. For example:
This will still trigger the workflow but can skip the jobs in the workflow. |
Beta Was this translation helpful? Give feedback.
-
Even though I still would prefer a merge option, I think this makes sense. Thanks |
Beta Was this translation helpful? Give feedback.
-
I use this which triggers on merge:
I also separate out the push/pull workflows into separate files. |
Beta Was this translation helpful? Give feedback.
-
It’s work for me
|
Beta Was this translation helpful? Give feedback.
-
This can not work as expected. If the PR is closed without merging, the action will still run. |
Beta Was this translation helpful? Give feedback.
-
When I say “this” I mean:
That code only looks for PRs that have been closed - and a merge is not necessarily a close - a PR can be closed without merging - so it’s not sufficient to guard against that case. I prefer:
|
Beta Was this translation helpful? Give feedback.
-
I think this is the best solution |
Beta Was this translation helpful? Give feedback.
-
Hey folks, I wrote a little blog post + showcase repo that explain how to execute a certain job or step only if a PR has been merged and not just closed. Hope that helps 🙂 |
Beta Was this translation helpful? Give feedback.
-
This solution with the restriction to directly push commits to master solves the problem. |
Beta Was this translation helpful? Give feedback.
-
This is exactly what I was looking for. Thanks, Jen! |
Beta Was this translation helpful? Give feedback.
-
What if the workflow requires inputs before running? After PR merge, how is one supposed to get the inputs? |
Beta Was this translation helpful? Give feedback.
-
I prefer this solution. Most obvious |
Beta Was this translation helpful? Give feedback.
-
The documentation shows exactly that in this example: Events that trigger workflows - GitHub Docs |
Beta Was this translation helpful? Give feedback.
-
Here’s what our team is doing:
This triggers the build in the following cases:
The job condition says “Run this for everything EXCEPT non-merged closed PRs”. |
Beta Was this translation helpful? Give feedback.
-
A merge technically encompasses a commit “push”, targeting a specific branch (i.e., main). If you don’t target a branch, the workflow would trigger with any, including main. It’s like if you were to merge a feature branch into main locally and then push main to remote; all done on the remote in this case. This is exactly what the workflow team was thinking as well. This makes complete sense. |
Beta Was this translation helpful? Give feedback.
-
I have the same concern as @minherz where I'd like to run integration tests and screenshot tests only before merge because they are very expensive if triggered on every PR update. It's too late to run if the test runs after they are merged to master. |
Beta Was this translation helpful? Give feedback.
-
Maybe put another way, it would be great to have pull request merges work as transactions, with a mechanism to have failing workflows cause the transaction to rollback. Besides expensive tests, this could also be used to validate successful deploys. (The deployment process would have to be carefully designed to roll itself back.) Without this capability, it gets pretty complicated. I've never set something up like this myself, but I feel like you have to do something like "CommentOps", where you create a state variable to represent if a workflow has succeeded on the latest commit, you create a check that blocks merge of that state variable is false, you trigger that workflow with a comment, and you rerun the check when the workflow successfully completes. Maybe there's a better way to do this that I'm not aware of! |
Beta Was this translation helpful? Give feedback.
-
Since I posted my comment, I realize GitHub is about to release a new feature called merge queue. Looks like we will get a new event "merge_group" to trigger a set of tests before the PR is merged. I haven't tried it since the beta test has ended. Can someone help me confirm whether my assumption is correct? |
Beta Was this translation helpful? Give feedback.
-
on:
pull_request:
branches:
- main
types:
- merged madness. I just want it. |
Beta Was this translation helpful? Give feedback.
-
@say8425 have you already tried this? https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#running-your-pull_request_target-workflow-when-a-pull-request-merges |
Beta Was this translation helpful? Give feedback.
-
I have the following:
and
I hope it helps someone |
Beta Was this translation helpful? Give feedback.
-
One way that works for me is:
|
Beta Was this translation helpful? Give feedback.
-
Just want to add my 5 cents: the solution was just to add the following - name: Login to Docker Hub
if: github.ref == 'refs/heads/main'
- name: Build and push Docker image
if: github.ref == 'refs/heads/main' |
Beta Was this translation helpful? Give feedback.
-
I wanted to also add my particular use case. I own a repository that is used as a flux source for kubernetes deployments. I have multiple different services running and I want to be able to send out notifications when a service is about to be deployed (which happens upon pushing to main). The caveat here is that since I have merge-queue enabled, more than one PR can be merged at the same time, and I only notify updates based on the services touched by the commit which is responsible for triggering the event, so the merge-queue defeats it (I know that an option here would be to limit the merge-queue to 1 PR at a time, but should it come to that? and also need to consider that depending on the time it takes on the merge-queue tests this could already be a bottleneck in itself). The alternative that I have already played around with are by parsing the The other use case I have is that depending on given labels from a PR, I want to perform some specific actions or not. On-push triggers don't really have a connection between the commit itself and which PR generated it (we restrict pushes to main, so it's guaranteed that it can only come from a PR). How to achieve that? For now I simply try to list the PRs that have been recently merged and try to pair with based on the list of files changed. All that being said, I reckon I could change the approach to use:
And leverage the |
Beta Was this translation helpful? Give feedback.
This solution with the restriction to directly push commits to master solves the problem.