Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REACT APP not access the google cloud run environment variables

I am using GitLab ci as my CI/CD tool. I am deploying the dockerized react app to cloud run but I am not able to access the environment variables declared on cloud run. Thank you!

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

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
like image 544
Usama Khan Avatar asked Dec 16 '20 22:12

Usama Khan


People also ask

Can react use environment variables?

When working with React, environment variables are variables that are available through a global process. env Object. That global Object is provided by your environment through NodeJs. And because we don't have NodeJs in the browser, we're going to need webpack.

Is there any official Google Cloud documentation for create react app?

Not official Google documentation. In this tutorial, you learn how to run a Create React App (CRA) with Nginx and deploy it to Cloud Run. Although other services in Google Cloud can serve similar web applications, Cloud Run is a good option in cases where some customization is needed to the underlying runtime.

What are environment variables in react app?

Environment variables are values that impact the behavior of running computer systems, OS environments, and applications. With respect to an application, it may access environment variable values for configuration purposes. By default, we will have NODE_ENV and any other env variables within React App will start with the prefix REACT_APP_

How do I set environment variables in GCloud?

You can set environment variables using the Cloud Console, the gcloud command line, or a YAML file when you create a new service or deploy a new revision: Console Command line YAML Go to Cloud Run Click Create Service if you are configuring a new service you are deploying to.

What is react_app_ dotenv?

By default, we will have NODE_ENV and any other env variables within React App will start with the prefix REACT_APP_ Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env. Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology. [1]


1 Answers

Credits to Guillaume Blaquiere because this answer is based on his post from this thread.

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. To read them at runtime, you would need to load HTML into memory on the server and replace placeholders in runtime.

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:

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. You should have an execution that run on Cloud Run to use the env vars. If the code is ran on user side (in their browser), the env vars will not appear.

like image 168
Donnald Cucharo Avatar answered Sep 19 '22 04:09

Donnald Cucharo