pull_request action does not run on merge

@amq just complementing, the other answers… your “if” condition needs “event” object, it could be:

name: CI

on:
pull_request:
types: [closed]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - name: Pull request merged
      if: github. **event**.pull_request.merged == true
      run: echo merged
3 Likes

Thanks for fixing this, I’ve tried it and works as expected

Would it make sense to have a PR event of merged?

10 Likes

Hi,

I am using

on:
  pull_request:
    types: [closed]

for my workflow trigger, without any conditionals, but it is still not triggering when a PR merges. The PR is being merged by a different GitHub Action that merges it using GitHub API with

uses: actions/github-script@0.2.0

Could that be a reason it wouldn’t trigger?  If I manually merge a PR, it triggers as expected.

I’ve also tried

on: push

where the merge does a push, but same results. A manual merge triggers the workflow based on push, but the merge via API and the github actions bot does not trigger the workflow in question…

2 Likes

I have the exact same problem as @rcasperson-jc:

Manually merging a pull request triggers my Action, but using the Github API in an other Action to merge a pull request does not trigger my Action.

The only difference I can see is that the merge in the latter case is done by the user github-action (bot).

Is it by design / a limitation / a bug of Github Actions?

Okay I figured out why my Action wouldn’t start.

As stated in the doc: “An action in a workflow run can’t trigger a new workflow run” (Events that trigger workflows).

I had a script in a step that did some changes to my repo with the ${{secrets.GITHUB_TOKEN}} parameter. I expected those changes to trigger another workflow but  they didn’t.

By using a personal Github token instead I was able to make it work because now the user responsible for repo changes is not github-actions who obviously cannot trigger workflow, but me.

Hope it’ll save some time to people

Any Success to fire the event for merge only ?

I can’t get pull_request to fire for anything. I’ve tried

on: pull_request

on: [pull_request]

and

on:
  pull_request:
    types: [opened, edited, read_for_review]

and none of those fire when I open a new pull request or edit an existing one.

How about on PR merge? 

2 Likes

This is weird, because I think it would be the most important to have. On PR closed the workflow would be triggered also also in case we reject the PR which is not ideal.

So not sure if this is helpful but for the use case where you

a) Would like some tests to run when you (or one of your team) opens a PR
b) Would like to deploy the npm package when you merge the PR I have found the following works well.

In your .github/workflows directory have 2 files ci.yaml and cd.yaml

#ci.yaml (for running test on PR open, edit, reopen and synchronise)

on:
  pull_request:
    types: [opened, reopened, edited, synchronize]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Use Node.js 12.x
        uses: actions/setup-node@v1
        with:
          registry-url: https://npm.pkg.github.com/
          node-version: 12.x
          scope: "@{your-scope}"
      - name: npm install, build, and test
        run: |
          npm install
          npm run build --if-present
          npm test

#cd.yaml

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Use Node.js 12.x
        uses: actions/setup-node@v1
        with:
          registry-url: https://npm.pkg.github.com/
          node-version: 12.x
          scope: "@{your-scope}"
      - name: npm install and build
        run: |
          npm install
          npm run build --if-present
      - name: publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
        run: |
          npm run deploy

Has the added benefit of deploying the package if an admin bypasses the PR process for an urgent hotfix type scenario.

Hope this helps

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