SnehaParkar / react-gh-pages

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deploying a React App* to GitHub Pages

Introduction

In this tutorial, we will learn how to create a React app and deploy it to GitHub Pages. follow the below steps

To create the React app, I'll be using create-react-app, which is a tool people can use to create a React app from scratch. To deploy the React app, I'll be using gh-pages, which is an npm package people can use to deploy things to GitHub Pages, a free web hosting service provided by GitHub.

Tutorial

Prerequisites

  1. Node and NPM are installed
  2. Git installed
  3. github account

Steps

1. Create an empty repository on GitHub

  1. Sign into your GitHub account.
  2. Visit the Create a new repository form.
  3. Fill in the form as follows: I'll enter: react-gh-pages I'll choose: Public
  4. Submit the form.

At this point, your GitHub account contains an empty repository, having the name and privacy type that you specified.

2. Getting Started with Create React App

This project was bootstrapped with Create React App.

  1. Create a React app named my-app:

    In case you want to use a different name from my-app (e.g. web-ui), you can accomplish that by replacing all occurrences of my-app in this tutorial, with that other name (i.e. my-app --> web-ui).

    $ npx create-react-app my-app

    That command will create a React app written in JavaScript. To create one written in TypeScript, you can issue this command instead:

    $ npx create-react-app my-app --template typescript

    That command will create a new folder named my-app, which will contain the source code of a React app.

    In addition to containing the source code of the React app, that folder is also a Git repository. That characteristic of the folder will come into play in Step 6.

    1. Enter the newly-created folder:
    $ cd my-app

At this point, there is a React app on your computer and you are in the folder that contains its source code. All of the remaining commands shown in this tutorial can be run from that folder.

3. Install the gh-pages npm package

  1. Install the gh-pages npm package and designate it as a development dependency:

    $ npm install gh-pages --save-dev

At this point, the gh-pages npm package is installed on your computer and the React app's dependence upon it is documented in the React app's package.json file.

4. Add a homepage property to the package.json file

  1. Open the package.json file in a text editor.

    $ vi package.json
    1. Add a predeploy property and a deploy property to the scripts object:
    "scripts": {
    +   "predeploy": "npm run build",
    +   "deploy": "gh-pages -d build",
        "start": "react-scripts start",
        "build": "react-scripts build",

At this point, the React app's package.json file includes deployment scripts.

6. Add a "remote" that points to the GitHub repository

  1. Add a "remote" to the local Git repository.

    You can do that by issuing a command in this format:

    $ git remote add origin https://github.com/{username}/{repo-name}.git

    To customize that command for your situation, replace {username} with your GitHub username and replace {repo-name} with the name of the GitHub repository you created in Step 1.

    In my case, I'll run:

    $ git remote add origin https://github.com/snehaparkar/react-gh-pages.git

    That command tells Git where I want it to push things whenever I—or the gh-pages npm package acting on my behalf—issue the $ git push command from within this local Git repository.

At this point, the local repository has a "remote" whose URL points to the GitHub repository you created in Step 1.

7. Push the React app to the GitHub repository

  1. Push the React app to the GitHub repository

    $ npm run deploy

    That will cause the predeploy and deploy scripts defined in package.json to run.

    Under the hood, the predeploy script will build a distributable version of the React app and store it in a folder named build. Then, the deploy script will push the contents of that folder to a new commit on the gh-pages branch of the GitHub repository, creating that branch if it doesn't already exist.

    By default, the new commit on the gh-pages branch will have a commit message of "Updates". You can specify a custom commit message via the -m option, like this:

    $ npm run deploy -- -m "Deploy React app to GitHub Pages"

At this point, the GitHub repository contains a branch named gh-pages, which contains the files that make up the distributable version of the React app. However, we haven't configured GitHub Pages to serve those files yet.

8. Configure GitHub Pages

  1. Navigate to the GitHub Pages settings page
    1. In your web browser, navigate to the GitHub repository
    2. Above the code browser, click on the tab labeled "Settings"
    3. In the sidebar, in the "Code and automation" section, click on "Pages"
  2. Configure the "Build and deployment" settings like this:
    1. Source: Deploy from a branch
    2. Branch:
      • Branch: gh-pages
      • Folder: / (root)
  3. Click on the "Save" button

That's it! The React app has been deployed to GitHub Pages! 🚀

At this point, the React app is accessible to anyone who visits the homepage URL you specified in Step 4. For example, the React app I deployed is accessible at https://gitname.github.io/react-gh-pages.

9. (Optional) Store the React app's source code on GitHub

In a previous step, the gh-pages npm package pushed the distributable version of the React app to a branch named gh-pages in the GitHub repository. However, the source code of the React app is not yet stored on GitHub.

In this step, I'll show you how you can store the source code of the React app on GitHub.

  1. Commit the changes you made while you were following this tutorial, to the master branch of the local Git repository; then, push that branch up to the master branch of the GitHub repository.

    $ git add .
    $ git commit -m "Configure React app for deployment to GitHub Pages"
    $ git push origin master

    I recommend exploring the GitHub repository at this point. It will have two branches: master and gh-pages. The master branch will contain the React app's source code, while the gh-pages branch will contain the distributable version of the React app.

About


Languages

Language:JavaScript 41.9%Language:HTML 37.7%Language:CSS 20.4%