React js with kubernetes deployment - node.js

mine is a react app (not build from creat react app) we use env file for different environments variables(use Dotenv web pack ) from npm . We are deploying this app to docker container in kubernetes. Looking for a way that env vars can be managed from kubernetes side than from .env file . While running In localhost (not in docker ) it should work also when deploying should take from kubernetes.any suggestion

You can use configmap and secrets in kubernetes to manage the environment variables. while both are default in kubernetes so no need of extra installation.
there are many other option you can also use the hashicorp vault for more secure varible store.
If environment variables will used inside kubernetes you can use configmap & secrets
If you want to transfer variable outside kubernetes better setup vault for more security purpose.
if you want to set the environment variables in docker file include you can do it like
...
RUN npm run build
ENV File_location=/app/.env
ENV DB_PORT=9090
WORKDIR /
RUN npm install express
...
This environment variable you can use and import to code.

Related

Using dotenv in cloud run

I have created a .env file in my local system while developing a project. If I upload my project along with .env file, will it work fine or do i have to assign env variables separately?
According to the documentation it won't work like that.
You can set them in Console or provide with --set-env-vars flags during deployment from command line or set id Dockerfile with ENV parameter.

Build variables not accessible in my react native app

I need to inject env variables into my code.
I'm using azure pipelines to build my android app in react native.
I have set env variables in the build configuration and I have created a file called appcenter-post-clone.sh. The contents of this file are as follows:
ENV ADMIN_HOST= $ADMIN_HOST
And in my build configuration I have defined
ADMIN_HOST = https://example.com.
But I'm getting this error, [command]/bin/bash /Users/runner/runners/2.160.1/work/1/s/appcenter-post-clone.sh
ENV: https://example.com: No such file or directory. What I fail to understand here is, why is azure treating the value of my env variables as a file? How do I make this work?
The blunder I made here is, I should have used
ENV ADMIN_HOST=$ADMIN_HOST
Without the space. That solved it for me.

Not able to access Gitlab env variables either in node.js or react

I have two different projects checked in Gitlab, frontend and backend.
For both the projects i have a Dockerfile each.
I have set the env variables in gitlab ci/cd .
I am running docker container in kubernetes,
but i am not able to access the gitlab env variables either in my react or node.js (express) application.
I was thinking that those env variable would be available to me when i do process.env.variable_name, but i am not able to access them.
What’s the best way to access Gitlab env variables in kubernetes (deployment.yaml) env variables ?
UPDATE
I have found that we can specify env variables in kubernetes, deployment.yaml file (under env section). How can i pass gitlab env variables to deployment.yaml?
Docker containers require that you set environment variables when you run them:
https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file
--env , -e Set environment variables
I was able to access environment variable in Node js application using Gitlab K8S_SECRET_ variable naming convention.
For example define a variable like K8S_SECRET_MY_TEST_API in gitlab.
In Node js, you can access this variable using process.env.MY_TEST_API.
Only issue I am facing is that those variables are not available in React app. Still trying to figure that out. I will update here once I resolve that issue.
To inject environment variables to a react app created with create-react-app you should add the prefix REACT_APP_ to every env var.
During the build, webpack will pick all the environment variables with that prefix and will add them to environment.

Is posible do after build or have multiple build steps with webpack?

I have webpack frontend application that have config file (config/default.json). I read this with config-webpack. My config file should change between environments (staging/production/etc)
I have CI pipeline that build my js code, minimize and package in docker image of nginx to be published in docker registry (inmutable|agnostic of my config file).
My docker container must be initialize env key with execution value
ENVIRONMENT=staging|production|stress
In docker container i have connection to secrets store and retrieve config data for my execution ENVIRONMENT.
Doing all the build on container start is expensive.
I need post process my build to attach config/default.json to webpack bundle. No build all my project.

Run JHipster in Docker with JHipster Registry-pass the variable ${jhipster.registry.password}

I am running JHipster application in Docker with JHipster Registry
how to pass the variable ${jhipster.registry.password} to app.yml
May I ask how to pass the variable ${jhipster.registry.password} to app.yml?
-
EUREKA_CLIENT_SERVICE_URL_DEFAULTZONE=http://admin:$${jhipster.registry.password}#jhipster-registry:8761/eureka
- SPRING_CLOUD_CONFIG_URI=http://admin:$${jhipster.registry.password}#jhipster-registry:8761/config
That value is parsed from your application's bootstrap.yml (or bootstrap-prod.yml in prod) found in src/resources/config.
Another way to set this variable is to set the JHIPSTER_REGISTRY_PASSWORD environment variable in your docker-compose app.yml file

Resources