Search code examples
githubgithub-actions

How to change repo's default branch with GitHub Actions?


Right now I have a workflow that bumps the version when something is merged to the main branch and creates a new release/$VERSION branch and opens a PR for that branch. The problem is, to make it nicer, I would like to also change the default branch to this new branch just created.

What I have so far:

name: version
on:
  push:
    branches:
      - main

jobs:
  version:
    name: Generate new version
    runs-on: ubuntu-latest
    permissions: write-all
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Install jq
        uses: dcarbone/install-jq-action@v2
        with:
          version: '1.7'

      - name: Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 18.x
          cache: yarn

      - name: Create PR
        run: |
          yarn
          yarn bump:minor
          VERSION=$(jq -r .version package.json)
          BRANCH=release/$VERSION
          git checkout -b $BRANCH
          git commit -am "Release $VERSION"
          git push --set-upstream origin $BRANCH
          gh pr create --title "Release $VERSION" --body "" --head $BRANCH --base main
          gh repo edit --default-branch $BRANCH
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

The problem is that the very last step, where I try to change the default branch, I always get this message: HTTP 403: Resource not accessible by integration

Is this not supported by GitHub Actions? If so, is there any other way that I can make this to work?


Solution

  • That will require the repo:admin permission, which the GitHub Actions token won't get.

    You can work around this by using a Personal Access Token or a GitHub App: