Getting Error While pushing Changes to my own Public repo through github actions

While pushing changes to my own public repo through GitHub actions, I'm getting this error.

remote: Permission to spooky/repo.git denied to github-actions[bot].
fatal: unable to access 'https://github.com/spooky/repo/': The requested URL returned error: 403
Error: Process completed with exit code 128.

The commit code in actions was

     - name: Commit Output files
        run: |
              git config remote.origin.url 'https://[email protected]/spooky/repo/'
              git config --local user.email "[email protected]"
              git config --local user.name "spook"
              git init
              git add .
              git commit -m "Updated"
              git push origin main

- name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: github_token

I tried with git config remote.origin.url 'https://username:[email protected]/spooky/repo/' also, Getting the same error(the GitHub token has all permissions).

Can anyone help me to fix this?


I also had the same problem. I solved it by adding persist-credentials: false, fetch-depth: 0 to the workflow.

jobs:
  build:
    runs-on: ubuntu-latest
#    permissions:
#      contents: write
#      packages: write

    steps:
      - uses: actions/checkout@v2
        with:
          persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token.
          fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository.

      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'
          server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
          settings-path: ${{ github.workspace }} # location for the settings.xml file


      - name: Commit files
        run: | 
          git config remote.origin.url https://github.com/<username>/<repo>.git
          git config --global user.name "$(git --no-pager log --format=format:'%an' -n 1)"
          git config --global user.email "$(git --no-pager log --format=format:'%ae' -n 1)"
          git add -A
          git commit -am "update"

      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}

      - name: Build with Maven
        run: mvn -B package --file pom.xml

      - name: Publish to GitHub Packages  Apache Maven
        run: mvn deploy -s $GITHUB_WORKSPACE/settings.xml
        env:
          GITHUB_TOKEN: ${{ github.token }}

You can also achieve this by changing the contents: read to write without adding persist-credentials and fetch-depth.

    permissions:
      contents: write
      packages: write