【问题标题】:Google App Engine: Modify Cloud Run EnvironmentGoogle App Engine:修改 Cloud Run 环境
【发布时间】: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_ENVdevelopment

也就是说,将以下内容添加到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


【解决方案1】:

不要使用 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 },
};

【讨论】:

  • 感谢您的回答,但这不起作用,因为Cloud Build环境是构建时环境,而app.yaml中指定的env_variables是运行时变量。所以,当next.config.js 实际运行时,process.env.ST_ENV 将是undefined
  • 我添加了一张 Cloud Build 输出图像来演示此问题。
【解决方案2】:

我遇到了同样的问题,最后我解决了在 package.json 脚本中设置 NODE_ENV=production 的问题,所以在你的情况下,只需使用:

{
  "gcp-build": "NODE_ENV=production yarn build"
}

否则你可以创建一个 Cloud Build 配置文件,检查the docs,它支持env 部分,您可以在其中定义环境变量

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,最终使用了类似于Luca's answer 的方法,但在部署之前使用了一种机制来替换package.json 中的正确变量(在我的例子中使用microsoft/variable-substitution GitHub 操作)。

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 2019-09-01
      • 2018-03-29
      • 2017-08-24
      • 2015-04-23
      • 2019-04-02
      相关资源
      最近更新 更多