Trigger Github Actions only when PR is merged

Viewed 24881

I have a github actions yaml file as follows:

name: Test deployment
on:
  pull_request:
    branches:
    - master

jobs:
  deploy:
    runs-on: ubuntu-18.04
    steps:
    - name: Random name
      run: date

When I raise a PR from a branch to master branch, Github Action gets triggered. So, I updated my YAML to:

name: Test deployment
on:
  pull_request:
    types:
    - closed
    branches:
    - master

Now it is triggered when I merge the PR rather than while raising it. But it also gets triggered when I close the PR without merging it.

I found nothing like merged type in docs

Even the following syntax i tried does not work as expected:

jobs:
  ...
    if: github.event_name == 'pull_request' && github.event.action == 'closed'

Can anyone pls help me out here? Is it possible for me to check if the PR is approved by atleast one reviewer? (I can enable branch protection, but wanted to know if any option exists for doing it in github actions)

5 Answers

There is no pull-request-merged event.

The closest thing you can get is to subscribe to push event, since a merged PR will always create a push event to the branch it's being merged onto.

If you only care about PRs to master then you can specify that:

on:
  push:
    branches:
      - master

The other thing you can do is filter on each step individually

      - name: Do something
        if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true
        run: ...

(Edit, as of 2022) Per the GitHub docs, this should be possible now via:

on:
  pull_request:
    types:
      - closed

jobs:
  if_merged:
    if: github.event.pull_request.merged == true
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo The PR was merged

While there is no specific merged type for pull requests, there is however a merged flag on the API response. This allows you to run actions only on merges, rather than all pushes to the target branch.

This would allow one to do the following, assuming we want to run an action on any merge to the main branch:

on:
  pull_request:
    branches: 
      - main
    types: [closed]

jobs:
  my-action:
    if: ${{ github.event.pull_request.merged }}
    runs-on: ...

So, I just tried the proposed solution and looking at the output I see something strange: the workflow gets triggered but I see that it runs on the branch being merged, not on the branch being merged into (main in my case).
This is a bit disturbing as my workflow is building and pushing Docker images. What if there are changes on main that are not on the branch being merged? It seems to me that the image being pushed might not exactly be what I was expecting??
My workflow runs are here: https://github.com/Axual/ksml/actions - you can see that one workflow was run on main (triggered by push), the other on a branch.
Is there a way to run a workflow on a push that resulted from a PR merge, or something like that?

Once merged a PR request will commit with a message 'Merge pull request <your PR, etc>'. You can then use that with an if statement of your job.

name: build-on-merge

on:
  push:
    branches: ["master"]
jobs:
  build-on-merge:
    if: startsWith(github.event.head_commit.message, 'Merge pull request')
  runs-on: ubuntu-latest
  steps:
Related