Google Cloud Runarrow-up-right is a fully managed compute platform for deploying and scaling containerized applications.
Database (MongoDB Atlas hosted at GCP or Google SQL are preferable)
Google Cloud Run requires the application to use docker, so add those two files to the backend folder.
backend/Dockerfile
Copy FROM node:14
WORKDIR /app
COPY . /app
RUN npm install
RUN npm run build
CMD [ " node " , " ./dist/server.js " ] backend/.dockerignore
Google Cloud Project
Go to https://cloud.google.com/arrow-up-right and create an account.
Create a new project for the production environment. To create a staging environment the steps are the same.
The project needs the billing enabled: https://cloud.google.com/billing/docs/how-to/modify-projectarrow-up-right .
Install the https://cloud.google.com/sdkarrow-up-right .
Sign in to your account calling gcloud auth login.
The first step is to build the image with Google Cloud Build.
Replace:
PROJECT-ID: By your Google Cloud project ID.
APPLICATION-ID: By the name of your application.
On your first try, you will receive an error warning you to enable the Google Build API, so please do it and try again .
After the image is built, you can deploy it to Google Cloud Run:
You'll be prompted to:
Enable the API: Select yes .
Region: Select the one you have the database hosted.
Allow unauthenticated invocations: Select yes .
Create deployment scripts
You may want to create a deployment script.
backend/package.json
Environment Variables
Go to the Google Cloud Run consolearrow-up-right .
Select the service you just deployed.
Click on Edit & Deploy a new Revision .
Go to variables and add the environment variables .
The environment variables are the same you set on the setup but related to production, with a few changes.
Backend chevron-right GOOGLE_CLOUD_PLATFORM_CREDENTIALS
You DON'T need to specify the GOOGLE_CLOUD_PLATFORM_CREDENTIALS because you are deploying inside the Google Cloud platform. But you have to make sure the Compute Engine default service account contains the following permissions:
Service Account Token Creator
FRONTEND_URL and FRONTEND_URL_WITH_SUBDOMAIN
Set this to your hosted frontend URL (if you have one already).
BACKEND_URL
Set this one with the URL you received after the first deployment. Don't forget to add the /api suffix. It will be something like https://APPLICATION-ID-RANDOM-CODE-uc.a.run.app/api .