ScaffoldHub - v2
  • Introduction
  • Modeling
    • Two-way Relationships
  • Setup
    • Backend
    • Frontend
    • File Storage
    • Emails with SendGrid
    • Payments with Stripe
  • Debugging
  • Deployment
    • Database
      • SQL
      • MongoDB
    • Backend
      • Google Cloud - App Engine
      • Google Cloud - Run
      • Under construction...
    • Frontend
      • Firebase Hosting
      • Heroku w/ Subdomains
      • Under construction...
  • Features
    • Projects
    • Preview
    • Tenants
      • Single-Tenant
      • Multi-Tenant
      • Multi-Tenant (w/ subdomains)
    • Payments
    • Security
    • Authentication
      • Sign-in and Sign-up
      • Invitation
      • Password Reset
      • Password Change
      • Email Verification
    • Audit Logs
    • Settings
    • Internationalization (I18n)
    • Entity
      • Form
      • Filter and List
      • Export
      • Import
    • API Documentation
  • Architecture
    • Security
    • File Storage
    • Internationalization (I18n)
    • Payments
    • Typescript Support
    • Rate limiting
    • Technologies Versions
    • Under construction...
  • Recipes
    • Testing the API with Postman
    • Enterprise sign-in with WorkOS
    • Under construction...
  • Support
  • Changelog
    • Documentation
    • Scaffolds
  • Custom Development
  • Legacy Scaffolds
  • Go to ScaffoldHub
Powered by GitBook
On this page
  • Requirements
  • Docker Files
  • Google Cloud Project
  • Enable billing
  • Google SDK
  • Deploy
  • Create deployment scripts
  • Environment Variables

Was this helpful?

  1. Deployment
  2. Backend

Google Cloud - Run

PreviousGoogle Cloud - App EngineNextUnder construction...

Last updated 4 years ago

Was this helpful?

is a fully managed compute platform for deploying and scaling containerized applications.

Requirements

  • (if payments are enabled)

  • (Google Cloud Storage is preferable)

  • (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

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

Google Cloud Project

Create a new project for the production environment. To create a staging environment the steps are the same.

Enable billing

Google 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.

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.

Create deployment scripts

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"
  },
  ...
}

Environment Variables

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.

Go to and create an account.

The project needs the billing enabled: .

Install the .

Go to the .

Set this to your URL (if you have one already).

https://cloud.google.com/
https://cloud.google.com/billing/docs/how-to/modify-project
https://cloud.google.com/sdk
Google Cloud Run console
Backend
hosted frontend
Google Cloud Run
Emails with Sendgrid
Payments with Stripe
Database
https://console.cloud.google.com/iam-admin/iamconsole.cloud.google.com
File Storage