无法使用 GitHub Actions 强制推送到主分支

问题描述 投票:0回答:2

错误:

! [remote rejected]     main -> main (refusing to allow a GitHub App to create or update workflow `.github/workflows/docker.yml` without `workflows` permission)

经过一番研究,我了解到我需要授予它必要的权限。我不知道从哪里以及如何授予它许可。我也没有太多经验。

我应该如何修复这个错误?这是因为我强制对存储库进行更改,因为正常推送被拒绝。

我非常清楚我一开始就不应该使用

--force
。既然我需要推动他们,我就有义务这样做。我在推送之前从 main 中拉取,这会导致需要手动解决的冲突,而我不想这样做。
如果有任何解决办法,我也很乐意实施。

代码:

name: Update Fork

on:
  workflow_dispatch:
  schedule:
    - cron: '0 10 * * *' # runs every everyday at 10:00

jobs:
  update_fork:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout Forked Repo
      uses: actions/checkout@v2
      with:
        repository: username/repo_name
        ref: main

    - name: Setup Git
      run: git config --global user.email ${{ secrets.EMAIL }} && git config --global user.name user_name

    - name: Check for Upstream Changes
      run: |
        git remote add upstream https://github.com/upstream_user_name/upstream_repo

        upstream_commit=$(git ls-remote --heads upstream | grep main | awk '{print $1}')
        # forked_commit=$(git rev-list --max-count=1 HEAD)
        forked_commit=$(git log -n 10 --pretty=%H)

        # if git rev-list $forked_commit..HEAD | grep -q $upstream_commit; then
        if echo "$forked_commits" | grep -q "$upstream_commit"; then
          echo "No commits to be synced!"
        else
          git fetch upstream
          git pull --rebase -X ours upstream main
          git push -f origin main

          echo "Rebase successful!"
        fi

此代码未能推送提交后,我添加了以下代码:

      with:
        repository: username/repo_name
        ref: main
        repo-token: ${{ secrets.GITHUB_TOKEN }}

这也没有解决问题。我很确定这是错误的..

git github yaml github-actions workflow
2个回答
1
投票

根据错误,您的 git 用户似乎无权更新工作流程。

似乎没有明确定义的

workflows
权限,但 workflow 有一个 OAuth 范围。

尝试将此权限添加到您的工作流程中。

permissions:
  actions: write

如果这不起作用,请尝试这个,我之前需要将其添加到提交回存储库的工作流程中。

permissions:
  contents: write

0
投票

我不确定以上答案是否解决了问题。我遇到了类似的问题,发现这个有效:

https://stackoverflow.com/a/75175628/22765977

© www.soinside.com 2019 - 2024. All rights reserved.