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

Github Actions CI执行TypeScript转JS构建时卡住该如何排查?

故障原因

  1. 构建命令使用了开发环境的常驻进程
    你配置的npm run convert同时启动两个不会自动退出的进程:
  • tsc -w是TypeScript的监听模式,启动后会持续监控文件变化,不会主动结束
  • nodemon是热重载开发工具,启动服务后会持续运行,监听文件变更重启服务
    CI步骤需要命令执行完成返回退出码才会标记为结束,两个常驻进程一直运行,直接导致步骤卡住。
  1. 流水线步骤顺序错误
    你当前的Actions流程是「拉取代码→部署到Heroku→安装依赖编译」,部署操作执行时还没有生成JS构建产物,推送到Heroku的是原始TS代码,无法正常运行。

修复方案

第一步:修改package.json新增生产构建命令

在scripts中添加一次性编译的构建命令,不要带watch模式:

{
  "scripts": {
    "tsc": "tsc",
    "build": "tsc", // 新增生产构建命令,仅执行一次编译
    "convert:build": "tsc -w",
    "convert:run": "nodemon backend-build/index.js",
    "convert": "concurrently npm:convert:*",
    "start": "node backend-build/index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  }
}

你本地开发仍然可以使用原来的npm run convert,CI环境使用npm run build做编译。

第二步:调整GitHub Actions流程顺序

把编译步骤放到部署之前,保证推送到Heroku的代码包含构建好的JS产物:

name: Deploy

on:
  push:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      # 先安装依赖、编译
      - name: install dependencies & build
        run: |
          npm install
          npm run build
      # 再执行部署操作
      - uses: akhileshns/heroku-deploy@v3.12.12
        with:
          heroku_api_key: ${{secrets.HEROKU_API_KEY}}
          heroku_app_name: "app"
          heroku_email: "a.com"

可选优化

你也可以把npm run build配置到package.json的postinstall脚本中,这样Heroku在安装依赖后会自动执行编译,不需要在CI中做构建操作,更符合Heroku的常规使用流程。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 14:54:04