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

Github Actions如何获取步骤状态以实现CI工作流失败检查?

如何在GitHub Actions中获取步骤状态并统一检查失败

我之前也踩过这个坑!你遇到的问题核心是GitHub Actions上下文的引用方式不对——job.steps并不是合法的上下文,而且你用的status字段也不是正确的步骤状态属性。

关键知识点:步骤状态的正确引用方式

GitHub Actions里,要获取某个步骤的执行状态,需要用steps.<step-id>.outcome或steps.<step-id>.conclusion,两者的区别很重要:

  • outcome:代表步骤实际的执行结果,成功是success,失败是failure,不受continue-on-error的影响。
  • conclusion:代表步骤最终的结论,如果设置了continue-on-error: true,哪怕步骤实际失败(outcome为failure),conclusion也会被标记为success;如果没有设置continue-on-error,conclusion和outcome结果一致。

你的场景是让失败的步骤继续执行,最后统一检查哪些步骤实际失败了,所以应该用outcome来判断。

修改后的工作流配置

把你的if条件改成正确的引用方式即可:

name: CI
on: [pull_request]
jobs:
  myjob:
    runs-on: ubuntu-latest
    steps:
      - name: Step 1
        id: hello
        run: <any>
        continue-on-error: true
      - name: Step 2
        id: world
        run: <any>
        continue-on-error: true
      - name: Check on failures
        # 检查任意步骤实际执行失败
        if: steps.hello.outcome == 'failure' || steps.world.outcome == 'failure'
        run: exit 1

进阶技巧:批量检查所有步骤

如果你的步骤很多,不想逐个写条件,可以用通配符语法(GitHub Actions 2.3.0及以上版本支持):

- name: Check all steps for failures
  if: contains(steps.*.outcome, 'failure')
  run: exit 1

这样就能自动检查所有步骤的实际执行结果,只要有一个步骤失败,就会触发最后一步的exit 1,把整个任务标记为失败。

内容的提问来源于stack exchange,提问作者Maxim

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:35:04