简体   繁体   English

REACT APP 无法访问谷歌云运行环境变量

[英]REACT APP not access the google cloud run environment variables

I am using GitLab ci as my CI/CD tool.我正在使用 GitLab ci 作为我的 CI/CD 工具。 I am deploying the dockerized react app to cloud run but I am not able to access the environment variables declared on cloud run.我正在将 dockerized react 应用程序部署到云运行,但我无法访问在云运行上声明的环境变量。 Thank you!谢谢!

Dockerfile Dockerfile

# build environment
FROM node:8-alpine as react-build

WORKDIR /app
COPY . ./

RUN npm install
RUN npm run build

# server environment
FROM nginx: alpine
COPY nginx.conf /etc/nginx/conf.d/configfile.template

COPY --from=react-build /app/build /usr/share/nginx/html

ENV PORT 8080
ENV HOST 0.0.0.0
EXPOSE 8080
CMD sh -c "envsubst '\$PORT' < /etc/nginx/conf.d/configfile.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"

gitlab-ci.yml gitlab-ci.yml

default:
  image: google/cloud-sdk:alpine
  before_script:
    - gcloud config set project PROJECTID
    - gcloud auth activate-service-account --key-file $GCP_SERVICE_CREDS
stages:
  - staging
  - production

staging:
  stage: staging
  environment:
    name: staging
  only:
    variables:
      - $DEPLOY_ENV == "staging"
  script:
    - gcloud builds submit --tag gcr.io/PROJECTID/REPOSITORY
    - gcloud run deploy REPOSITORY --image gcr.io/PROJECTID/REPOSITORY --platform managed --region us-central1 --allow-unauthenticated 

production:
  stage: production
  environment:
    name: production
  only:
    variables:
      - $DEPLOY_ENV == "production"
  script:
    - gcloud builds submit --tag gcr.io/PROJECTID/REPOSITORY
    - gcloud run deploy REPOSITORY --image gcr.io/PROJECTID/REPOSITORY --platform managed --region us-central1 --allow-unauthenticated --set-env-vars NODE_ENV=production

As I understand it, you want to access environment variables declared on GCP such as $GCP_SERVICE_CREDS, $PROJECTID, $REPOSITORY and others on your Gitlab pipeline.据我了解,您希望访问在 GCP 上声明的环境变量,例如 $GCP_SERVICE_CREDS、$PROJECTID、$REPOSITORY 和 Gitlab 管道上的其他变量。

To do this, go to Gitlab settings then click on CI/CD.为此,go 到 Gitlab 设置,然后单击 CI/CD。 Once there, click on the Expand button in front of Variables and you add your various GCP variables with their Values.到达那里后,单击变量前面的展开按钮,然后添加各种 GCP 变量及其值。

Credits to Guillaume Blaquiere because this answer is based on his post from this thread .归功于 Guillaume Blaquiere,因为这个答案是基于他在这个线程中的帖子。

According to React documentation :根据反应文档

The environment variables are embedded during the build time .环境变量是在构建时嵌入的 Since Create React App produces a static HTML/CSS/JS bundle, it can't possibly read them at runtime.由于 Create React App 生成一个 static HTML/CSS/JS 包,它不可能在运行时读取它们。 To read them at runtime, you would need to load HTML into memory on the server and replace placeholders in runtime.要在运行时读取它们,您需要将 HTML 加载到服务器上的 memory 并在运行时替换占位符。

Most likely what happens is that you expose static files to your users and the users get the files and load them in their browser like this:最有可能发生的情况是您将 static 文件暴露给您的用户,而用户获取文件并将其加载到浏览器中,如下所示:

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

This returns null because the users' browser execute the Javascript and read the env variable on the current environment: the users' browser.这将返回null因为用户的浏览器执行 Javascript 并读取当前环境上的 env 变量:用户的浏览器。 You should have an execution that run on Cloud Run to use the env vars.您应该有一个在 Cloud Run 上运行的执行以使用环境变量。 If the code is ran on user side (in their browser), the env vars will not appear.如果代码在用户端(在他们的浏览器中)运行,则不会出现环境变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM