salaami / portfolio

my website in react.js.
0 stars 0 forks source link

CI/CD workflow implementation #244

Open salaami opened 2 months ago

salaami commented 2 months ago

Absolutely! Using your personal React website as the project for improving and displaying your CI/CD skills is a great idea. Here's a detailed plan on how you can set up CI/CD pipelines for your React app:

Step-by-Step Guide to Setting Up CI/CD for Your React App

1. Version Control:

Ensure your React app is hosted on a Git platform (e.g., GitHub or GitLab).

2. CI Pipeline Configuration:

GitHub Actions:

  1. Create a Workflow File:

    • Navigate to your repository on GitHub.
    • Go to the Actions tab and click on "New workflow".
    • Select "Set up a workflow yourself".
  2. Configure the Workflow: Create a file named ci.yml in the .github/workflows directory of your repository.

    name: CI for React App
    
    on: [push, pull_request]
    
    jobs:
     build:
       runs-on: ubuntu-latest
    
       steps:
       - name: Checkout code
         uses: actions/checkout@v2
    
       - name: Set up Node.js
         uses: actions/setup-node@v2
         with:
           node-version: '14'
    
       - name: Install dependencies
         run: npm install
    
       - name: Run tests
         run: npm test
    
       - name: Build the app
         run: npm run build

GitLab CI/CD:

  1. Create a CI Configuration File:

    • Create a file named .gitlab-ci.yml in the root of your repository.
    stages:
     - build
     - test
    
    build:
     stage: build
     script:
       - npm install
       - npm run build
    
    test:
     stage: test
     script:
       - npm install
       - npm test

3. CD Pipeline Configuration:

To deploy your React app, you can use a cloud service like AWS, Azure, Google Cloud, or Heroku. Here's how you can configure it:

Example: Deploy to GitHub Pages

  1. GitHub Actions Configuration:

    Add a new job in your workflow file for deployment.

    name: CI/CD for React App
    
    on: [push, pull_request]
    
    jobs:
     build:
       runs-on: ubuntu-latest
    
       steps:
       - name: Checkout code
         uses: actions/checkout@v2
    
       - name: Set up Node.js
         uses: actions/setup-node@v2
         with:
           node-version: '14'
    
       - name: Install dependencies
         run: npm install
    
       - name: Run tests
         run: npm test
    
       - name: Build the app
         run: npm run build
    
     deploy:
       runs-on: ubuntu-latest
       needs: build
    
       steps:
       - name: Checkout code
         uses: actions/checkout@v2
    
       - name: Deploy to GitHub Pages
         uses: peaceiris/actions-gh-pages@v3
         with:
           github_token: ${{ secrets.GITHUB_TOKEN }}
           publish_dir: ./build
  2. GitLab CI/CD Configuration:

    If you want to deploy to a different platform like Heroku, you can modify the .gitlab-ci.yml file accordingly.

    stages:
     - build
     - test
     - deploy
    
    build:
     stage: build
     script:
       - npm install
       - npm run build
    
    test:
     stage: test
     script:
       - npm install
       - npm test
    
    deploy:
     stage: deploy
     script:
       - apt-get update -qy
       - apt-get install -y ruby-dev
       - gem install dpl
       - dpl --provider=heroku --app=your-heroku-app-name --api-key=$HEROKU_API_KEY

    Make sure to set the HEROKU_API_KEY in your repository's CI/CD settings under Variables.

4. Testing:

  1. Unit Tests:

    • Ensure you have unit tests for your React components.
    • Use a framework like Jest or React Testing Library.
  2. Integration Tests:

    • Write tests that cover the integration of multiple components.
  3. End-to-End Tests:

    • Use a tool like Cypress to simulate user interactions.

5. Deployment:

  1. Containerization:

    • Optional: Containerize your application using Docker for consistency across environments.
    # Dockerfile
    FROM node:14
    
    WORKDIR /app
    
    COPY package.json ./
    RUN npm install
    
    COPY . ./
    RUN npm run build
    
    CMD ["npm", "start"]
  2. Cloud Deployment:

    • Deploy to a cloud service like AWS, Azure, Google Cloud, or Heroku.
    • Use appropriate deployment steps in your CI/CD pipeline.

Summary

By configuring CI/CD pipelines for your personal React website, you'll gain hands-on experience with automated testing, building, and deployment processes. This will significantly enhance your skills and make your project a showcase of your CI/CD expertise.