React Js Website Google Cloud Setup and Deployment Guide

Author

Reads 866

Confident young man in a striped shirt presenting a smartphone mockup with a blank screen.
Credit: pexels.com, Confident young man in a striped shirt presenting a smartphone mockup with a blank screen.

Setting up a React JS website on Google Cloud is a straightforward process. You can create a new project on the Google Cloud Console and enable the Google Cloud App Engine service.

To start, create a new directory for your project and initialize a new npm project using the command npm init. This will help you set up a package.json file that outlines the dependencies and scripts for your project.

Next, create a new App Engine standard environment application using the command gcloud app create. This will generate a new configuration file for your App Engine application.

You can then install the necessary dependencies, including the Google Cloud SDK and the App Engine SDK, using the command npm install.

On a similar theme: Google Analytics New Website

React Project Setup

To set up a React project, start by initializing and running a React app. Run the following command in your terminal to create a new React app: `npx create-react-app my-react-app`. This will start a development server, allowing you to view your React app in a browser at http://localhost:3000.

Credit: youtube.com, Run your React app on Google Cloud

To prepare a build of your React app, run the command `npm run build`. This creates a build folder at the root of your project, containing the compiled version of your React app. It's also a good idea to exclude the build folder from Git by adding `build/` to your `.gitignore` file.

You can also use Adapt's starter template to quickly set up a new project. The `hello-react-node-postgres` starter template is a good example, and you can set it up by running `npx adapt init hello-react-node-postgres myapp`. This will create a directory called `myapp` and set it up with all the files required to start a new React app.

React Project Setup

To set up a React project, you can use Adapt's starter template. This template is available as the hello-react-node-postgres starter, which you can use to quickly set up a new project.

You can also use other starters or write your own, as any git URL will work. To set up the starter, simply type `adapt init hello-react-node-postgres` in your terminal.

Credit: youtube.com, How to Set Up React in 2025 | Install React with Vite in 2 minutes

This will create a directory called myapp and set it up with all the files required to start a new React app, an Express back-end, and an Adapt project.

The React app is in the ./myapp/frontend directory, the back-end is in ./myapp/backend directory, and the Adapt project is in ./myapp/deploy.

If you already have your React application ready, you can follow along here and then copy your React application into the directory we create.

To initialize and run a React app, you need to create a new React app by running the command `npx create-react-app my-react-app` in your terminal.

This will start a development server, allowing you to view your React app in a browser at http://localhost:3000.

To prepare a build of your React app, run the command `npm run build` in your terminal. This will create a build folder at the root of your project, containing the compiled version of your React app.

You should exclude the build folder from Git by adding `build/` to your .gitignore file to prevent it from being tracked and pushed to your Git repository.

Here are the scripts you should include in your package.json:

  • start: npm start
  • build: npm run build
  • test: npm run test
  • eject: npm run eject

Make sure your scripts align with the provided commands for starting, building, testing, and ejecting your React app.

Setting Up

Credit: youtube.com, React Website Tutorial - Beginner React JS Project Fully Responsive

To set up a new React project, start by navigating to the root directory of your app in the terminal and installing the Google Cloud SDK following the instructions at Google Cloud SDK Installation Guide. This will allow you to authenticate the Google Cloud SDK and set up your project.

First, install the Google Cloud SDK in your terminal by running the command `gcloud auth login` and following the on-screen instructions. This will authenticate your Google Cloud SDK.

Next, set up your project by running the command `gcloud config set project PROJECT_ID` and replacing `PROJECT_ID` with your actual project ID. This will set your project ID in the Google Cloud SDK.

If you already have a React application ready, you can use the example provided as a guide for setting up your existing repository or using multiple repositories. You can also use the Adapt starter template to quickly set up a new project.

Credit: youtube.com, React JS Tutorial - 1 - How to Install React on Windows 10

To set up your project using the Adapt starter template, simply type `adapt init hello-react-node-postgres` and it will create a directory called `myapp` and set it up with all the files required to start a new React app, an Express back-end, and an Adapt project.

Here are the steps to initialize and run a React app:

  • Open your terminal and run the command `npx create-react-app my-react-app` to create a new React app.
  • Prepare a build of your React app by running the command `npm run build`.
  • Exclude the build folder from Git by adding `build/` to your `.gitignore` file.
  • Ensure your `package.json` includes the following scripts:
  • `start`: `react-scripts start`
  • `build`: `react-scripts build`
  • `test`: `react-scripts test`
  • `eject`: `react-scripts eject`

GCP Account and Project Setup

