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

GitHub Actions启动应用后持续加载卡住问题求助

GitHub Actions 启动应用后任务持续挂起的问题

问题描述

我正在为Backend-Bug-Tracker仓库搭建GitHub Actions CI,当前使用的YAML配置如下:

# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
  push:
    branches: ["dev"]

jobs:
  DEV:
    runs-on: ubuntu-latest
    environment: DEV

    strategy:
      matrix:
        node-version: [18.x]
        # See supported Node.js release schedule at https://nodejs.org/en/about/releases/

    env:
      PORT: 8000
      MONGO_DB_URL_DEV: ${{ secrets.MONGO_DB_URL_DEV }}

    steps:
      - name: Check env
        run: echo $MONGO_DB_URL_DEV

      - name: Checkout code
        uses: actions/checkout@v3

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"

      - name: Install dependencies
        run: npm ci

      - name: Build
        run: npm run build

      - name: Start app
        run: npm run start

依赖安装与构建流程均执行成功,启动应用后GitHub输出如下:

Run npm run start
  
> bug-tracker@1.0.0 start
> ts-node src/server.ts
app is listening on 8000
mongo db connected: cluster0-shard-00-00.f3to2.mongodb.net

应用已正常监听端口并连接MongoDB,但GitHub Actions任务一直处于加载状态,最长已等待超1小时仍未停止,求解决方向。

解决方向

  • GitHub Actions任务依赖进程退出完成:npm run start启动的是长期运行的服务进程,该进程不会自动退出,导致Action一直等待进程结束而挂起。如果CI仅需验证应用能正常启动,可做如下调整:
    • 添加健康检查逻辑,确认服务正常响应后主动终止进程:
      npm run start &
      sleep 10 # 给服务预留启动时间
      curl http://localhost:8000/health # 假设存在健康检查接口
      pkill -f "ts-node src/server.ts"
      
    • 或者用timeout命令设置超时自动终止:
      timeout 30s npm run start
      
  • 明确CI的定位:如果需要持续运行服务,GitHub Actions虚拟机会在任务结束后销毁,不适合长期托管服务。这种场景应改用专门的部署平台托管服务,CI仅负责构建、测试和触发部署流程。
  • 排查后台进程:有时主服务启动成功后,可能存在未清理的子进程导致任务无法结束,可在步骤中添加进程检查命令,确保相关进程能被正确终止。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 04:43:10