Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger Github Actions only when PR is merged

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)

like image 656
Srikanth Sharma Avatar asked Mar 16 '20 17:03

Srikanth Sharma


People also ask

What happens when a pull request is merged?

Merging occurs once a developer's submitted pull request has been approved by the repository maintainer. Before merging, the repository maintainer will need to review the developer's completed work. They will comment on any issues, and request edits from the developer as needed.

What happens when I merge a pull request in GitHub?

Pull requests are merged using the --no-ff option, except for pull requests with squashed or rebased commits, which are merged using the fast-forward option. You can link a pull request to an issue to show that a fix is in progress and to automatically close the issue when someone merges the pull request.


1 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

like image 155
Michael Parker Avatar answered Dec 18 '22 14:12

Michael Parker