Shizzle
Shizzle

Reputation: 1011

Perform Github Action when trying to merge branch

I'm setting up Github actions for a few of my projects. The flow I'd like to achieve is:

  1. A developer clicks on the "Merge pull request" button
  2. A Github action testing workflow will take place
  3. If the tests pass - The merge is executed

The reason for this kind of flow, is I wouldn't like the tests to run on each commit pushed to the branch. I want the flow to run only when trying to merge.

My question is: Is there a way to manually execute a workflow only when trying / wanting to merge, and making sure the branch can be merged into master if and only if the tests have passed?

Upvotes: 18

Views: 20724

Answers (2)

fenix
fenix

Reputation: 329

UPDATE 2023-03-08

Github now has merge queues in beta, which allow you to trigger a workflow when a PR is added to a merge group. Using that functionality you should be able to trigger a workflow when the PR is added to the merge queue. You can also specify that the merge should fail if the tests fail.

Upvotes: 12

Max
Max

Reputation: 9889

Unfortunately, there's no merged or merge_attempt activity type on the pull request event (yet). Even if there was, I don't believe GitHub has a way to block merges on the completion of a workflow (yet).

What I would suggest as a workaround here is to run your test 1. after the fact on pushes to the master branch, and 2. on pull_request events with certain activity types which indicate that the user is likely to attempt a merge soon. For example, ready_for_review or review_requested.

Something like this:

name: tests
on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
    types: 
      - ready_for_review
      - review_requested

Upvotes: 6

Related Questions