简体   繁体   English

Google App Engine:修改 Cloud Run 环境

[英]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:我想将自定义构建变量注入应用程序:

next.config.js下一个.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 },
};

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。

package.json包.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应用程序.yaml

runtime: nodejs10

image图片

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_ENVdevelopment

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.

相关问题 REACT APP 无法访问谷歌云运行环境变量 - REACT APP not access the google cloud run environment variables 通过Google App Engine(Java)将文件上传到Google云端存储 - Upload file to Google cloud storage from Google App Engine (Java) 如何从谷歌云应用引擎与外部 exe 程序通信? - How to communicate with an external exe program from an google cloud app engine? 如何将数据从应用程序引擎保存到数据存储谷歌云 javascript - How to save data from app engine to datastore google cloud javascript 在 Cloud Run 中为我的 React 应用正确定义环境变量 - Properly defining environment variables in Cloud Run for my React App 如何使用oAuth2在服务器之间进行通信(Google Cloud Engine-> Google App Engine) - How to communicate between servers (Google Cloud Engine -> Google App Engine) using oAuth2 Google Cloud Secrets Manager 拒绝访问 App Engine 应用程序,但可与 Google Cloud Function 一起使用 - Google Cloud Secrets Manager denies access to App Engine application but works with Google Cloud Function 如何在本地Google App Engine的非默认模块上访问Google Cloud Endpoints? - How to access Google Cloud Endpoints on non-default module of local Google App Engine? 使用Google Cloud End Point将文件上传到Google App Engine应用程序 - Upload file using google cloud end points to google app engine application 如何保留部署在谷歌云应用引擎上的 node.js 应用的统计数据并保持我的数据持久化 - How to keep stats for a node.js app deployed on google cloud app engine and keep my data persistent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM