[英]Google App Engine: Modify Cloud Run Environment
I'm trying to deploy a Next.js app which uses a custom Node.js server.我正在尝试部署一个使用自定义 Node.js 服务器的 Next.js 应用程序。
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 },
};
The above file is run at build time ( yarn build
).上面的文件在构建时运行( yarn build
)。
The issue is that Google App Engine uses Cloud Build behind the scenes.问题在于 Google App Engine 在幕后使用 Cloud Build。 There, the NODE_ENV
is always set to development
.在那里, NODE_ENV
始终设置为development
。 How can I override the NODE_ENV
there;我如何覆盖那里的NODE_ENV
; ie how can I customize the Cloud Build used for the Google App Engine gcloud app deploy
?即如何自定义用于 Google App Engine gcloud app deploy
的 Cloud Build?
I can't just use Docker because of this issue .由于这个问题,我不能只使用 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"
}
}
runtime: nodejs10
Below is the output of passing a DOGE_ENV
variable from app.yaml
.下面是从app.yaml
传递DOGE_ENV
变量的输出。 As you can see, it is undefined
.如您所见,它是undefined
。 However, NODE_ENV
is development
.但是, NODE_ENV
是development
。
That is, adding the following to app.yaml
does not work.也就是说,将以下内容添加到app.yaml
不起作用。
env_variables:
DOGE_ENV: production
Don't use NODE_ENV, create your own environment variable and use that:不要使用 NODE_ENV,创建自己的环境变量并使用它:
App.yaml应用程序.yaml
env_variables:
ST_ENV: Production
next.config.js下一个.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 },
};
I had the same issue, at the end I solved setting NODE_ENV=production
in the package.json script, so in your case just use:我遇到了同样的问题,最后我解决了在 package.json 脚本中设置NODE_ENV=production
问题,所以在你的情况下只需使用:
{
"gcp-build": "NODE_ENV=production yarn build"
}
otherwise you can create a Cloud Build configuration file, check the docs , it supports an env
section where you can define environmental variables否则,您可以创建一个 Cloud Build 配置文件,检查文档,它支持env
部分,您可以在其中定义环境变量
我遇到了同样的问题,最终使用了类似于Luca's answer 的方法,但有一种机制可以在部署之前替换package.json
的正确变量(在我的情况下使用microsoft/variable-substitution
GitHub 操作)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.