我想在具有不同名称的同一台服务器上部署暂存和生产,但是鉴于pm2生态系统文件的文档,我仍然看不到要实现这一目标。这是我下面的ecosystem.config.js
:
module.exports = {
apps : [{
name: 'frontend',
script: 'server/index.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '256M',
env: {
NODE_ENV: 'development'
},
env_staging: {
NODE_ENV: 'staging',
PORT: 3001
},
env_production: {
NODE_ENV: 'production',
PORT: 3002
}
}],
deploy : {
production : {
user : '<redacted>',
host : ['<redacted>'],
ref : 'origin/master',
repo : '<redacted>',
path : '<redacted>/production',
'pre-deploy': 'git fetch --all',
'post-deploy' : 'npm install -d && npm run build:production && pm2 reload ecosystem.config.js --env production',
'post-setup' : 'npm install -d && npm run build:production && pm2 reload ecosystem.config.js --env production'
},
staging : {
user : '<redacted>',
host : ['<redacted>],
ref : 'origin/development',
repo : '<redacted>',
path : '<redacted>/staging',
'pre-deploy': 'git fetch --all',
'post-deploy' : 'npm install -d && npm run build:staging && pm2 reload ecosystem.config.js --env staging',
'post-setup' : 'npm install -d && npm run build:staging && pm2 reload ecosystem.config.js --env staging'
}
}
};
考虑到deploy
的配置没有给name
作为选择,我是否仍然可以实现这一目标?
答案 0 :(得分:1)
那为什么不使用差异名称创建其他应用?
[{
name: 'frontendDev',
script: 'server/index.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '256M',
env: {
NODE_ENV: 'development'
},
}, {
name: 'frontendStag',
script: 'server/index.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '256M',
env_staging: {
NODE_ENV: 'staging',
PORT: 3001
},
},{
name: 'frontendProd',
script: 'server/index.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '256M',
env_production: {
NODE_ENV: 'production',
PORT: 3002
}
}],
您还可以将其拆分为diff文件。
答案 1 :(得分:1)
我在项目中使用了不同的应用程序名称,但我不使用pm2 deloy,希望您可以根据环境对相同的应用程序名称使用相同的方式,
首先,您需要从npm脚本运行deloy命令,因为您可以将env附加到它)
{
"scripts": {
"deloy:staging": "cross-env NODE_ENV=staging pm2 deploy ecosystem.config.js staging",
"deloy:prod": "cross-env NODE_ENV=production pm2 deploy ecosystem.config.js production",
},
"devDependencies": {
"cross-env": "^5.2.0",
}
}
然后仅使用NODE_ENV在ecosystem.config.js中创建不同的应用程序名称:
const name = 'frontend_' + process.env.NODE_ENV
module.exports = {
apps : [{
name: name,
script: 'server/index.js',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '256M',
env: {
NODE_ENV: 'development'
},
env_staging: {
NODE_ENV: 'staging',
PORT: 3001
},
env_production: {
NODE_ENV: 'production',
PORT: 3002
}
}],
deploy : {
production : {
user : '<redacted>',
host : ['<redacted>'],
ref : 'origin/master',
repo : '<redacted>',
path : '<redacted>/production',
'pre-deploy': 'git fetch --all',
'post-deploy' : 'npm install -d && npm run build:production && pm2 reload ecosystem.config.js --env production',
'post-setup' : 'npm install -d && npm run build:production && pm2 reload ecosystem.config.js --env production'
},
staging : {
user : '<redacted>',
host : ['<redacted>],
ref : 'origin/development',
repo : '<redacted>',
path : '<redacted>/staging',
'pre-deploy': 'git fetch --all',
'post-deploy' : 'npm install -d && npm run build:staging && pm2 reload ecosystem.config.js --env staging',
'post-setup' : 'npm install -d && npm run build:staging && pm2 reload ecosystem.config.js --env staging'
}
}
};