Calmcode - github actions: prevent merge

Prevent a Bad Merge in GitHub Actions

1 2 3 4 5 6

When you add a GitHub Action that runs unit tests then you'll get warnings when the checks don't pass. But you may still be able to merge the PR. You can prevent this from happening by setting up branch protection rules.

Setting up Branch Protection Rules

To set up branch protection rules, first go to the repository settings.

From there, go to branches.

This is where you can set up branch protection rules. In this case, we are going to add a rule for our main branch.

Make sure that we're adding a rule for the main branch and select the checkbox that reads "Require status checks to pass before merging". For good measure you might also want to select the "Require branches to be up to date before merging" checkbox as well.

You can now select the GitHub actions steps that you'd like to enforce. In our case the name of the job is build. If you look at the .yml file that defines our jobs you should be able to find it.

name: Python Unit Tests

on:
  pull_request:
    branches:
    - main

jobs:
  build: # This is the name we're interested in!
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.7'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install -r requirements.txt
        python -m pip install -r dev-requirements.txt
    - name: Test with pytest
      run: |
        pytest --verbose

Selecting the job.

This is what we see after selecting the right job.

The final thing to do is to scroll down and hit "save changes". This will configure your repository to prevent accidental merges from taking place.