通过 Github Actions 推送更改到自己的公共仓库时出现错误

6
在通过 GitHub Actions 推送更改到我自己的公共仓库时,我遇到了这个错误。
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.

Actions 中的提交代码为:

     - name: Commit Output files
        run: |
              git config remote.origin.url 'https://github_token@github.com/spooky/repo/'
              git config --local user.email "spook@gmail.com"
              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

我尝试过使用 git config remote.origin.url 'https://username:github_token@github.com/spooky/repo/',但是仍然遇到了相同的错误(GitHub token 具有所有权限)。

有人能帮我解决这个问题吗?

2个回答

6

我也遇到了同样的问题。我通过在工作流中添加persist-credentials: false, fetch-depth: 0来解决它。

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 }}

你可以通过更改内容来实现此目的:将阅读改为写入,而不添加 "persist-credential" 和 "fetch-depth"。
    permissions:
      contents: write
      packages: write

“permissions”选项运作良好,整洁且功能符合其描述。 - altius_rup

0

首先检查一下使用相同的令牌在本地(而不是通过 GitHub 操作工作流程在 GitHub 上)是否可以正常完成 git push

git clone @github.com/spooky/repo
cd repo
# make a dummy commit
git push https://username:github_token@github.com/spooky/repo

如果您正在使用 ad-m/github-push-action,则您的操作中不需要 git push 命令,因为它会自动为您执行。
 - name: Push changes
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: ${{ github.ref }}

我也遇到了同样的问题,使用令牌在本地可以工作,但在 Github Action 工作流中无法工作。该令牌具有所有权限。 - Mani
@Mani Strange。这是作用域问题吗?(https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps) - VonC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接