我们如何在服务器环境中指定运行哪个node.js启动脚本



这可能是个愚蠢的问题,但我被难住了。

我有3个环境:开发(本地(、暂存(远程(和生产(远程(。

我正在写一个相对简单的Express应用程序。

在我的package.json文件中,我为这3个环境中的每一个指定了启动脚本。这是完整的文件:

{
"name": "test-app",
"version": "0.0.1",
"description": "test utility app",
"private": true,
"scripts": {
"start": "cross-env NODE_ENV=development nodemon ./bin/www",
"start:staging": "cross-env NODE_ENV=staging nodemon ./bin/www",
"start:production": "cross-env NODE_ENV=production && nodemon ./bin/www"
},
"devDependencies": {
"cross-env": "^7.0.3"
},
"dependencies": {
"axios": "^0.24.0",
"config": "^3.3.6",
"cookie-parser": "~1.4.4",
"cors": "^2.8.5",
"debug": "~2.6.9",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"http-errors": "~1.6.3",
"jade": "~1.11.0",
"morgan": "~1.9.1",
"nodemon": "^2.0.15"
}
}

现在,我的问题是,如何指定在每个远程服务器环境中应该使用哪个启动脚本

我了解如何启动开发环境,所以忽略这一点,专注于暂存和生产。

例如,我希望start:stating在staging中执行,start:production在production中执行。

我可能想错了,所以请告诉我是否应该以不同的方式处理暂存和生产环境。

在我看来,如果没有我以某种方式将它们分配为一个或另一个,暂存和生产环境将不知道它们应该作为哪个环境运行。

也许这是工作流程的一部分?我正在使用github操作进行构建/部署。

更新

所以,我做了更多的挖掘。在工作流文件中,有一个构建步骤:

name: npm install, build, and test
run: |
npm install
npm run build --if-present
npm run test --if-present

我会简单地添加类似于:npm run start:stagingnpm run start:production的内容吗?

您基本上想运行脚本,但不知道要运行的命令,对吧?

所以

npm run start:staging

关于行动

- name: run app on staging
run: npm run start:staging

最新更新