如何跳过由github-actions[bot]发起PR合并触发的GitHub Actions工作流?
解决GitHub Actions跳过机器人PR合并触发的工作流问题
方案一:通过GitHub API查询关联PR的作者(最可靠)
当main分支的push是合并PR产生的,GitHub会为合并提交保留关联PR的信息,我们可以通过GitHub API查询该提交对应的PR,再检查PR的创建者是否为github-actions[bot]。
具体实现步骤
在你的工作流中添加前置步骤,使用actions/github-script调用API查询关联PR,根据结果决定是否跳过后续任务:
on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: 检查是否来自机器人创建的PR合并 id: check-bot-pr uses: actions/github-script@v7 with: script: | // 获取当前提交的SHA const commitSha = context.sha; // 查询该提交关联的PR const pulls = await github.rest.repos.listPullRequestsAssociatedWithCommit({ owner: context.repo.owner, repo: context.repo.repo, commit_sha: commitSha }); // 检查是否有PR,且PR作者是github-actions[bot] const isBotPR = pulls.data.some(pr => pr.user.login === 'github-actions[bot]'); // 设置输出变量供后续步骤使用 core.setOutput('is_bot_pr', isBotPR); if (isBotPR) { core.info('该提交来自机器人创建的PR合并,将跳过工作流'); } - name: 执行主要任务(仅当不是机器人PR时运行) if: steps.check-bot-pr.outputs.is_bot_pr == 'false' run: | echo "执行正常的工作流任务..." # 这里替换成你的实际任务命令
方案二:给机器人PR添加标识(需配合机器人工作流)
如果能控制机器人创建PR的工作流,可以在创建PR时添加特定标识,比如自定义标签或提交信息前缀,后续在push事件中通过这些标识判断是否跳过。
示例:给机器人PR添加标签
在机器人创建PR的工作流中,添加步骤为PR打上标签:
# 机器人创建PR的工作流 jobs: create-pr: runs-on: ubuntu-latest steps: - name: 创建PR并添加标签 uses: actions/github-script@v7 with: script: | const pr = await github.rest.pulls.create({ owner: context.repo.owner, repo: context.repo.repo, head: 'bot-branch', base: 'main', title: '自动更新依赖', body: '由github-actions[bot]自动创建的更新PR' }); // 给PR添加自定义标签 await github.rest.issues.addLabels({ owner: context.repo.owner, repo: context.repo.repo, issue_number: pr.data.number, labels: ['bot-generated'] });
然后在main分支的push工作流中,通过API查询关联PR的标签:
# main分支的push工作流 jobs: build: runs-on: ubuntu-latest steps: - name: 检查PR是否含机器人标签 id: check-bot-label uses: actions/github-script@v7 with: script: | const commitSha = context.sha; const pulls = await github.rest.repos.listPullRequestsAssociatedWithCommit({ owner: context.repo.owner, repo: context.repo.repo, commit_sha: commitSha }); const hasBotLabel = pulls.data.some(pr => pr.labels.some(label => label.name === 'bot-generated') ); core.setOutput('has_bot_label', hasBotLabel); - name: 执行主要任务 if: steps.check-bot-label.outputs.has_bot_label == 'false' run: echo "执行正常任务..."
方案三:检查合并提交的父提交分支名(备选)
如果机器人创建的PR分支有固定命名规则(比如bot/*),可以通过查询合并提交的父提交所属分支来判断:
jobs: build: runs-on: ubuntu-latest steps: - name: 获取合并提交的父分支 id: check-parent-branch run: | # 获取合并提交的两个父SHA PARENTS=$(git show --pretty=format:"%P" $GITHUB_SHA) # 遍历父提交,检查是否来自bot分支 for SHA in $PARENTS; do BRANCH=$(git branch --contains $SHA | grep -E 'bot/' | head -n1) if [ -n "$BRANCH" ]; then echo "is_bot_pr=true" >> $GITHUB_OUTPUT exit 0 fi done echo "is_bot_pr=false" >> $GITHUB_OUTPUT - name: 执行主要任务 if: steps.check-parent-branch.outputs.is_bot_pr == 'false' run: echo "执行正常任务..."
注意:这个方法依赖分支命名规则,若分支名被修改可能失效,仅作为备选方案。
最佳实践建议
- 优先使用方案一:通过API直接查询PR作者,是最准确且不依赖额外配置的方法,只要PR是
github-actions[bot]创建的就能准确识别。 - 避免依赖提交作者:人工合并PR时,提交作者会显示为合并者,而非PR创建者,所以检查提交作者的方法不可靠。
- 权限配置:确保GitHub Actions有足够权限查询PR信息,默认的
GITHUB_TOKEN已经具备仓库的读写权限,无需额外配置。
内容的提问来源于stack exchange,提问作者Tharun
相关产品推荐
相关产品推荐

