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

如何确保GitHub Actions缓存Post步骤在测试失败后仍运行

解决GitHub Actions缓存post步骤因测试失败被跳过的问题

方案一:捕获测试结果,确保job执行完所有步骤

这个方法既能保证缓存的post步骤正常运行,又不会掩盖测试失败的结果:

  • 给测试步骤添加id并设置continue-on-error: true,让job在测试失败后继续执行后续步骤(包括缓存的post步骤)
  • 在job末尾添加步骤,根据测试步骤的结果决定job最终状态

示例工作流片段:

jobs:
  ci:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm' # 启用.npm文件夹缓存

      - name: Cache node_modules
        uses: actions/cache@v4
        with:
          path: node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Install dependencies
        run: npm ci

      - name: Run tests
        id: test-step
        continue-on-error: true
        run: npm test

      # 最后根据测试结果设置job状态
      - name: Mark job as failed if tests failed
        if: steps.test-step.outcome == 'failure'
        run: exit 1

方案二:手动拆分缓存的恢复与保存操作

放弃依赖第三方action的自动post步骤,手动控制缓存的保存逻辑,强制在测试后执行保存,无论测试是否失败:

jobs:
  ci:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: false # 关闭setup-node的自动缓存,手动处理.npm文件夹

      # 恢复node_modules缓存
      - name: Restore node_modules cache
        id: restore-node-modules
        uses: actions/cache/restore@v4
        with:
          path: node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

      # 恢复.npm缓存
      - name: Restore .npm cache
        id: restore-npm
        uses: actions/cache/restore@v4
        with:
          path: ~/.npm
          key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-npm-

      - name: Install dependencies
        run: npm ci
        # 只有缓存未命中时才执行安装
        if: steps.restore-node-modules.outputs.cache-hit != 'true'

      - name: Run tests
        run: npm test

      # 保存node_modules缓存,无论测试是否成功
      - name: Save node_modules cache
        if: always()
        uses: actions/cache/save@v4
        with:
          path: node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}

      # 保存.npm缓存,无论测试是否成功
      - name: Save .npm cache
        if: always()
        uses: actions/cache/save@v4
        with:
          path: ~/.npm
          key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }}

这个方案完全手动控制缓存的恢复和保存,if: always()确保保存步骤不受测试结果影响,彻底解决post步骤被跳过的问题。

方案三:调整job的失败处理策略

如果job中没有其他需要提前终止的逻辑,可以设置job的continue-on-error为true,最后统一判断步骤结果,但这种方法可能掩盖其他步骤的失败,建议谨慎使用:

jobs:
  ci:
    runs-on: ubuntu-latest
    continue-on-error: true
    steps:
      # ... 前面的步骤和之前一致 ...

      - name: Run tests
        id: test-step
        run: npm test

      - name: Final job status
        if: steps.test-step.outcome == 'failure'
        run: exit 1

内容的提问来源于stack exchange,提问作者Arvind Kumar Sharma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 17:23:17