PR重新打开时触发GitHub Action的问题及同步方案咨询
GitHub Action在PR重新打开时的条件判断与状态同步问题
问题场景
我需要在PR(拉取请求)打开或重新打开时触发GitHub Action(GHA),PR打开时的GHA运行正常,但重新打开PR时必须先执行检查并等待一段时间,再执行常规安装步骤——因为删除步骤耗时较长,如果直接重装,重装步骤会误判软件仍已安装而提前退出,后续删除完成后会导致安装状态异常。
当前使用的工作流配置如下:
on: pull_request: types: [opened, reopened] jobs: wait-on-pr-reopening: runs-on: ubuntu-latest if: github.event.pull_request.reopened == true steps: - name: Wait for deletion to complete before proceeding with reinstallation run: | # here there's some logic to check that the GHA that runs when PR is closed is done install-app: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - run: | # here goes the normal installation GHA that should be executed at PR reopening too, # but only after the deletion is completed
核心问题
if: github.event.pull_request.reopened(已尝试带== true和不带的写法)对应的任务总是被跳过,想问这个过滤值是否正确?还有其他方法可以在重新打开PR时同步状态吗?
解决方案
1. 修正条件判断逻辑
你用的github.event.pull_request.reopened字段是不存在的,这就是任务被跳过的根源。PR事件触发时,判断当前操作类型应该用github.event.action字段:
- PR打开时,
github.event.action的值是opened - PR重新打开时,
github.event.action的值是reopened
所以需要把wait-on-pr-reopening任务的if条件改成if: github.event.action == 'reopened',同时给install-app任务添加依赖,让它在PR重新打开时必须等等待任务完成后再执行。
修正后的完整配置:
on: pull_request: types: [opened, reopened] jobs: wait-on-pr-reopening: runs-on: ubuntu-latest if: github.event.action == 'reopened' steps: - name: 等待删除任务完成后再执行重装 run: | # 这里写检查PR关闭时触发的GHA是否完成的逻辑 install-app: runs-on: ubuntu-latest # PR打开时直接执行,重新打开时等待wait-on-pr-reopening完成 needs: [wait-on-pr-reopening] if: github.event.action == 'opened' || github.event.action == 'reopened' steps: - uses: actions/checkout@v3 - run: | # 这里写常规的安装逻辑,PR打开和重新打开时都会执行
2. 其他状态同步方法
如果需要更可靠的状态同步,还可以试试这些方式:
- 通过GitHub API查询工作流状态:如果删除任务是同仓库的工作流,可以用
gh命令行工具(需要给GHA配置仓库权限)查询该工作流的运行状态,在等待任务里轮询,直到删除任务标记为成功才继续。 - 用状态标记同步:在删除任务完成后,写入一个状态文件到仓库或者缓存中,等待任务轮询这个标记,直到检测到删除完成的信号再执行安装。
内容的提问来源于stack exchange,提问作者Alberto Chiusole
相关产品推荐
相关产品推荐

