首页 > 解决方案 > pm2 将生产和登台部署到具有不同应用程序名称的相同服务

问题描述

我想在具有不同名称的同一台服务器上部署登台和生产,但鉴于 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作为选项提供,我是否可以实现这一点?

标签: pm2

解决方案


那么为什么不创建具有不同名称的不同应用程序呢?

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

您还可以拆分为差异文件。


推荐阅读