允许GitHub Actions在受保护的分支上合并PR。

4
我已经配置了我的存储库,使得 GitHub actions 能够批准 PR。

Screenshot 2022-11-01 at 11 44 10 AM

我有分支保护规则,要求在合并之前进行1次批准。

enter image description here

然而,接下来的步骤失败了。
      - name: perform the merge if applicable
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        shell: bash
        run: |
            echo "Approving PR..."
            gh pr review --approve ${{ github.event.issue.number }}
            echo "Merging PR..."
            gh pr merge ${{ github.event.issue.number }} --admin --squash

(这个)公关确实被批准了,但合并失败了。
Approving PR...
Merging PR...
Message: You're not authorized to push to this branch. Visit https://docs.github.com/articles/about-protected-branches/ for more information., Locations: [{Line:1 Column:58}]

如何允许 github-actions 机器人合并 PR?
更新1:
移除了 --admin 标志,以防万一。
Approving PR...
Merging PR...
X Pull request #199 is not mergeable: the base branch policy prohibits the merge.
To have the pull request merged after all the requirements have been met, add the `--auto` flag.
To use administrator privileges to immediately merge the pull request, add the `--admin` flag.

更新2

我已经将以下权限添加到GITHUB_ACTIONS令牌中,但没有任何效果

permissions:
 contents: write
 pull-requests: write
 repository-projects: write

你尝试从gh pr merge命令中删除--admin标志了吗?据我所知,GH action无法使用其默认的GH_TOKEN执行管理员任务。 - tjarbo
刚刚尝试了一下。请查看我在问题中的更新。 - pkaramol
好的,太棒了。我猜第一个“权限被拒绝”与管理员标志有关。由于我无法看到您定义的要求,是否可能仍然存在必需的操作,例如linting或testing?您尝试过--auto标志吗? - tjarbo
当然,请尝试仅使用 --auto 标志。另外,您对分支策略有哪些要求? - tjarbo
禁止直接推送到受保护的分支;我不认为这会对它产生影响,因为它不是直接推送。 - pkaramol
显示剩余3条评论
1个回答

0

默认的GITHUB_TOKEN没有管理员权限,您需要使用具有管理员权限的用户的自定义令牌进行更改。

示例:

    jobs:
      Merge_PR_Example:
        runs-on: ubuntu-latest

        permissions:
          contents: write
          pull-requests: write
          repository-projects: write

        env:
          GH_TOKEN: ${{ secrets.ADMIN_RIGHTS_TOKEN }}

        steps:
          - uses: actions/checkout@v3

          - name: Merge PR
            run: gh pr merge ${{ github.event.issue.number }} --admin --squash
            env:
              GH_TOKEN: ${{ secrets.ADMIN_RIGHTS_TOKEN }}

选择令牌的repowrokflow作用域。这已经足够了。

Selected scopes for the token


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