借助GitHub Actions合并两个私有仓库至第三仓库遇权限问题
解决GitHub Actions合并私有仓库时的“仓库找不到”错误
你遇到的remote: Repository not found错误,本质不是仓库地址错误,而是第三个仓库的Workflow在拉取私有仓库时没有携带认证令牌——GitHub对无权限访问的私有仓库会返回404,伪装成“仓库不存在”。你给令牌配置了repo和workflow权限,但第三个仓库的git操作根本没用到这个令牌,导致权限不足。
具体修复步骤
- 同步令牌到第三个仓库Secrets:把你之前创建的
MY_SECRET_TOKEN添加到ThirdRepo的仓库Secrets中(路径:仓库Settings → Secrets and variables → Actions → New repository secret)。 - 修改第三个仓库的Workflow配置:在添加remote仓库时,将令牌嵌入到HTTPS地址中,让git操作自动带上认证信息。修改后的完整配置如下:
name: Merge Changes to Third on: repository_dispatch: types: - event_name_first - event_name_second jobs: merge: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v2 - name: Configure git run: | git config --global user.name "github-actions" git config --global user.email "github-actions@users.noreply.github.com" - name: Add authenticated remote repositories run: | # 嵌入令牌到HTTPS地址,实现私有仓库认证 git remote add first https://${{ secrets.MY_SECRET_TOKEN }}@github.com/MyOrganisation/first.git git remote add second https://${{ secrets.MY_SECRET_TOKEN }}@github.com/MyOrganisation/second.git - name: Fetch latest changes from first or second run: | if [[ "${{ github.event.action }}" == "event_name_first" ]]; then git fetch first main git merge first/main --no-edit elif [[ "${{ github.event.action }}" == "event_name_second" ]]; then git fetch second main git merge second/main --no-edit fi - name: Push to main run: | git push origin main
额外检查点
- 确认令牌所属的GitHub用户,对
first、second、ThirdRepo三个私有仓库都拥有读写权限; - 确认令牌的
repo权限是完整勾选的(包括repo:status、repo_deployment等所有子权限); - 检查ThirdRepo的Secrets中,
MY_SECRET_TOKEN的内容和前两个仓库完全一致,无拼写错误。
内容的提问来源于stack exchange,提问作者BigPocc
相关产品推荐
相关产品推荐

