pm2 将生产和暂存部署到具有不同应用程序名称的同一服务

pm2 deploy production and staging to same serve with different app names

我想在同一台服务器上使用不同的名称部署暂存和生产,但鉴于 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 作为选项,我是否可以实现此目的?

为什么不创建不同名称的不同应用?

[{
    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
    }
  }],

你也可以拆分成不同的文件。

我在我的项目中创建了不同的应用程序名称,但我没有使用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'
    }
  }
};