Google Cloud - Run
Google Cloud Run is a fully managed compute platform for deploying and scaling containerized applications.
Google Cloud Run requires the application to use docker, so add those two files to the backend folder.
backend/Dockerfile
FROM node:14
WORKDIR /app
COPY . /app
RUN npm install
RUN npm run build
CMD ["node", "./dist/server.js"]
backend/.dockerignore
node_modules
npm-debug.log
Dockerfile*
docker-compose*
.dockerignore
.git
.gitignore
.env
*/bin
*/obj
README.md
LICENSE
.vscode
Create a new project for the production environment. To create a staging environment the steps are the same.
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.
gcloud builds submit --tag gcr.io/PROJECT-ID/APPLICATION-ID
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:
gcloud run deploy --image gcr.io/PROJECT-ID/APPLICATION-ID --platform managed --project PROJECT-ID
You'll be prompted to:
- Enable the API: Select yes.
- Region: Select the one you have the database hosted.
- Allow unauthenticated invocations: Select yes.
You may want to create a deployment script.
backend/package.json
{
...
"scripts": {
...
"deploy:staging": "gcloud builds submit --tag gcr.io/PROJECT-ID-STAGING/APPLICATION-ID --project PROJECT-ID-STAGING && gcloud run deploy backend --image gcr.io/PROJECT-ID-STAGING/APPLICATION-ID --platform managed --project PROJECT-ID-STAGING --memory=2Gi --region us-central1",
"deploy:production": "gcloud builds submit --tag gcr.io/PROJECT-ID-PRODUCTION/APPLICATION-ID --project PROJECT-ID-PRODUCTION && gcloud run deploy backend --image gcr.io/PROJECT-ID-PRODUCTION/APPLICATION-ID --platform managed --project PROJECT-ID-PRODUCTION --memory=2Gi --region us-central1"
},
...
}
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
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 modified 2yr ago