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

React应用多Job的GitHub Workflow因制品未共享执行失败求助

问题分析与解决方案

问题根源

  1. 测试Job仅下载了build目录,但运行npm run test需要项目源码、package.json、依赖包(node_modules)等核心文件,这些资源并不在编译后的build目录中,直接导致npm找不到package.json报错。
  2. test:watch是交互式监听模式,会持续等待用户输入,完全不适合CI环境运行,会导致流程挂起超时。

修正后的Workflow配置

name: Project name

on:
  push:
    branches:
      - "main"

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20  # 替换为你的项目实际Node版本
          cache: 'npm'

      - name: Install dependencies and build
        run: npm install && npm run build

      - name: Upload build artifact
        uses: actions/upload-artifact@v3
        with:
          name: build-artifact
          path: build

  unit-tests:
    # 若测试无需依赖构建产物,可移除`needs: build`,让build和test并行执行以节省时间
    needs: build
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'

      - name: Install dependencies
        run: npm install

      - name: Run unit tests
        run: npm run test  # 替换为项目中单次执行测试的命令

  deploy:
    needs: unit-tests
    runs-on: ubuntu-latest

    steps:
      - name: Download build artifact
        uses: actions/download-artifact@v3
        with:
          name: build-artifact

      - name: Deploy 🚀
        uses: JamesIves/github-pages-deploy-action@v4
        with:
          folder: build

关键修改说明

  • 测试Job独立拉取资源:测试是针对源码的验证,必须重新拉取项目代码、安装依赖,不能仅依赖编译后的build产物。
  • 替换测试命令:移除交互式的test:watch,改用单次执行的测试命令,适配CI非交互式环境。
  • 依赖缓存优化:通过actions/setup-node的缓存功能,复用已下载的依赖包,加快后续步骤执行速度。
  • 简化产物流转:deploy直接从build Job下载产物,无需经过test Job中转,减少冗余操作。
  • 更新Action版本:将actions/checkout等基础Action更新到最新稳定版,提升兼容性与安全性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 11:30:59