使用GitHub Actions创建PR时遭遇权限错误求助
解决方案
1. 显式配置工作流权限
在你的update任务中添加permissions字段,明确授予推送分支和创建PR的权限:
jobs: update: runs-on: ubuntu-latest permissions: contents: write # 允许推送分支到仓库 pull-requests: write # 允许创建Pull Request
2. 统一分支名称变量
你当前在多个步骤中重复生成分支名称,会因步骤执行的时间差导致推送的分支与PR引用的分支名称不匹配,这会引发错误。改用步骤输出的变量:
- 修改推送分支的步骤:
- name: Push to remote branch run: git push -u origin ${{ steps.date.outputs.date }}
- 修改创建PR的步骤:
- name: Create pull request run: gh pr create -B master -H ${{ steps.date.outputs.date }} --title "Merge upstream master into ${{ steps.date.outputs.date }}" --body "Created by GitHub Action" env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3. 确认仓库Workflow权限设置
再次检查仓库的全局Workflow权限配置:
- 进入仓库 → Settings → Actions → General
- 在"Workflow permissions"区域,选择Read and write permissions
- 确保勾选Allow GitHub Actions to create and approve pull requests
4. 优化分支创建逻辑(可选)
当前使用git switch --orphan创建的孤儿分支会丢失原有仓库历史,若不需要这种效果,建议改为基于master分支创建新分支:
- name: Checkout new branch from master id: date run: | BRANCH_NAME=$(date +%Y-%m-%d-%H-%M-%S) git checkout -b $BRANCH_NAME master echo "::set-output name=date::$BRANCH_NAME"
合并上游代码时添加--no-edit参数自动生成提交信息:
- name: Merge upstream changes run: git merge upstream/master --no-edit
内容的提问来源于stack exchange,提问作者Timmy Suntichaiwakin
相关产品推荐
相关产品推荐

