You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过GitHub Actions在仓库A触发向仓库B创建Pull Request并修复报错

问题分析与修复方案

核心问题点

  • 默认GITHUB_TOKEN仅拥有当前仓库(A)的操作权限,无法访问仓库B
  • git push origin branchB推送到的是仓库A,而非目标仓库B
  • curl命令使用了中文引号,导致语法错误(exit code 3通常是curl参数格式错误)
  • 未正确拉取仓库A的test分支内容并推送到仓库B的新分支

修复后的工作流代码

name: Create PR to Repo B

on:
  push:
    branches: [test]

jobs:
  create-pr:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo A's test branch
        uses: actions/checkout@v3
        with:
          ref: test  # 明确拉取test分支内容

      - name: Configure Git credentials
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github.com"

      - name: Push Repo A content to Repo B's new branch
        env:
          REPO_B_URL: "https://${{ secrets.REPO_B_TOKEN }}@github.com/givvemee/bot-test2.git"
        run: |
          # 创建唯一命名的分支(用commit SHA避免重复冲突)
          git checkout -b auto-update-from-A-${{ github.sha }}
          # 添加仓库B的远程地址,通过PAT授权
          git remote add repo-b $REPO_B_URL
          # 推送新分支到仓库B
          git push repo-b auto-update-from-A-${{ github.sha }}

      - name: Create Pull Request via GitHub API
        env:
          REPO_B_TOKEN: ${{ secrets.REPO_B_TOKEN }}
          HEAD_BRANCH: "auto-update-from-A-${{ github.sha }}"
        run: |
          curl -X POST \
            -H "Accept: application/vnd.github.v3+json" \
            -H "Authorization: Bearer $REPO_B_TOKEN" \
            https://api.github.com/repos/givvemee/bot-test2/pulls \
            -d '{
              "title": "Auto-update from Repo A test branch",
              "body": "This PR is automatically created when Repo A test branch has new pushes.\nTriggered by commit: ${{ github.sha }}",
              "head": "'"$HEAD_BRANCH"'",
              "base": "main"
            }'

前置准备步骤

  1. 生成Personal Access Token(PAT):
    • 在GitHub账号设置中创建PAT,勾选repo权限(确保能读写仓库B)
  2. 添加Secret到仓库A:
    • 进入仓库A的Settings → Secrets and variables → Actions,新建名为REPO_B_TOKEN的Secret,值为上述生成的PAT

关键修改说明

  • 替换默认GITHUB_TOKEN为自定义PAT,获取仓库B的操作权限
  • 明确拉取仓库A的test分支内容,用commit SHA生成唯一分支名,避免重复推送时的分支冲突
  • 通过HTTPS地址+PAT的方式授权访问仓库B,完成分支推送
  • 修正curl命令的引号为英文,确保JSON格式合法

内容的提问来源于stack exchange,提问作者someone called jr dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 17:45:32