Nanu
Nanu

Reputation: 73

Have a GitHub Action run when a PR is merged

I am looking for a way to have a GitHub Action run when a PR is merged, the GitHub Action has to grab the PR description and store the data somewhere for later use.

Is there any way to do this through GitHub Actions without an API or webhook?

Upvotes: 6

Views: 9111

Answers (2)

rethab
rethab

Reputation: 8413

There are two approaches: Either run the workflow when a PR is closed with merge=true or run a workflow on the target branch if you know all pushes to the target branch go through a PR.


Run on PR Closed

You can trigger an action when a PR is closed like so:

on:
  pull_request:
    types: [closed]

The above event is triggered whether a PR was merged or closed without merging. Therefore, you still need to check that flag when running a job:

my_job:
  build:
    if: github.event.pull_request.merged == true

Run on Target Branch

If you know all your PRs are merged into main and users cannot directly push to main, you might as well trigger your workflow on push events on main like so:

on:
  push:
    branches:
      - main

Upvotes: 10

Christian Kasim Loan
Christian Kasim Loan

Reputation: 422

Answer is great but slightly outdated, using 'true' did not work for me.

The following did the trick for me:

jobs:
  publish:
    if: github.event.pull_request.merged == true

Docs on this: jobs.<job_id>.if

Upvotes: 0

Related Questions