Github Actions workflow for merged/closed PRs

Lately I made some investigations on different events that you can use to trigger Github Actions workflows. I was especially interested in the pull request events as executing some clean up task as soon as a PR is merged was one of my goals.

Going through the list of available events, closed turned out to be what I was looking for. Additionally I added the requirement to make a distinction between the following two cases when closing a pull request.

Turns out the event received contains the pull request object which itself contains a lot of additional information such as whether the PR has been merged or not. Thus the following configuration is what I came up with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
name: Close Pull Request

# only trigger on pull request closed events
on:
  pull_request:
    types: [ closed ]

jobs:
  merge_job:
    # this job will only run if the PR has been merged
    if: github.event.pull_request.merged == true
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo PR #${{ github.event.number }} has been merged

  close_job:
    # this job will only run if the PR has been closed without being merged
    if: github.event.pull_request.merged == false
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo PR #${{ github.event.number }} has been closed without being merged

To see the above workflow in action check out this little showcase repo I created. Feel free to use it as a starting point.


Comments

If you have questions or want to give feedback feel free to contact me.