React JS Tutorial: CI/CD Basics 🎯

beginner
22 min

React JS Tutorial: CI/CD Basics 🎯

Welcome to our comprehensive guide on CI/CD (Continuous Integration and Continuous Deployment) basics for React JS! This tutorial is designed to help both beginners and intermediates understand and implement CI/CD pipelines in their React projects. Let's dive in!

What is CI/CD? 📝

CI/CD is a practice in software development that automates the process of integrating code changes and deploying the application. It helps in maintaining a stable and reliable software delivery process by encouraging small, frequent code commits and automated testing.

Why is CI/CD important for React projects?

  • Faster feedback: CI/CD allows developers to catch errors early, reducing the time spent on debugging.
  • Improved collaboration: It enables multiple developers to work on a project simultaneously without introducing conflicts.
  • Reduced deployment time: CI/CD reduces the time and effort required to deploy updates to production.

Setting Up CI/CD for React Projects 💡

Prerequisites

  • Node.js (latest LTS version)
  • npm (Node Package Manager)

Steps

  1. Initializing the project:

Initialize your React project with create-react-app:

bash
npx create-react-app my-app
  1. Setting up Git:

Initialize your project with Git and create a remote repository on GitHub:

bash
cd my-app git init git remote add origin <your-git-repository-url> git add . git commit -m "Initial commit"
  1. Setting up Continuous Integration (CI):

For this tutorial, we will use GitHub Actions as our CI provider. Create a .github/workflows directory in your project root and create a ci.yml file:

yaml
name: CI on: push: branches: - master jobs: build: runs-on: ubuntu-latest steps: - 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: Build the project run: npm run build
  1. Setting up Continuous Deployment (CD):

Now, let's set up CD using GitHub Pages. In your package.json, add a script to build the project:

json
"scripts": { ... "build": "react-scripts build", "deploy": "git push --force --quiet gh-pages HEAD:gh-pages" }

Create a new branch for your production code:

bash
git checkout --orphan gh-pages git add . git commit -m "Initial commit"

Now, modify the ci.yml file to deploy the project on push to the gh-pages branch:

yaml
... deploy: runs-on: ubuntu-latest needs: build steps: - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 with: branch: gh-pages publish-dir: ./build

That's it! Now, whenever you push to the master branch, GitHub Actions will automatically build, test, and deploy your React app to GitHub Pages.

Quiz 💡

Question: What does CI/CD stand for, and why is it important for React projects?

A:

  1. Continuous Integration and Deployment
  2. Code Integration and Debugging
  3. Continuous Integration and Maintenance

Correct: A

Explanation: CI/CD stands for Continuous Integration and Continuous Deployment. It is important for React projects because it provides faster feedback, improves collaboration, and reduces deployment time.

Quick Quiz
Question 1 of 1

What does GitHub Actions do in this CI/CD setup?