【问题标题】:ShipIt node/express deployment with PM2 - cannot set the correct NODE_ENV使用 PM2 的 ShipIt 节点/快速部署 - 无法设置正确的 NODE_ENV
【发布时间】:2016-11-28 22:08:01
【问题描述】:

我正在尝试在使用 ShipIt(w ship-npm 插件)部署时在正确的环境中启动我的节点应用程序?我在登台环境中部署它,但应用程序以开发模式启动,如显示 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 │ 你好 │ 0 │ fork │ 7224 │ 在线 │ 2 │ 0s
└──────────┴────┴──────┴──────┴────────┴──────────┴ ────────┴──────┴────────── @myhost16.live 使用pm2 show <id|name> 获取有关应用程序的更多详细信息 9.01 秒后完成“start_server”

我认为在“暂存”模式下部署会将 NODE_ENV 设置为“暂存”...不确定

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",
...

生态系统.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"
      }
    }]
}    

我的生态系统.js 文件有什么问题?

感谢反馈

【问题讨论】:

    标签: node.js pm2 shipitjs


    【解决方案1】:

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

    Here你可以找到更多关于它的信息。

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

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

    【讨论】:

    • 谢谢!在这种情况下,我需要在我的 package.json 文件中设置 2 个脚本,start-staging 和 start-production...
    猜你喜欢
    • 1970-01-01
    • 2015-04-15
    • 2019-06-24
    • 2023-03-17
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2015-07-30
    • 2011-03-28
    相关资源
    最近更新 更多