Deploy React App to Google Cloud Run with Github Actions CI/CD - A Complete Guide
Rushi Patel
Posted on March 10, 2024
This guide provides you a step-by-step process to deploy your React app efficiently to Google Cloud Run. Each section offers actionable steps, ensuring a smooth deployment journey.
In this guide, we'll cover the following sections:
- Pre-requisite
- React Project Config
- GCP Account & GCP Project Setup
- Dockerize your App
- Github Actions CI/CD
- Testing
Whether you're a seasoned developer seeking a detailed deployment workflow or an enthusiast eager to take your React project live, this guide will equip you with the necessary knowledge and steps for a successful deployment journey. Let's start with pre-requisites.
Section 1: Pre-requisites
Before diving into deploying a React app to Google Cloud Run, it's essential to ensure that you have the necessary tools and knowledge in place. Mininum requirements are
- Node & npm Installed: Make sure Node.js and npm are installed on your machine. Node 18 is used throughout this guide.
- React JS Setup: Familiarize yourself with creating a React project and running it locally.
- Google Cloud Account: If you don't have one yet, don't worry; we'll create it in a later step.
Section 2: React Project Config
Initialize & Run a React App
If you haven't already set up a React project, follow these steps:
Open your terminal.
Run the following command to create a new React app:
npx create-react-app my-react-app
Replace my-react-app with your own project name.
- Run the React app:
npm start
This will start a development server, allowing you to view your React app in a browser at http://localhost:3000.
- Prepare a build of your React app:
npm run build
This command creates a build folder at the root of your project, containing the compiled version of your React app.
- Exclude build folder from Git:
Add
build/
to your.gitignore
file to prevent it from being tracked and pushed to your Git repository. - React Scripts Configuration:
Ensure your
package.json
includes the following scripts:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
Make sure your scripts align with the provided commands for starting, building, testing, and ejecting your React app.
Section 3: GCP Account & GCP Project Setup
Step 1: Create a New Project
- Go to the Google Cloud Console and sign in.
- Click on "Select a project" at the top and then click on "New Project."
- Choose a unique project name and select the desired organization if applicable.
- Once the project is created, note down the Project ID for future reference.
- Link this project to appropriate Billing Account.
Step 2: Enable Required APIs
- Navigate to the APIs & Services section in the Google Cloud Console.
- Click on Library in the left sidebar.
- Search for Cloud Storage and enable the Cloud Storage API.
- Search for Cloud Run API and enable the Cloud Run API.
Step 3: Set Up Cloud Storage
- Go to the Cloud Storage section in the Google Cloud Console.
- Click on Create Bucket to make a new bucket.
- Choose a unique name and select the
us-central1
region. - Leave other settings as default and create the bucket.
Step 4: Install GCP CLI
- 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.
- Authenticate the Google Cloud SDK by running
gcloud auth login
and following the on-screen instructions. - Set the project ID by running
gcloud config set project PROJECT_ID
, replacing PROJECT_ID with your actual Project ID.
Service Account Setup
Generate & Download a Service Account
To set up a Service Account for Google Cloud Run deployment, follow these steps:
- Go to the Google Cloud Console.
- Navigate to IAM & Admin → Service Accounts.
- Click on Create Service Account.
- Provide an appropriate name and description for the service account. For instance, use
github-ci-cd
as it will be utilized for Github CI/CD. -
Assign the following roles:
- Cloud Run Admin
- Cloud Build Service Account
- Cloud Run Service Agent
- Service Account User
- Storage Object Admin
Click the three dots and select Manage keys.
Click on Add Key → Create New Key.
Choose the JSON key type and securely download it. Remember, this key grants access to Google Cloud resources, so keep it safe.
Step 4: Dockerize your React App
What is Docker
Docker is a platform that enables developers to automate the deployment of applications inside lightweight, portable containers. Containers encapsulate the application and its dependencies, ensuring consistency and portability across various environments.
Placement:
The Dockerfile
, which contains instructions to build a Docker image for your React app, should be placed in the root directory of your project, alongside your source code.
Example Configuration:
# Use Node.js as the base image
FROM node:18.15.0-alpine
# Set the working directory in the container
WORKDIR /app
# Copy package.json and yarn.lock to the container
COPY package.json yarn.lock ./
# Install dependencies
RUN yarn install --frozen-lockfile
# Install dependencies
RUN yarn install
# Copy the app's source code to the container
COPY . .
# Build the React app
RUN yarn build
# Serve the build
CMD ["npx", "serve", "-s", "build"]
Explanation of Configuration:
Base Image: Uses Node.js version 18.15.0 Alpine as the lightweight base image.
Dependency Installation: Copies
package.json
andyarn.lock
, installs dependencies using Yarn, leveraging Docker layer caching for efficiency.Source Code and Build: Copies the app's source code, builds the React app, and sets up the working directory.
Serve the Build: Uses
npx serve
to serve the built React app from thebuild
directory when the container starts.
Step 5: CI/CD using GitHub Actions
CI/CD and GitHub Actions:
CI/CD: CI (Continuous Integration) and CD (Continuous Deployment) automate the software delivery process. CI involves merging code changes into a shared repository frequently, automatically running tests, and validating changes. CD automates the deployment of validated code changes to production or staging environments.
CI/CD in GitHub Actions: GitHub Actions provide workflows to automate tasks in your repository, including CI/CD processes. These workflows are defined in YAML files and can be triggered by various events like code pushes, pull requests, etc.
Storing Service Account Key and Project ID in GitHub Secrets:
- 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
- Secrets:
-
GCP_SA_KEY
: Your entire JSON of Service Account Key generated in previous step. -
GCP_PROJECT_ID
Your GCP Project ID. -
DOCKER_IMAGE_NAME
Your preferred name of Docker Image.
-
Workflow File Name & Placement:
- The workflow file can be named
cloud-run-deploy.yml
. - Place this file in the
.github/workflows
directory in your project. Refer the below given repository incase of any confusion. - Paste the below configuration in recently created yml file.
Configuration
name: Deploy to Cloud Run
env:
SERVICE_NAME: react-app
on:
push:
branches:
- main
jobs:
dockerize-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Google Cloud SDK
uses: google-github-actions/setup-gcloud@v0.2.0
with:
service_account_key: ${{ secrets.GCP_SA_KEY }}
project_id: ${{ secrets.GCP_PROJECT_ID }}
- name: Configure Docker
run: |
gcloud auth configure-docker
- name: Build and Push Docker Image
run: |
docker build -t gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest .
docker push gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest
- name: Deploy to Cloud Run
run: |
gcloud run deploy $SERVICE_NAME \
--image gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest \
--platform managed \
--region us-central1 \
--allow-unauthenticated
This workflow triggers on pushes or pull requests to the main
branch. It uses GitHub Actions to perform various steps such as building the React app, authenticating with Google Cloud, setting up the Cloud SDK, dockerize your app and deploying the app to Google Cloud Run.
Note: When you are deploying for first time to bucket, remove the service name from
gcloud run deploy
command on line 36. As first deployment must be of default service. From second deploy you provide preferred service name.
Step 6: Testing the CI/CD
Push Your Changes to GitHub (on Main Branch):
Ensure your latest changes are pushed to the main branch of your GitHub repository.
Navigate to the 'Actions' Tab in GitHub Repository:
Visit your GitHub repository and go to the 'Actions' tab.
Check Workflow Status:
Verify the status of your React app deployment workflow using GitHub Actions.
Open the Deployed App URL:
Access the provided URL after a successful deployment in your browser to confirm your React application runs smoothly.
Note: In GitHub Actions logs, the URL might contain masked text (**** text) representing your GCP project ID. Replace **** with your project ID and open the URL in your browser.
Access Google Cloud Engine Services:
Go to Google Cloud, locate and click on Cloud Run, then navigate to Services tab. Find your recent deployments and click on the service to open your React App in a new tab.
GitHub Repository Link
Access the complete code reference for this guide by visiting the GitHub Repository.
Feel free to clone the repository and experiment with it on your own!
Conclusion
🚀 Congratulations on mastering the deployment of your React app to Google Cloud Run and GitHub Actions! 🎉
Embrace this automated workflow to streamline your development journey and propel your projects to new heights. You can refer the above given repository link as and when required. If you have any queries or need guidance, feel free to chat or drop a comment.
Keep coding, exploring, and sharing the joy of efficient deployments! Don't forget to like and share this guide to inspire others on their deployment adventures! 👍
Happy coding! 💻✨
Posted on March 10, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
March 10, 2024
January 9, 2024