我正在尝试部署一个使用自定义 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 的 Cloud Buildgcloud app deploy
?
我不能只使用 Docker 因为这个问题 https://stackoverflow.com/questions/55885235/next-js-infinite-reload-from-docker-container.
包.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
image
下面是传递一个的输出DOGE_ENV
变量来自app.yaml
。正如你所看到的,它是undefined
。然而,NODE_ENV
is development
.
也就是说,添加以下内容app.yaml
不起作用。
env_variables:
DOGE_ENV: production