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:
on: pull_request: types: [closed]
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:
github.event.pull_request.merged == true
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?
Solved! Solved! Go to Solution.
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:
on: push: branches: - master
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:
on: push: branches: - master - develop
Or use a more sophisticated filter pattern.
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.
jobs: build: if: github.event.pull_request.merged == 'true' . . .
This will still trigger the workflow but can skip the jobs in the workflow.
I use this which triggers on merge:
- name: pull request if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true