Can't push to protected branch in GitHub Action

Viewed 4297

I have a GitHub action that I have created in order to create a new version and publish it for our JS repo. It looks similar to this

steps:
  # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
  - uses: actions/checkout@v2

  - uses: actions/setup-node@v1
    with:
      node-version: 12.18.3
      registry-url: https://npm.pkg.github.com/
      scope: '<redacted>'

  - name: Install Dependencies
    run: npm ci

  - name: Build
    run: npm run build

  - name: Bump Version & Push
    run: |
      git config --local user.email "<redacted>"
      git config --local user.name "<redacted>"
      npm version patch
      git push https://${{ secrets.KEY }}@github.com/<redacted> HEAD:master --follow-tags

The KEY that I am using is a person access token I created from my account. I have set the repo so that I have push access to the master branch. When I try the push command from my machine with the access token it works without an issue. However every time I see this in the GitHub Action

remote: error: GH006: Protected branch update failed for refs/heads/master.        
remote: error: You're not authorized to push to this branch. Visit https://docs.github.com/articles/about-protected-branches/ for more information.

I have been racking my brain trying to figure this out and I'm out to ideas. If I remove the branch protection this action works fine.

1 Answers

I think this is because of how authentication is persisted by actions/checkout. It's stored in an extraheader config option which takes precedence over the credentials you are setting manually.

Try not persisting the auth:

  - uses: actions/checkout@v2
    with:
      persist-credentials: false

Or alternatively:

  - uses: actions/checkout@v2
    with:
      token: ${{ secrets.KEY }}

I know this because I've had my own issue with overriding this config option in the past.

Related