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

Github Actions无法识别pm2的ecosystem.config.js文件求助

问题描述

项目根目录结构:

  • actions_runner
  • ecosystem.config.js
  • nodesource_setup.sh
  • server

本地在根目录运行前后端服务,仅通过Github Actions处理客户端,但Actions执行时抛出错误:

File ecosystem.config.js not found

推测是因为Actions在actions-runner目录内无法定位到该配置文件,不知道如何让Github Actions正确访问它。

Github Actions配置文件:

name: CI

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    runs-on: self-hosted
    strategy:
      matrix:
        node-version: [16.x]

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm install
    - run: npm run build --if-present
    - run: pm2 restart ecosystem.config.js --only client

ecosystem.config.js内容:

{
      name: 'strapi',
      cwd: 'myPath',
      script: 'npm',
      args: 'start',
      env: {
        NODE_ENV: 'production',
      },
    },
    {
      name: 'client',
      cwd: '/myPath',
      script: 'npm',
      args: 'start',
      env: {
        NODE_ENV: 'production',
      },
解决方案

1. 验证文件是否被正确拉取

actions/checkout@v3默认会将代码拉取到自托管Runner的工作目录,而非actions-runner文件夹。先添加一步查看当前目录结构,确认文件是否存在:

- run: ls -la

2. 使用绝对路径指定配置文件

如果文件确实在项目根目录,直接用绝对路径调用pm2即可。可以借助Github Actions内置的GITHUB_WORKSPACE环境变量(指向当前工作目录的绝对路径):

- run: pm2 restart $GITHUB_WORKSPACE/ecosystem.config.js --only client

或者手动指定项目的绝对路径,比如:

- run: pm2 restart /home/your-user/your-project/ecosystem.config.js --only client

3. 检查自托管Runner的工作目录配置

如果自定义了Runner的工作目录,查看actions-runner/.env文件中的RUNNER_WORKDIR值,确保actions/checkout将代码拉取到了包含ecosystem.config.js的目录下。

4. 修复ecosystem.config.js的语法错误

你的配置文件存在语法问题:两个对象直接用逗号分隔,没有包裹在数组中,也缺少闭合的括号和导出语句。语法错误可能导致pm2误报“文件未找到”,正确格式如下:

module.exports = [
  {
    name: 'strapi',
    cwd: 'myPath',
    script: 'npm',
    args: 'start',
    env: {
      NODE_ENV: 'production',
    },
  },
  {
    name: 'client',
    cwd: '/myPath',
    script: 'npm',
    args: 'start',
    env: {
      NODE_ENV: 'production',
    },
  }
];

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 09:03:19