GitHub Actions中PR任务成功但未创建PR且无内容推送求助
问题描述
配置了一个GitHub Actions工作流,目标是通过Gradle任务从.proto文件生成Kotlin文件,并将生成文件推送到另一个仓库。工作流执行到Verify pushed branch步骤均正常,分支和提交已成功创建,但Create Pull Request步骤未能创建PR,也无相关输出。
工作流文件如下:
name: Generate Kotlin on: push: branches: [ "main" ] pull_request: branches: [ "main" ] workflow_dispatch: jobs: generate-kotlin: runs-on: ubuntu-latest steps: - name: Checkout repo uses: actions/checkout@v4 - name: List directory structure before building run: ls -R - name: Generate Protos run: ./gradlew generateProtos - name: List directory structure after building run: ls -R - name: List generated Kotlin files for debugging run: ls -R lib/build/generated/source/wire/ || echo "Directory not found" - name: Upload generated Kotlin files uses: actions/upload-artifact@v4 with: name: generated-kotlin-files path: lib/build/generated/source/wire/**/*.kt - name: Checkout target repo uses: actions/checkout@v4 with: repository: 'ksharma-xyz/Kotlin-Proto' token: ${{ secrets.TOKEN }} - name: Download artifact uses: actions/download-artifact@v4 with: name: generated-kotlin-files path: ./generated - name: List downloaded files run: ls -R ./generated - name: Configure Git run: | git config --global user.name 'github-actions[bot]' git config --global user.email 'github-actions[bot]@users.noreply.github.com' git config --global init.defaultBranch main - name: Create a new branch run: git checkout -b kotlin - name: Copy generated files to target repo run: | mkdir -p kotlin touch test.txt cp -r ./generated/* kotlin/ ls -R kotlin/ || echo "Copy failed" - name: Create Commit run: | git add . git commit -m "Kotlin Files" - name: Push changes to target repo run: git push --set-upstream origin kotlin - name: Verify pushed branch run: | git fetch git checkout kotlin git log -1 - name: Create Pull Request uses: peter-evans/create-pull-request@v6 with: token: ${{ secrets.TOKEN }} commit-message: "Add generated Kotlin files from ksharma-xyz/Proto-API" branch: kotlin title: "Add generated Kotlin files from main project" body: "This PR adds the generated Kotlin files from the main project." base: main committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> author: ksharma-xyz <55722391+ksharma-xyz@users.noreply.github.com> signoff: false delete-branch: false draft: false
问题排查与修复方案
核心原因
peter-evans/create-pull-request 动作的设计逻辑是基于当前工作目录的未提交/未推送变更来创建PR。你的工作流中已经手动执行了git push,将分支推送到远程仓库,此时本地工作目录没有待推送的变更,动作检测不到触发条件,因此不会执行PR创建操作。
修复方案
方案1:让create-pull-request 处理提交与推送
移除手动的git commit和git push步骤,由该动作自动处理提交、分支创建和PR创建,确保动作能识别到本地变更:
# 移除以下手动提交推送步骤: # - name: Create Commit # run: | # git add . # git commit -m "Kotlin Files" # # - name: Push changes to target repo # run: git push --set-upstream origin kotlin # # - name: Verify pushed branch # run: | # git fetch # git checkout kotlin # git log -1 # 保留并直接使用该动作: - name: Create Pull Request uses: peter-evans/create-pull-request@v6 with: token: ${{ secrets.TOKEN }} commit-message: "Add generated Kotlin files from ksharma-xyz/Proto-API" branch: kotlin title: "Add generated Kotlin files from main project" body: "This PR adds the generated Kotlin files from the main project." base: main committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> author: ksharma-xyz <55722391+ksharma-xyz@users.noreply.github.com> signoff: false delete-branch: false draft: false
该动作会自动检测本地变更,创建分支、提交并推送,然后生成PR。
方案2:手动触发PR创建(针对已推送的分支)
如果需要保留手动推送步骤,可以使用GitHub API直接创建PR,替代create-pull-request动作:
- name: Create Pull Request via GitHub API run: | curl -X POST \ -H "Authorization: token ${{ secrets.TOKEN }}" \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/ksharma-xyz/Kotlin-Proto/pulls \ -d '{ "title": "Add generated Kotlin files from main project", "body": "This PR adds the generated Kotlin files from the main project.", "head": "kotlin", "base": "main" }'
额外注意事项
- 若
kotlin分支已存在,无论是哪种方案都可能失败。建议改用动态分支名(比如kotlin-generated-${{ github.sha }}),避免分支冲突。 - 确保
secrets.TOKEN拥有目标仓库的repo权限,能够创建分支和PR。
内容的提问来源于stack exchange,提问作者Karan Sharma
相关产品推荐
相关产品推荐

