HomeSegun Adebayo

Push to protected branch from Github actions

#github-actions
#ci
Segun Adebayo

Segun Adebayo

1 min read
Push to protected branch from Github actions

While working to improve the CI workflows in Chakra UI, we wanted to create a Github action that updates the changelog anytime we release a new version. The github action looked like:

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout branch
        uses: actions/checkout@v2

We noticed that this action step kept failing because you can't push to a protected branch (we had protected the `main` branch to prevent unwanted mistakes).

It turns out that, with the default setup used in `actions/checkout@v2`, you can't `git push` from your GitHub actions if the repository has branch protection turned on.

After a few hours of research, here's how I resolved it:

  • Ensure you have admin-level repo permissions
  • Create a personal access token (PAT) with "repo" permissions.
    Go to "Settings" > "Developer Settings" > "Personal access tokens"
  • Confirm that administrators are not included in branch protection rules
  • Use that PAT in the `actions/checkout@v2`step

The new workflow looks like this

jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout branch
        uses: actions/checkout@v2
        with:
          token: ${{ secrets.GH_PAT }}

and viola! We can now push to the protected main repo via Github actions.


Segun Adebayo

Written by Segun Adebayo (Sage)

Sage is a Github Star 🌟 and Design Engineer 👨🏽‍💻. He is passionate about helping people build an accessible web faster. Sage is the author of Chakra UI, a React UI library for building accessible experiences.