Home > Software engineering >  Have a GitHub Action run when a PR is merged
Have a GitHub Action run when a PR is merged

Time:11-24

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?

CodePudding user response:

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
  • Related