GitHub Status Checks and Branch Protection Made Easy
Tags: github, opensource, node
Published: 2024-02-07 17:00:00

GitHub provides a suite of options, allowing you to determine specific criteria to be met before a pull request can be merged into your repository. This article will outline how to configure a [branch protection rule](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/defining-the-mergeability-of-pull-requests/managing-a-branch-protection-rule) that requires pull requests to be reviewed, and a [status check](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/collaborating-on-repositories-with-code-quality-features/about-status-checks) to ensure tests are run in a variety of environments. In less time than it takes to drink a cup of coffee, you can dramatically reduce the risk of rogue merges wreaking havoc on your code.

## 🛡 Protect Your Branch

The first step to guarding the kingdom is to add a [branch protection rule](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/defining-the-mergeability-of-pull-requests/managing-a-branch-protection-rule). Adding a branch protection rule allows you to specify a list of criteria that must be met to merge a change into a protected branch. Specifying a list of criteria that must be met before a merge allows you to formalize and automate repetitive tasks that are necessary to ensure quality contributions to your repo. Branch protections are free for public repos but require a Pro subscription to be added to private repos.

Add your first rule by clicking the Settings tab on your repos’ GitHub page and navigating to the Branches section. Take note of your Default Branch (usually main) and click Add Rule:

![GitHub Repo Branch Settings Menu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gx0nbqqc9m66q7gwzogj.png)

Enter `main` into the Branch name pattern input and hit Enter or click Save Changes:

![GitHub Branch Protection Rule
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik4p1cg94t906h3qd96t.png)

Congrats! You have just defined `main` as a protected branch. In the next step, you will begin to define the criteria that must be met for changes to be merged into your protected branch.

## 👀 Require Reviews

When a review is required, someone with push access to your repo must give an open pull request their blessing before the pull request can be merged into a protected branch. Requiring a pull request review is a great way to reduce the number of bugs introduced, as it ensures someone with experience working in your repo has inspected the change and asked questions where appropriate.

To require pull requests be reviewed, click the checkbox next to **Require a pull request before merging**. Be sure to also click the checkbox next to “Require approvals”:

![Require Approvals for Pull Requests](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1gzecbvei8w9hojcjq20.png)

Awesome! When a pull request is opened, the **Merge** button will be disabled until at least one contributor has reviewed and approved the changes. In the final step, we will add a [status check](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/collaborating-on-repositories-with-code-quality-features/about-status-checks) to ensure that a specific action has been run successfully.

## 🦿 Run Tests

Running all the tests in your repository for any change contributors propose is important. Running tests for each pull request reduces the likelihood of regressions and other bugs being introduced to your code and ensures a higher quality in the code merged to the protected branch. If you’re interested in learning more about testing, check out this [article](https://dev.to/bobbyg603/getting-started-with-unit-testing-software-in-2024-1g72) for a crash course on the basics of unit testing.

To configure a status check that leverages GitHub Actions to run your tests, create a `.github/workflows/ci.yml file`. The following gist contains a workflow that will run tests for a Node.js app:

```yml
# Based on https://github.com/actions/starter-workflows/blob/main/ci/node.js.yml
name: CI
on:
  pull_request:
    branches:
      - main
jobs:
  ci:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v2
      with:
        node-version: lts/*
        cache: 'npm'
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test
```

If you’re not developing with Node.js you’ll have to modify `ci.yml` to work with your platform. Here are some links that outline how to run tests with GitHub Actions on other platforms: [.NET](https://medium.com/r/?url=https%3A%2F%2Fdocs.github.com%2Fen%2Factions%2Fautomating-builds-and-tests%2Fbuilding-and-testing-net), [Python](https://medium.com/r/?url=https%3A%2F%2Fdocs.github.com%2Factions%2Fautomating-builds-and-tests%2Fbuilding-and-testing-nodejs-or-python%3FlangId%3Dpy), [Ruby](https://medium.com/r/?url=https%3A%2F%2Fdocs.github.com%2Fen%2Factions%2Fautomating-builds-and-tests%2Fbuilding-and-testing-ruby), [Swift](https://medium.com/r/?url=https%3A%2F%2Fdocs.github.com%2Fen%2Factions%2Fautomating-builds-and-tests%2Fbuilding-and-testing-swift), and [Java](https://medium.com/r/?url=https%3A%2F%2Fdocs.github.com%2Fen%2Factions%2Fautomating-builds-and-tests%2Fbuilding-and-testing-java-with-gradle).

To require a clean test run before a pull request can be merged, navigate back to your repo’s **Settings** page. Click the checkbox next to **Require status checks to pass before merging**. Search for `ci` in the **Search for status checks in the last week for this repository** input field and click the search result to enable the status check:

![Require Status Checks for Protected Branches](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gcnlssux2mz9fgngo578.png)

Nice! If you open a pull request, you should notice that the merge button won’t turn green until the status checks are complete and the pull request has been reviewed:


![Pull Request after Enabling a Protected Branch with Reviews and Status Checks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1mspp9vxydu2c5o69p3.png)

---

**Want to Connect?**

If you found the information in this tutorial useful, please follow me on [X](https://x.com/bobbyg603).