Google App Engine:修改云运行环境

问题描述 投票:1回答:1

我正在尝试部署使用自定义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

图片

下面是从DOGE_ENV传递app.yaml变量的输出。如你所见,它是undefined。然而,NODE_ENVdevelopment

也就是说,将以下内容添加到app.yaml不起作用。

env_variables:
  DOGE_ENV: production

enter image description here

javascript node.js google-app-engine google-cloud-platform
1个回答
0
投票

不要使用NODE_ENV,创建自己的环境变量并使用它:

的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 },
};
© www.soinside.com 2019 - 2024. All rights reserved.