Deploy env file in test and qa - reactjs

For setting the API URL and key value , i have created .env.development and .env.qa for test and QA environment. But how to deploy this file in azure devops,like how to setup the environemtn variable in pipeline.
can someone guide me through steps.

We have a similar setup, multiple dotenv files for each environment. In the package.json, we've defined multiple build scripts:
"scripts": {
"build": "react-scripts build",
"build:test": "env-cmd --no-override .env.tst react-scripts build",
"build:qa": "env-cmd --no-override .env.qa react-scripts build",
"build:prod": "env-cmd --no-override .env.prod react-scripts build"
},
Don't use .env.production or .env.test however, otherwise, these will be used automatically by react-scripts in every build or test command.

I have successfully implemented the feature in Azure Devops.
I have created seperate zip file by creating new task for test and qa,buildid with the suffix mentioning the environment in the Build Pipeline. Similarly we need to mention the given zip folder name in Test and QA task in release pipeline.

Related

Create-React-App .env file directory location (Non-root)

After creating a new web app with create-react-app (CRA), I need to include some environment files for configuring various endpoints. Noticed that CRA comes with the cool dotenv package all ready to go. There's only one problem with that - I would like to have dotenv read these files from within my ./environments directory and not the root directory. Is there any way to load the .env, .env.local, .env.test, etc... files in a directory separate from the root directory?
Noticing I can achieve this in my express backend server.js by simply importing like so:
require('dotenv').config({ path: `./environments/` })
Can I do the same with my client-side code in React? If so, where should I put this import? Doesn't seem to work for me.
If you created your project using CRA and want to configure dotenv to change the path from where it loads the env files, you will have to do npm eject.
Alternatively, you can use env-cmd to achieve the same:
Installation:
npm i env-cmd
Add scripts in package.json, for example:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"dev": "env-cmd -f envs/.env.dev react-scripts build",
"qa": "env-cmd -f envs/.env.qa react-scripts build",
"demo": "env-cmd -f envs/.env.demo react-scripts build"
},
After creating .env.dev, .env.qa, .env.demo in envs/ directory.
Now, you can run:
$ npm run dev // it will use envs/.env.dev file
$ npm run qa // it will use envs/.env.qa file
$ npm run demo // it will use envs/.env.demo file

Build react project based on branch name environment variables