To set up your GCP account and project, navigate to the root directory of your React app in the terminal. Install the Google Cloud SDK following the instructions at Google Cloud SDK Installation Guide.

You'll need to authenticate the Google Cloud SDK by running gcloud auth login and following the on-screen instructions. This will ensure a smooth setup process.

To set your project ID, run gcloud config set project PROJECT_ID, replacing PROJECT_ID with your actual Project ID. This will help you keep track of your project's unique identifier.

Here's a quick summary of the steps:

GCP Account and Project Setup

Credit: youtube.com, How To Create A Project: Google Cloud Console / GCP

To set up your Google Cloud Platform (GCP) account and project, start by navigating to the root directory of your React app in the terminal. This is the first step in setting up your GCP account and project.

You'll then need to install the Google Cloud SDK, which you can do by following the instructions at the Google Cloud SDK Installation Guide. This guide will walk you through the process of installing the SDK on your machine.

Once the SDK is installed, authenticate it by running the command `gcloud auth login` and following the on-screen instructions. This will allow you to access the Google Cloud Console.

Next, set the project ID by running the command `gcloud config set project PROJECT_ID`, replacing `PROJECT_ID` with your actual Project ID. This will ensure that your project is properly configured in the GCP console.

Here's a step-by-step guide to setting up your GCP project:

  1. Navigate to the root directory of your React app in the terminal.
  2. Install the Google Cloud SDK following the instructions at Google Cloud SDK Installation Guide.
  3. Authenticate the Google Cloud SDK by running gcloud auth login and following the on-screen instructions.
  4. Set the project ID by running gcloud config set project PROJECT_ID, replacing PROJECT_ID with your actual Project ID.

Service Account Setup

To set up a Service Account for Google Cloud Run deployment, follow these steps:

Credit: youtube.com, Set Up Google Cloud Project & Service Account

First, go to the Google Cloud Console. Navigate to IAM & Admin → Service Accounts. Then, click on Create Service Account. Provide an appropriate name and description for the service account, such as github-ci-cd, as it will be used for Github CI/CD.

Next, assign the following roles:

After assigning roles, click the three dots and select Manage keys. Then, click on Add Key → Create New Key. Choose the JSON key type and securely download it, remembering that this key grants access to Google Cloud resources, so keep it safe.

Store your Service Account Key and Project ID as secrets in your GitHub repository to securely access them during the CI/CD process. You can create Github secrets by Github Repository → Settings → Secrets & Variables → Actions → Secrets tab → New Repository Secret.

If this caught your attention, see: Google Drive Api Key

Deploying to Google Cloud

You can deploy your React JS website to Google Cloud using two main methods: Google Cloud SDK and Google Cloud Shell.

Credit: youtube.com, Run your React app on Google Cloud

There are two ways to deploy to Google Cloud using the Google Cloud SDK: one using the Google Cloud SDK and another using Google Cloud Shell. You can deploy using Google Cloud Shell without installing the Google Cloud SDK locally.

First, you'll need to install the Google Cloud SDK on Windows by running a Powershell command. Once installed, you can deploy your build files to Google App Engine using the `gcloud app deploy` command.

Alternatively, you can deploy using Google Cloud Shell by activating it from your Google Console. Navigate to the cloned directory, install dependencies with `npm i`, and then build your project using a command. After the build is completed, run `gcloud app deploy` to deploy your application.

Once deployed, you can access your application by looking up the IP address of the exposed service via the GCP console. You can also update your deployment by editing your React app code and pushing the changes to GKE.

Building and Deploying

Credit: youtube.com, how to deploy React App on Google Cloud?

To build and deploy your React JS website on Google Cloud, you can use either Google Cloud SDK or Google Cloud Shell. You can install the Google Cloud SDK by running a Powershell command on Windows, or you can activate Cloud Shell by clicking on the Terminal icon in the header of your Google Console.

Once you have chosen your deployment method, you can deploy your application using the Google Cloud SDK command `gcloud app deploy`, or by running `npm i` and then `gcloud app deploy` in Cloud Shell. After deployment, you can access your application by looking up the IP address of the exposed service via the GCP console.

You can also create a deployment for your app by running the Adapt project, which builds your Hello World React app and deploys a test version of the app to the cluster.

Building a To-Do

We can now build a To-Do app by editing the code in the myapp/frontend/src/App.js file.

Credit: youtube.com, Software Build and Deployment Process: A Step-by-Step Guide

This is similar to how we changed the Hello World text earlier.

To implement the To-Do app, we can start by adding new features to the existing React app code.

