如何触发@github-actions机器人创建的PR中的所有检查?
GitHub Actions创建PR后触发检查的可行方案
先解决权限报错问题
你遇到的"Resource not accessible by integration"是因为默认GITHUB_TOKEN权限不足,无法操作PR检查或评论事件。先从权限配置入手:
- 进入仓库
Settings → Actions → General,在「Workflow permissions」中选择「Read and write permissions」,让默认token拥有足够操作权限; - 或者在单个Workflow的
jobs节点下显式声明权限:permissions: contents: write pull-requests: write actions: write
方案一:在创建PR的Action中直接触发检查
无需额外评论触发,在cherry-pick并创建PR的Workflow末尾,直接调用GitHub API触发检查,避免多Action联动的麻烦:
- 假设你用步骤输出了PR编号
pr_number,添加以下步骤:
- name: Trigger PR checks run: | curl -X POST \ -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.create_pr.outputs.pr_number }}/check-runs
- 或者通过编辑PR标题触发检查(无突兀日志):
- name: Refresh PR to trigger checks run: | # 先给标题加标记 curl -X PATCH \ -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.create_pr.outputs.pr_number }} \ -d '{"title":"${{ steps.create_pr.outputs.pr_title }} [refresh]"}' # 再改回原标题 curl -X PATCH \ -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.create_pr.outputs.pr_number }} \ -d '{"title":"${{ steps.create_pr.outputs.pr_title }}"}'
方案二:修复评论触发的Action逻辑
如果坚持用评论触发,调整Workflow配置如下:
name: Trigger PR Checks via Comment on: issue_comment: types: [created] jobs: trigger-checks: runs-on: ubuntu-latest # 仅响应PR下的指定评论(比如/re-run-checks) if: github.event.issue.pull_request && contains(github.event.comment.body, '/re-run-checks') permissions: pull-requests: write actions: write steps: - name: Re-run PR checks uses: actions/github-script@v6 with: script: | const prNumber = context.issue.number; // 请求重新审查(触发检查) await github.rest.pulls.createReview({ owner: context.repo.owner, repo: context.repo.repo, pull_number: prNumber, event: 'request_review', reviewers: ['github-actions[bot]'] }); // 或者直接重新触发最近一次Workflow运行 await github.rest.actions.reRunWorkflow({ owner: context.repo.owner, repo: context.repo.repo, run_id: context.runId });
内容的提问来源于stack exchange,提问作者hbelmiro
相关产品推荐
相关产品推荐

