Neil Macy

GitHub Action to block a PR from merging

This is really handy. If you have an open GitHub PR that you don't want to merge yet, you can use a combination of labels and GitHub Actions to block the PR from merging:

name: Do Not Merge

on:
  pull_request:
    types: [synchronize, opened, reopened, labeled, unlabeled]

jobs:
  do-not-merge:
    if: ${{ contains(github.event.*.labels.*.name, 'do not merge') }}
    name: Prevent Merging
    runs-on: ubuntu-latest
    steps:
      - name: Check for label
        run: |
          echo "Pull request is labeled as 'do not merge'"
          echo "This workflow fails so that the pull request cannot be merged"
          exit 1

Using this Action, you just apply a label containing the text "do not merge" to your PR, and the PR will be blocked until the label is removed.

(This was copied directly from Jesse Squires's article on label-based GitHub Actions. There are other cool examples of actions on his site too. This version of the Action came from Ben Asher.)

Why?

There are a few good reasons not to want to merge a PR immediately. Maybe you want feedback but know you want to make changes before merging. Maybe you're holding off on meeting a big feature because of an impending release, or because of external dependencies that aren't quite ready, such as server side functionality.

You can always just avoid pressing the Merge button. But it's easy to press accidentally. And some teams have a proactive attitude towards merging. Sometimes a label can simply be missed, in spite of best intentions.

A label alone is a good indicator that something shouldn't be merged. But having a tool to enforce this is even better.

Published on 30 May 2022