This process is easy and straightforward, thanks to the flexibility of the GKE platform.

Once we've built the To-Do app, we can push the changes to GKE using the command we used earlier.

This command allows us to easily deploy our updates to the cloud.

Dockerize Your

Dockerizing your applications involves creating a containerized version of your code, which can be easily deployed to any environment.

This process begins by creating a Dockerfile, a text file that contains instructions for building your Docker image.

You can then use the Docker CLI to build and run your Docker image, making it easy to test and iterate on your code.

By using a Dockerfile, you can ensure consistency across different environments, reducing the risk of errors and inconsistencies.

Credit: youtube.com, Learn Docker in 7 Easy Steps - Full Beginner's Tutorial

A well-crafted Dockerfile can also improve the efficiency of your development process by allowing you to quickly spin up new containers for testing and development.

Docker containers provide a lightweight and portable way to deploy your applications, making it easy to move from development to production.

This portability is due in part to Docker's ability to package your application and its dependencies into a single container, eliminating the need for manual configuration.

By using Docker, you can also take advantage of Docker Compose, a tool that allows you to define and run multiple containers as a single unit.

With Docker Compose, you can easily manage complex applications with multiple services, making it easier to deploy and manage your applications.

Broaden your view: Google Cloud Platform Docker

Creating Engine

To create an App Engine, start by searching for 'App Engine' in the search bar and click on App Engine. This will take you to the application creation page.

Choose the region where most of your users are located to ensure faster loading times. Alternatively, leave it as it is if you don't have a specific preference.

Credit: youtube.com, How to Deploy an Application using Google App Engine | GCP Tutorial

In the Service account dropdown, select the default option to proceed with the next step. Click on Next to continue with the application creation process.

Wait for the application to be created, and a notification will arrive at the header once it's completed. This may take a few moments, so be patient.

Once the application is created, you can move on to the next step, which is building the project. Use the following command to build the project:

Configuration and Tools

To deploy your React JS website to Google Cloud, you'll need to configure the workflow to trigger on pushes or pull requests to the main branch. This involves using GitHub Actions to perform various steps such as building the React app, authenticating with Google Cloud, setting up the Cloud SDK, dockerizing your app, and deploying the app to Google Cloud Run.

You'll need to remove the service name from the gcloud run deploy command on line 36 during the first deployment to the bucket, as the first deployment must be of the default service.

Broaden your view: Google Drive Service Account

Credit: youtube.com, How To Deploy A Full Stack React App on Google Cloud Platform | Small to Medium Sized Application

To deploy to a Google Kubernetes Engine (GKE) cluster, you'll need to install a few tools, including the Google Cloud SDK, Docker, Node.js, and Yarn.

Here are the tools you'll need to install:

  • Google Cloud SDK (installation instructions)
  • Docker (installation instructions)
  • Node.js and Yarn (installation instructions)

If you encounter an EACCES error from npm install, retry the command as administrator, or use npx as explained at the bottom of the page.

Debugging and Deployment

Deploying your React JS website to Google Cloud is a straightforward process. You can use the Google Cloud SDK to deploy your build files to Google App Engine. First, install the Google Cloud SDK by running a Powershell command on Windows.

Once you've completed the installation, run the deploy command to deploy your build files. When asked for confirmation, type Y to proceed. After the deployment is done, it will print out the URL where it is deployed.

You can also deploy without installing the Google Cloud SDK locally by running the deployment from Google Cloud Shell. This method requires you to have pushed your project to a git repository. You can activate Cloud Shell by clicking on the Terminal icon in the header of your Google Console.

Credit: youtube.com, A better way to deploy your React and Vue apps on Google Cloud

To deploy using Cloud Shell, navigate to the cloned directory using cd react-gcp-app-engine/, install dependencies with npm i, and then build the project using the specified command. After the build has finished, run gcloud app deploy to deploy your application.

If you're using a Kubernetes cluster, you can deploy your app by running the Adapt project, which builds your Hello World React app and deploys a test version of the app to the cluster. You can access the application by looking up the IP address of the exposed service via the GCP console.

To update your deployment, you can edit the React app code and easily push the changes to GKE. Try changing the Hello World text in the myapp/frontend/src/App.js file, and then push your changes by typing the specified command.

Nancy Rath

Copy Editor

Nancy Rath is a meticulous and detail-oriented Copy Editor with a passion for refining written content. With a keen eye for grammar, syntax, and style, she has honed her skills in ensuring that articles are polished and engaging. Her expertise spans a range of categories, including digital presentation design, where she has a particular interest in the intersection of visual and written communication.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.