【发布时间】:2019-09-17 10:54:22
【问题描述】:
我正在尝试部署一个使用自定义 Node.js 服务器的 Next.js 应用程序。
我想将自定义构建变量注入应用程序:
next.config.js
const NODE_ENV = process.env.NODE_ENV;
const envType = NODE_ENV === `production` ? `production` : `staging`;
const envPath = `./config/${envType}`;
const { env } = require(envPath);
module.exports = {
env: { ...env },
};
上述文件在构建时运行 (yarn build)。
问题在于 Google App Engine 在幕后使用 Cloud Build。在那里,NODE_ENV 始终设置为development。我怎样才能覆盖那里的NODE_ENV;即如何自定义用于 Google App Engine gcloud app deploy 的 Cloud Build?
因为this issue,我不能只使用 Docker。
package.json
{
"name": "blah",
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "NODE_ENV=staging node server.js",
"build": "rm -rf node_modules/ && yarn && rm -rf .next/ && next build",
"start": "node server.js",
"lint": "eslint . --ext .js",
"gcp-build": "yarn build"
},
"dependencies": {
"body-parser": "^1.18.3",
"dotenv": "^7.0.0",
"dotenv-webpack": "^1.7.0",
"express": "^4.16.4",
"express-session": "^1.16.1",
"firebase": "^5.10.0",
"firebase-admin": "^7.3.0",
"isomorphic-unfetch": "^3.0.0",
"lodash": "^4.17.11",
"next": "^8.1.0",
"now": "^15.0.6",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"session-file-store": "^1.2.0",
"styled-components": "^4.2.0",
"yenv": "^2.1.0"
},
"devDependencies": {
"babel-eslint": "^10.0.1",
"eslint": "^5.16.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.17.2",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-react": "^7.12.4"
},
"engines": {
"node": "10.x.x"
}
}
app.yaml
runtime: nodejs10
图片
下面是从app.yaml 传递DOGE_ENV 变量的输出。如您所见,它是undefined。但是,NODE_ENV 是 development。
也就是说,将以下内容添加到app.yaml 不起作用。
env_variables:
DOGE_ENV: production
【问题讨论】:
-
在您的应用在 Cloud Run/AppEngine 中运行之前,您是否使用 Google Cloud Build 来编译您的纱线资源?或者这是在运行的 Cloud Run/AppEngine 实例中生成的运行进程?
-
云构建运行
yarn build,它构建由node server.js提供服务的应用程序。我实际上并没有直接使用 Cloud Build,但所有gcloud app deploys 都有一个关联的 Cloud Build,使用在app.yaml中声明的runtime(在本例中为runtime: nodejs10)。
标签: javascript node.js google-app-engine google-cloud-platform