为何PM2在GitLab CI部署时自动创建source目录并迁移文件?
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 processedsourcedirectory after runningpost-deployscripts).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:
- App Configuration: Set
cwdto/home/ubuntu/projet2/current/—this is where PM2 expects your live code to reside (via the symlink). Using a relativescriptpath (./index.js) keeps things clean and tied to the working directory. - Post-Deploy Script: The
post-deploycommand runs inside thesourcedirectory after cloning. By referencing thecurrentdirectory's ecosystem file, you ensure PM2 uses the correct paths for your live app.
Alternative: Skip the Default Structure (Not Recommended)
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

