Google Cloud - Run
Google Cloud Run is a fully managed compute platform for deploying and scaling containerized applications.
Requirements
Payments with Stripe (if payments are enabled)
File Storage (Google Cloud Storage is preferable)
Database (MongoDB Atlas hosted at GCP or Google SQL are preferable)
Docker Files
Google Cloud Run requires the application to use docker, so add those two files to the backend folder.
backend/Dockerfile
backend/.dockerignore
Google Cloud Project
Go to https://cloud.google.com/ and create an account.
Create a new project for the production environment. To create a staging environment the steps are the same.
Enable billing
The project needs the billing enabled: https://cloud.google.com/billing/docs/how-to/modify-project.
Google SDK
Install the https://cloud.google.com/sdk.
Sign in to your account calling gcloud auth login
.
Deploy
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 console.
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.
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
Storage Admin
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.
Last updated