Drife Code blogs and ai

Workflow file to deploy react app automatically on multiple domains from single runner

Cover Image for Workflow file to deploy react app automatically on multiple domains from single runner
senior developer gautam swami
Gautam swami

This blog post guides you through setting up automated deployment for your React application on different environments based on the branch you push to using GitHub Actions.

Benefits:

Saves time and effort by automating deployments.
Reduces human error by removing manual deployment steps.
Provides a clear separation between development, UAT, and production environments.
Prerequisites:

A React application
A GitHub repository
A self-hosted runner configured for your GitHub Actions workflow

Here is the yml file using which you can do deployment on testing and production domain at the same time

name: Deploy React App


on:

push:

branches:

- main

- production# Update with your production branch name


jobs:

deploy:

runs-on: self-hosted


steps:

- name: Checkout code

uses: actions/checkout@v2


- name: Create environment based on branch

run: |

if [[ ${{ github.ref_name }} == "main" ]]; then

echo "REACT_APP_BASE_URL=your api endpoint" >> .env.development

echo "UAT environment created"

else

echo "REACT_APP_BASE_URL=your api endpoint" >> .env.production

echo "Production environment created"

fi


- name: Install Node.js

uses: actions/setup-node@v2

with:

node-version: 16# Adjust as needed


- name: Install dependencies

run: npm i


- name: Build React app

run: |

if [[ ${{ github.ref_name }} == "main" ]]; then

echo "UAT build creating"

CI=false npm run build:development

else

echo "Production build creating"

CI=false npm run build:production

fi


- name: Deploy to EC2

run: |

if [[ ${{ github.ref_name }} == "main" ]]; then

echo "UAT build deploying"

scp -r ./build/* /home/builds/uatBuild

else

echo "Production build deploying"

scp -r ./build/* /home/builds/prodBuild

fi

Explanation:

Conditional Environment Creation:

The workflow checks the current branch name (${{ github.ref_name }}).
Based on the branch (main or production), appropriate environment variables are set in the .env.development file.
Deployment Steps:

The workflow fetches code from the repository.
Node.js is installed.
Dependencies are installed.
The React app is built using the build:development script (adjust if needed).
Conditional scp commands deploy the build to the designated directory on your EC2 instances based on the branch pushed.
More Secure Methods:

Environment Variables:

Consider using a secrets management solution in GitHub Actions to store sensitive information like base URLs. This prevents accidentally committing them to your public repository.
You can access secrets within your workflow using the ${{ secrets.<SECRET_NAME> }} syntax.
SSH Keys:

For scp deployments, avoid committing your SSH key to the repository. Instead, configure a deployer user on your EC2 instances and use a key pair specifically for this user.
You can configure a deployer user with limited privileges for security.
Conclusion:

This approach streamlines React app deployments based on branches, improving development efficiency and reducing manual configuration. By incorporating more secure practices for secrets and SSH access, you can further enhance the security of your deployment process.


More Stories

Cover Image for How I Created a Mobile App Portfolio with AI in Just One Hour

How I Created a Mobile App Portfolio with AI in Just One Hour

Discover how I created a professional mobile app portfolio for a client in just one hour using Vercel's v0 AI. From generating the base code with a simple prompt to customizing it and deploying it on Vercel, this blog details the step-by-step process—highlighting the power of AI in modern web development. And yes, even this blog was crafted with a little help from AI! 🚀

senior developer gautam swami
Gautam swami
Cover Image for Using wireless debugging for Device connection and how to connect android device over wifi for development

Using wireless debugging for Device connection and how to connect android device over wifi for development

Using wireless debugging for Device connection and how to connect android device over wifi for development

senior developer gautam swami
Gautam swami