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

为何PM2在GitLab CI部署时自动创建source目录并迁移文件?

Why PM2 Creates a source Directory During Deployment & How to Fix Your Path Issue

Hey there, let's clear up what's going on with your PM2 deployment and get your app running from the right directory.

First off: the automatic source directory creation is normal behavior for PM2 deploy—it's part of how PM2's deployment workflow is designed. Here's the breakdown:

PM2's deploy system uses a structured directory setup by default when you specify a path in your ecosystem.config.js. For your path: '/home/ubuntu/projet2/', PM2 will create three key directories there:

  • source: Where your Git repository gets cloned directly. This is the raw, unprocessed code from your repo.
  • current: A symbolic link that points to the latest deployed version (usually the processed source directory after running post-deploy scripts).
  • shared: For persistent files like logs, environment variables, or uploads that you don't want overwritten on deploy.

The issue here is that your app configuration is pointing to /home/ubuntu/projet2/ directly, but PM2 is actually placing your code in /home/ubuntu/projet2/source. That's why pm2 show projet2 shows the working directory as the source folder—your app is trying to run from the raw cloned repo instead of the intended deployment target.

How to Fix This

Adjust your ecosystem.config.js to align with PM2's default directory structure:

module.exports = {
  apps: [{
    name: 'projet2',
    script: './index.js', // Use relative path since we'll set cwd to current
    cwd: '/home/ubuntu/projet2/current/' // Point to the symlinked latest version
  }],
  deploy: {
    production: {
      user: 'ubuntu',
      host: 'xxxxxxxxxxxx',
      ref: 'origin/master',
      repo: 'git@gitlab.com:xxxxxxx/projet2.git',
      key: './pk.pem',
      path: '/home/ubuntu/projet2/',
      // post-deploy runs inside the source directory, so use relative commands
      'post-deploy': 'npm install && pm2 startOrRestart /home/ubuntu/projet2/current/ecosystem.config.js'
    }
  }
}

Key Changes Explained:

  1. App Configuration: Set cwd to /home/ubuntu/projet2/current/—this is where PM2 expects your live code to reside (via the symlink). Using a relative script path (./index.js) keeps things clean and tied to the working directory.
  2. Post-Deploy Script: The post-deploy command runs inside the source directory after cloning. By referencing the current directory's ecosystem file, you ensure PM2 uses the correct paths for your live app.

If you really want to avoid the source directory, you can override PM2's deploy behavior by customizing the deploy section with pre-deploy/post-deploy scripts that manually move files to your desired directory. However, this defeats the purpose of PM2's built-in versioning and rollback capabilities—so it's better to work with the default structure if possible.

Quick Troubleshooting Tip

Run pm2 deploy ecosystem.config.js production --verbose to see detailed logs of the deployment process. This will show you exactly how PM2 is creating directories and executing scripts, which can help spot any misconfigurations.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:08:02