AWS CodePipeline සහ CodeDeploy භාවිතයෙන් React Application එක EC2 එකට Deploy කිරීම

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated

React Application එකක් AWS EC2 එකට dist/ folder එක විතරක් deploy කරලා run කරන ආකාරය පහත පියවර අනුව විස්තර කරමි.

🔧 AWS Architecture Overview

  • GitHub Repository → React Source Code
  • AWS CodeBuildnpm install && npm run build (Creates dist/ folder)
  • AWS CodeDeploy → Deploys dist/ folder to EC2 → /var/www/html
  • EC2 Instance → Nginx/Apache to serve the React app

💡 Step 1: AWS CodePipeline එකක් සකසා ගනිමු

  1. AWS Console → CodePipeline → Create Pipeline
  2. Pipeline Namereact-app-pipeline
  3. RoleCreate new role
  4. S3 Bucket → Select default S3 bucket
  5. Click Next

💡 Step 2: GitHub Repository එක Source එකක් Connect කිරීම

  1. Source ProviderGitHub
  2. Connect GitHub → Authenticate (Allow AWS to access repo)
  3. Select RepositoryYour GitHub repo
  4. Branch Name → Example: main
  5. Output Artifact NameSourceArtifact
  6. Click Next

💡 Step 3: AWS CodeBuild සකසා ගැනීම

  1. Build ProviderAWS CodeBuild
  2. Click Create New Build Project
  3. Project Namereact-app-build
  4. Environment:
    • OSUbuntu
    • RuntimeStandard
    • Imageaws/codebuild/standard:latest
    • Service RoleCreate new role
  5. Buildspec file → Select Use a buildspec file
  6. Click Create Build Project
  7. Go back to CodePipeline and Click Next

💡 Step 4: AWS CodeDeploy සකසා ගැනීම

  1. Deploy ProviderAWS CodeDeploy
  2. Application Namereact-app-deploy
  3. Deployment Group Namereact-app-group
  4. Select EC2 InstanceEnsure EC2 instance has CodeDeploy agent installed
  5. Click Next → Create Pipeline

💡 Step 5: buildspec.yml එක GitHub Repo එකට එකතු කිරීම

GitHub repo එකේ buildspec.yml කියලා file එකක් හදලා පහත content එක එක් කරන්න:

version: 0.2

phases:
  install:
    runtime-versions:
      nodejs: latest
  pre_build:
    commands:
      - echo Installing dependencies...
      - npm install
  build:
    commands:
      - echo Building the React app...
      - npm run build
  post_build:
    commands:
      - echo Build complete!
artifacts:
  files:
    - '**/*'
  base-directory: build/

💡 Step 6: appspec.yml එක GitHub Repo එකට එකතු කිරීම

GitHub repo එකේ appspec.yml කියලා file එකක් හදලා පහත content එක එක් කරන්න:

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html
permissions:
  - object: /var/www/html
    owner: ec2-user
    group: ec2-user
    mode: 755
hooks:
  AfterInstall:
    - location: scripts/restart_server.sh
      timeout: 300
      runas: root

💡 Step 7: Restart Script එක එක් කිරීම (Nginx)

GitHub repo එකේ scripts/restart_server.sh කියලා folder එකක් හදලා පහත content එක එක් කරන්න:

#!/bin/bash
sudo systemctl restart nginx

Then give it execute permission:

chmod +x scripts/restart_server.sh

💡 Step 8: GitHub Repo එක Push කිරීම

git add .
git commit -m "Added buildspec and appspec files for React app"
git push origin main

💡 Step 9: EC2 Server එකේ Nginx Setup කිරීම

  1. EC2 Instance එකට Login වීම
ssh -i "your-key.pem" ec2-user@your-ec2-ip
  1. Nginx Install කිරීම
sudo yum install nginx -y
  1. Nginx Config File එක Edit කිරීම
sudo nano /etc/nginx/nginx.conf
  1. මේක add කරන්න (server block එක)
server {
    listen 80;
    server_name your-ec2-ip;

    location / {
        root /var/www/html;
        index index.html;
        try_files $uri /index.html;
    }
}
  1. Nginx Restart කිරීම
sudo systemctl restart nginx

🚀 Step 10: Pipeline එක Run කරලා Deploy කිරීම!

AWS Console → CodePipeline → Release Change

GitHub Repo → React Source CodeCodeBuild → npm run buildbuild/ folder create වෙයිCodeDeploy → build/ folder එක /var/www/html copy කරයිNginx restart කරලා React App එක serve කරයි

Browser එකේ http://your-ec2-ip එකට ගිහින් React App එක check කරන්න! 🎉


🌟 Done!

දැන් ඔයා GitHub Repo එකට push කලම, React App එක AWS EC2 එකට Auto Deploy වෙයි! 🚀

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.