This guide provides a comprehensive, step-by-step walkthrough for deploying the Vidora application from a local prototype to a scalable, secure, and production-ready state on Google Cloud Platform (GCP).
We will map the application's components to specific, recommended GCP services for a robust and manageable deployment:
- Frontend (React): Deployed on Firebase Hosting, which provides a global CDN, free SSL, and simple, fast deployments.
- Backend (Node.js/Express): Deployed as a container on Cloud Run, a fully managed, serverless platform that automatically scales with traffic (even to zero).
- Database (MongoDB): We'll use MongoDB Atlas deployed on Google Cloud for a fully managed, scalable database experience, keeping it within the GCP ecosystem.
- File Storage: Videos, avatars, and other user-generated content will be stored in Google Cloud Storage, a highly durable and cost-effective object storage solution.
- Security: We'll manage all our secrets (API keys, database URLs) securely using Secret Manager.
First, let's set up your Google Cloud workspace.
- Create a GCP Project: Go to the Google Cloud Console and create a new project (e.g.,
vidora-production). Note your Project ID. - Enable Billing: Ensure billing is enabled for your project. Many services have a generous free tier, but billing is required to use them.
- Install the
gcloudCLI: Follow the official instructions to install the Google Cloud CLI on your local machine. This is essential for managing your resources from the command line. - Configure the CLI: Authenticate and set your newly created project as the default.
# Log in to your Google Account gcloud auth login # Set your project ID gcloud config set project [YOUR_PROJECT_ID]
A managed database service is strongly recommended for production to handle backups, scaling, and security automatically.
- Create an Atlas Cluster: Go to MongoDB Atlas and create a new cluster.
- Select Google Cloud: When prompted for a cloud provider, choose Google Cloud and select a region close to your primary user base (e.g.,
us-central1). - Configure Security (VPC Peering): For production, a secure, private connection between your backend and database is crucial.
- In the Atlas console, navigate to Network Access > VPC Peering.
- Follow the instructions to set up a peering connection with your GCP project's default VPC network. This ensures your database is not exposed to the public internet. For simpler setups or testing, you can temporarily allow access from anywhere (
0.0.0.0/0), but this is not recommended for production.
- Create a Database User: In Database Access, create a user with a strong, auto-generated password.
- Get Your Connection String: Go to your cluster's "Connect" tab, select "Drivers," and copy the connection string URI. Replace
<password>with the password you just created. Save this string for later.
- Enable the API: In the GCP Console, search for and enable the "Cloud Storage API".
- Create a Bucket:
- Navigate to Cloud Storage in the console.
- Click "Create Bucket". Give it a globally unique name (e.g.,
vidora-prod-media). - Choose a region and the "Standard" storage class.
- For Access control, choose "Fine-grained".
- Make Files Publicly Readable: To allow users to view uploaded videos, the objects in the bucket need to be public.
- After creating the bucket, go to the "Permissions" tab.
- Click "Grant Access". For "New principals", add
allUsers. For "Role", selectStorage Object Viewer.
We will containerize the backend and deploy it to Cloud Run for serverless scaling.
-
Create a
Dockerfile: In yourbackend/directory, create a file namedDockerfile. This tells Google Cloud how to build your application.# Use an official Node.js runtime as a parent image FROM node:18-slim # Set the working directory in the container WORKDIR /usr/src/app # Copy package.json, package-lock.json, and prisma schema COPY package*.json ./ COPY prisma ./prisma/ # Install app dependencies RUN npm install --production # Run Prisma generate to create the client RUN npx prisma generate # Copy the rest of the built application code # Assumes you have already run `npm run build` locally or in a CI/CD step COPY dist ./dist/ # Your app binds to a port, expose it EXPOSE 8080 # Define the command to run your app CMD [ "node", "dist/server.js" ]
-
Store Your Secrets: Never hard-code secrets.
- In the GCP Console, go to Secret Manager.
- Create secrets for each of your environment variables:
DATABASE_URL,JWT_SECRET, etc.
-
Build and Push the Container Image:
- Enable the Artifact Registry API in the GCP Console.
- Create a Docker repository to store your image:
gcloud artifacts repositories create vidora-backend --repository-format=docker --location=us-central1
- Build and push your image (replace
[PROJECT_ID]):# First, build your TypeScript code to Javascript npm run build --prefix backend # Build the container image docker build -t us-central1-docker.pkg.dev/[PROJECT_ID]/vidora-backend/api:latest ./backend # Configure Docker to authenticate with Artifact Registry gcloud auth configure-docker us-central1-docker.pkg.dev # Push the image to the registry docker push us-central1-docker.pkg.dev/[PROJECT_ID]/vidora-backend/api:latest
-
Deploy to Cloud Run:
- Deploy the service using the
gcloudcommand. This powerful command links the secrets and configures the service.gcloud run deploy vidora-api \ --image=us-central1-docker.pkg.dev/[PROJECT_ID]/vidora-backend/api:latest \ --platform=managed \ --region=us-central1 \ --allow-unauthenticated \ --set-secrets="DATABASE_URL=[SECRET_NAME]:latest,JWT_SECRET=[SECRET_NAME]:latest" # Add other secrets
- After deployment, Cloud Run will give you a public URL for your API. Copy this URL.
- Deploy the service using the
Firebase Hosting is the easiest and fastest way to deploy modern web apps.
- Set up Firebase:
- Go to the Firebase Console and click "Add project", selecting your existing GCP project.
- On your local machine, install the Firebase CLI:
npm install -g firebase-tools.
- Initialize Firebase in Your Project:
- Run
firebase login. - In the root directory of your project, run
firebase init hosting. - Follow the prompts:
Use an existing project-> Select yourvidora-productionproject.What do you want to use as your public directory?-> Enterdist.Configure as a single-page app (rewrite all urls to /index.html)?-> Yes.Set up automatic builds and deploys with GitHub?-> No (for now).
- Run
- Configure API URL: Create a file named
.env.productionin your project's root with your backend's URL:VITE_API_BASE_URL=https://your-cloud-run-api-url.a.run.app/api/v1 - Build and Deploy:
Firebase will give you a URL for your live frontend (e.g.,
# Build the production version of your React app npm run build # Deploy to Firebase Hosting firebase deploy --only hosting
vidora-production.web.app).
- Update CORS: Go back to your Cloud Run service in the GCP Console. In the "Variables & Secrets" tab, add an environment variable
CORS_ORIGINand set its value to your Firebase Hosting URL (https://vidora-production.web.app). This will redeploy your backend service with the correct cross-origin policy. - Custom Domain: In the Firebase Hosting console, you can easily add a custom domain. Firebase will guide you through the process of updating your DNS records.
You now have a production-ready, scalable version of Vidora running entirely on Google Cloud!