如何使用React中在Google Cloud Run Dashboard上声明的ENV变量 [英] How to use ENV Variables Declared on Google Cloud Run Dashboard in React

查看:62
本文介绍了如何使用React中在Google Cloud Run Dashboard上声明的ENV变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Dockerfile在Google Cloud Run上部署一个create-react-app服务,但是我想摆脱在.env文件中声明env变量的方法,而是像这样在Google Cloud Run的仪表板上声明它们:

I am deploying a create-react-app Service onto Google Cloud Run using a Dockerfile, but I want to move away from declaring env variables in a .env file, and instead, declare them on Google Cloud Run's Dashboard like so:

但是,当我使用

console.log("REDIRECT", process.env.REACT_APP_REDIRECT_URI)

我尝试引用的任何env变量都返回

null.还有其他步骤可以访问我丢失的这些变量吗?

null is returned for any env variable I try to reference. Is there another step to access these variables that I am missing?

这是我的Dockerfile:

Here is my Dockerfile:

FROM node:10-alpine as react-build
WORKDIR /app
COPY . ./
RUN yarn
RUN yarn build

FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/configfile.template
ENV PORT 8080 
ENV HOST 0.0.0.0
RUN sh -c "envsubst '\$PORT'  < /etc/nginx/conf.d/configfile.template >      /etc/nginx/conf.d/default.conf"
COPY --from=react-build /app/build /usr/share/nginx/html
EXPOSE 8080
CMD ["nginx", "-g", "daemon off;"]

推荐答案

您的容器仅通过NGINX提供静态文件,并且在Cloud Run端不执行任何处理.

Your container serve only static files (through NGINX) and no processing is performed on Cloud Run side.

实际上,您将静态文件公开给用户.用户获取文件并将其加载到他们的浏览器中.用户的浏览器执行Javascript并在当前环境(用户的浏览器)上读取Env变量.

Actually, you expose your static file to your users. The users get the files and load them in their browser. The users' browser execute the Javascript and read the Env Variable on the current environment: the users' browser.

因此,在此用例中不使用Cloud Run env var.您必须在Cloud Run上执行处理才能使用Cloud Run env变量.

Therefore, the Cloud Run env var aren't use in this use case. You have to perform a processing on Cloud Run to use the Cloud Run env variables.

这篇关于如何使用React中在Google Cloud Run Dashboard上声明的ENV变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