Google App Engine:修改云运行环境 [英] Google App Engine: Modify Cloud Run Environment
问题描述
我正在尝试部署一个使用自定义Node.js服务器的Next.js应用程序.
I'm trying to deploy a Next.js app which uses a custom Node.js server.
我想将自定义构建变量注入应用程序:
I want to inject custom build variables into the app:
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
).
The above file is run at build time (yarn build
).
问题在于Google App Engine在后台使用Cloud Build.在那里,NODE_ENV
始终设置为development
.我该如何覆盖NODE_ENV
?即如何定制用于Google App Engine gcloud app deploy
的Cloud Build?
The issue is that Google App Engine uses Cloud Build behind the scenes. There, the NODE_ENV
is always set to development
. How can I override the NODE_ENV
there; i.e. how can I customize the Cloud Build used for the Google App Engine gcloud app deploy
?
由于此问题,我不能只使用Docker .
{
"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
.
image
Below is the output of passing a DOGE_ENV
variable from app.yaml
. As you can see, it is undefined
. However, NODE_ENV
is development
.
也就是说,将以下内容添加到app.yaml
无效.
That is, adding the following to app.yaml
does not work.
env_variables:
DOGE_ENV: production
推荐答案
不要使用NODE_ENV,请创建您自己的环境变量并使用该变量:
Don't use NODE_ENV, create your own environment variable and use that:
App.yaml
env_variables:
ST_ENV: Production
next.config.js
const environment = process.env.ST_ENV;
const envType = environment === `production` ? `production` : `staging`;
const envPath = `./config/${envType}`;
const { env } = require(envPath);
module.exports = {
env: { ...env },
};
这篇关于Google App Engine:修改云运行环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!