In my project I have many git branch names (dev, test, stage ....) and I am building my React app using Docker, and using Jenkins to deploy it.
What I want is to specify my REACT_APP_BASE_URL based on my current branch name because I have several base URLs, for example (https://abc.dev.example, https://abc.test.example ..), and I want to run the matched name.
What I did was:
create .env.dev :
REACT_APP_BASE_URL= 'https://abc.dev.example'
create .env.production :
REACT_APP_BASE_URL= 'https://abc.production.example'
create .env.test :
REACT_APP_BASE_URL= 'https://abc.test.example'
npm install env-cmd --save
My script:
"start": "env-cmd -f .env.dev react-app-rewired start",
"build": "react-app-rewired build",
"build:develop": "env-cmd -f .env.dev react-app-rewired build",
"build:test": "env-cmd -f .env.test react-app-rewired build",
"build:demo": "env-cmd -f .env.demo react-app-rewired build",
It's always looking for the url on .env.production file for all branches because it considers the production during the build process, so how can I solve that?
Any ideas?

How to create and run a development build of an application using create-react-app configuration

npm run build creates production build of the project. How do I create development build? I'm using gradle-node-plugin to build the artifact. The project has standard create-react-app configuration.
This is not really doable with just create-react-app, there is an open issue Here and it doesn't look like it will be added anytime soon.
However, you can use a package called dotenv for that, following are the steps you should take:
Install dotenv (make sure to add save dev) npm install dotenv-cli --save-dev
In package.json scripts section add new script: "build:dev": "dotenv -e .env.development react-scripts build",
And you can build for development with npm run build:dev
PS: if you want to avoid mistakenly deploying dev builds to production (as mentioned here) you can add build:prod to package.json and disable the regular build command, see code:
"build:dev": "dotenv -e .env.development react-scripts build",
"build:prod": "dotenv -e .env.production react-scripts build",
"build": "echo \"Please use build:dev or build:prod \" && exit 1",
Also note that process.env.NODE_ENV will still be production but it'll load your .env.development file
Thanks, #Moses. This is an extension to the answer posted by Moses Schwartz. You can also make the build pick the environment files dynamically by exporting the value of the environment(development, test, production) in the bash shell. And then you don't have to have different build commands for different environments.
You can use this in your package.json
"start": "dotenv -e .env react-scripts start",
"build": "dotenv -e .env.${REACT_APP_ENVIRONMENT} react-scripts build",
So when your run npm start, it will pick the environment values from .env
and when you run npm run build, it will pick the environment values from .env.{REACT_APP_ENVIRONMENT}
To define the REACT_APP_ENVIRONMENT, you can do:
export REACT_APP_ENVIRONMENT="development" or
export REACT_APP_ENVIRONMENT="test" or
export REACT_APP_ENVIRONMENT="production"
Hope this helps. This will help in staging the react application for multiple environments.
Thanks to #Tx_monster comment
github.com/Nargonath/cra-build-watch
A script for create-react-app that writes development builds to the disk
npm install -D cra-build-watch
package.json:
{
"scripts": {
"watch": "cra-build-watch"
}
}
npm run watch

Run npm build with specific .env file

I have a react application that connect to micro services. I have different micro services urls per environment. I therefore have multiple env files: .env, .env-develoment-local, env-development,...
When I start the app locally, the app pick up the setting in the .env.development-local which is expecetd.
When I do npm run build I noticed that since it creating a production build, it picks up the .env file.
My question is how can configure the build such a way that it picks other .env files like .env.development or .env.qa, etc... ?
I was able to get this working using https://github.com/toddbluhm/env-cmd
I did the following:
npm install --save-dev env-cmd
Then updated package.json accordingly:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"build:stage": "env-cmd -f ./.env.stage npm run-script build"
}
Then run the following command:
npm run build:stage

How to use .env in react js

How to use .env in react . I can get access to .env.development and .env.production with
"start":"react-scripts start",
"build": "react-scripts build",
How to get access to another like .env.staging ?
I gave like this
"build_staging": "set REACT_APP_ENV=staging & react-scripts build",
but not working.
Any suggestions please.
To keep things consistent across linux(my production server) and windows(my development server) I use cross-env
npm install --save cross-env
and my scripts look like this
"scripts": {
"dev": "cross-env NODE_ENV=development node server",
"build": "cross-env NODE_ENV=production next build ",
"start": "cross-env NODE_ENV=production node server"
},
so to set a custom env like REACT_APP_ENV you'll need to
"build_staging": "cross-env REACT_APP_ENV=staging react-scripts build",
and you can access it in your javascript code using
process.env.REACT_APP_ENV
also to start a staging server you might want to add
"start_staging": "cross-env REACT_APP_ENV=staging react-scripts start"
more about this here
[CONTEXT]
- I've created React project with Create React.
- Running on Windows 10
- Using VSCode IDE
- I have the file .env.development on above /src folder.
- My code has console.log(process.NODE_ENV) and console.log(process.REACT_APP_YOUR_KEY)
[PROBLEM]
When I'm running the program with npm start, the browser prints 'development' for NODE_ENV, but for my React .env variable, it prints undefined.
I try to run npm start with changing the default script of package.json to this start script: set REACT_APP_YOUR_KEY && react-scripts start. Then there isn't any undefined, all works well. 🤨
[SOLUTION]
The cause was that the file .env.development is not detected correctly. My hypothesis is the environment development file, Windows or VSCode don't detect well.
Windows File Explorer files
VS Code explorer. Look up the icon of the files 🤔
You have to change the file name from .env.development to .env.
[SOLUTION]: I've created env.js as shown below.
env.js placement
after that i have added in index.html the script below
my env.js content
NB: i can acces my env.js var without import them (it seems the best way for me)

Resources