Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 739
Shizzle Avatar asked Jul 11 '20 07:07

Shizzle


People also ask

How do I merge branches in GitHub?

In GitHub Desktop, click Current Branch. Click Choose a branch to merge into BRANCH. Click the branch you want to merge into the current branch, then click Merge BRANCH into BRANCH. Note: If there are merge conflicts, GitHub Desktop will warn you above the Merge BRANCH into BRANCH button.

Why GitHub Cannot automatically merge?

When you create a pull request from YourAccount\repo1 to OriginalAccount\repo1 (virtually from origin to upstream), seeing the message that you can't merge automatically means that OriginalAccount\repo1 has commits that YourAccount\repo1 doesn't have (commits that were most likely pushed after you forked).

What happens when you merge GitHub branches?

Merging is Git's way of putting a forked history back together again. The git merge command lets you take the independent lines of development created by git branch and integrate them into a single branch.

What happens when merging two branches?

Merging Branches. Once you've completed work on your branch, it is time to merge it into the main branch. Merging takes your branch changes and implements them into the main branch. Depending on the commit history, Git performs merges two ways: fast-forward and three-way merge.


1 Answers

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
like image 141
Max Avatar answered Oct 10 '22 14:10

Max