pull_request action does not run on merge

I have the following workflow, but the closed pr not trigerring the workflow.

name: Cleanup Review App Resources

on:
  pull_request:
    types: [closed]

jobs:
  delete-review-app:
    runs-on: ubuntu-latest
    name: Delete Heroku Review App
    steps:
      - env:
          HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
        run: |
          HEROKU_APP_NAME=`./scripts/appname.sh ${GITHUB_REF##*/}`
          heroku apps:destroy $HEROKU_APP_NAME --confirm=$HEROKU_APP_NAME

this will trigger when pull request is closed without being merged

So how can we run an action if someone clicks on the merge button in a pull request in order to perform some checks? Under certain circumstance in need the action to fail and deny the merge of the pull requests. Is this possible?

Really? In my case, it triggers _only_ when the PR is merged and not when it is closed without merging…

Hi @thejoebourneidentity - in one of my repos this does not trigger when the PR is closed without being merged - is this expected?

It does not work for merging the PR. There is no event for it. I tested it and no action is ever run when the PR is merged. It would be nice to have such a functionality so you can do some checks and prevent the merge from happening.

Check again. There is only an event type for “close” which is triggerend when the PR is closed manually. If you merge it the action won’t run.

I was able to accomplish this using this solution: https://github.community/t5/GitHub-Actions/How-to-trigger-different-action-only-on-merge/m-p/56019/highlight/true#M9719

@thejoebourneidentity This doesn’t seem to work.

image

In the above screenshot, we used to have a branch merge trigger our workflow.
But, we changed from push to pull_request in commit @Demmonius CI - Automatically run LightHouse (on PR commits) which broke that behaviour.

I tried your solution using closed, but it didn’t run the workflow either when I merged the PR 107 into 86. (I want to trigger the workflow on 86 when any PR is merged into it)

(You can know it didn’t work, because there is no :white_check_mark: close by the commit ID)

Is there any workaround for this? I kinda need to use pull_request because the LightHouse action depends on it.


Edit: I tried another variant, but doesn’t work either.

image

This is how I got it working. The closed event does trigger the workflow. That’s not the problem. But the merged field might not be available or parsed differently. It’s definitely not ‘true’ in my case. So I used merged_by instead. merged_by isn’t matching null though but simply an empty string if unmerged.

Here’s the working example:

on:
  pull_request_target:
    types:
      - closed

jobs:
  on_merged:
    if: github.event.pull_request.merged_by != ''

Commit: https://github.com/gofunky/update-codeowners/commit/14ff47b40a38a927cb5f895b11e698dd58af5e55
Triggered before merge (by label): here
Triggered on merge: here
Triggered after merge (by labeling merged PR): here