使用PM2的ShipIt节点/快速部署 - 无法设置正确的NODE_ENV

时间:2016-11-28 22:08:02

标签: node.js pm2 shipitjs

我正在尝试使用ShipIt(w ships-npm插件)在正确的环境中启动我的节点应用程序?我正在staging环境中部署它,但应用程序在开发模式下启动,如显示yj-he process.env.NODE_ENV

使用船舶部署

>$ shipit staging deploy
Starting deployment...
....
Running 'start_server' task...
Running "cd /opt/hello/releases/20161128182300 && npm start" on host "myhost.live".
@myhost.live 
@myhost.live > hello-world-express@0.0.1 start /opt/hello/releases/20161128182300
@myhost.live > pm2 startOrReload ecosystem.json
@myhost.live 
@myhost.live [PM2] Applying action reloadProcessId on app [hello](ids: 0)
@myhost.live [PM2] [hello](0) ✓
@myhost.live ┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬──────────
@myhost.live │ App name │ id │ mode │ pid  │ status │ restart │ uptime 

├──────────┼────┼──────┼──────┼────────┼─────── ──┼────────┼─────┼──────────     @myhost.live│hello│0│前叉│7224│在线│2│0s
└──────────┴────┴──────┴──────┴────────┴─────────┴ ────────┴─────┴──────────     @ myhost16.live使用pm2 show <id|name>获取有关应用的更多详细信息     已完成&#39; start_server&#39;在9.01秒之后

我认为部署在&#39; staging&#39;模式会将NODE_ENV设置为&#39; staging&#39; ...不确定

hello.js

var express = require('express');
var app = express();

app.get('/', function (req, res) {
    res.send('Hello World! Now you can call Express at 3637');
});

var port = process.env.PORT || 3637;

app.listen(port);
console.log('Now on ' + process.env.NODE_ENV + ' server');
console.log('Express app listening on localhost:'+ port);

控制台日志声明:

0|hello    | Now on development server
0|hello    | Express app listening on localhost:3637

shipitfile.js

...
// this task starts the application with PM2
shipit.blTask('start_server', function () {
    var cwd = shipit.releasePath;
    return shipit.remote( "cd " + cwd + " && npm start");
});

shipit.on('deployed', function () {
    console.log("Deployed !");
    shipit.start('start_server');
});
...

的package.json

...
"main": "hello.js",
"scripts": {
  "start": "pm2 startOrReload ecosystem.json",
...

ecosystem.json

{
  "apps" : [
    {
      "name": "hello",
      "cwd": "/opt/hello/current",
      "script": "hello.js",
      "args": "",
      "watch": true,
      "node_args": "",
      "merge_logs": true,
      "env": {
        "NODE_ENV": "development"
      },
      "env_production": {
        "NODE_ENV": "production"
      },
      "env_staging": {
        "NODE_ENV": "staging"
      }
    }]
}    

我的ecosystem.js文件出了什么问题?

感谢您的反馈

1 个答案:

答案 0 :(得分:1)

使用PM2,要使用生产环境变量(在env_production中设置),您需要指定--env选项。

Here您可以找到有关它的更多信息。

要解决您的问题,只需将--env生产添加到package.json中的start属性:

"start": "pm2 startOrReload ecosystem.json --env production",