Running a job on pull request merge in specific branch

How to write a GitHub action workflow in which, a job is run only when a pull request is merged and that too in a specific branch say dev.

Something like this should work:

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

jobs:
  deploy:
    runs-on: ubuntu-latest
    if: github.event.pull_request.merged

    steps:
      - name: checkout
        uses: actions/checkout@v1
        with:
          ref: dev
      - name: Other steps
        <do your stuff here...>

Notice the on and if parts. This workflow only runs on a CLOSED pull request (which has to be specifically set as Actions do not trigger on the closed event by default) and the job only runs when the Pull Request that triggered this event was acutally merged.

2 Likes