Google Cloud - App Engine
The configuration for the App Engine server is placed at the app-engine.environment.yaml file.
backend/app-engine.production.yaml
backend/app-engine.staging.yaml
runtime: nodejs10
instance_class: F4_1G
env_variables:
# MongoDB only
DATABASE_CONNECTION: ''
DATABASE_TRANSACTIONS: false
# END - MongoDB only
# SQL only
DATABASE_USERNAME: 'postgres'
DATABASE_DIALECT: 'postgres'
DATABASE_PASSWORD: ''
DATABASE_DATABASE: 'production'
DATABASE_HOST: ''
DATABASE_LOGGING: 'true'
# END - SQL only
DATABASE_INDIVIDUAL_CONNECTIONS_PER_REQUEST: 'false'
TENANT_MODE: 'multi'
AUTH_JWT_SECRET: 'GENERATE_SOME_RANDOM_UUID_HERE'
AUTH_JWT_EXPIRES_IN: '7 days'
SENDGRID_EMAIL_FROM: ''
SENDGRID_KEY: ''
SENDGRID_TEMPLATE_EMAIL_ADDRESS_VERIFICATION: ''
SENDGRID_TEMPLATE_INVITATION: ''
SENDGRID_TEMPLATE_PASSWORD_RESET: ''
FRONTEND_URL: 'https://domain.com:<port>'
FRONTEND_URL_WITH_SUBDOMAIN: 'https://[subdomain].domain.com:<port>'
BACKEND_URL: 'https://YOUR_SERVER_URL/api'
PLAN_STRIPE_SECRET_KEY: ''
PLAN_STRIPE_WEBHOOK_SIGNIN_SECRET: ''
PLAN_STRIPE_PRICES_GROWTH: ''
PLAN_STRIPE_PRICES_ENTERPRISE: ''
FILE_STORAGE_PROVIDER: 'gcp'
FILE_STORAGE_BUCKET: ''
GOOGLE_CLOUD_PLATFORM_CREDENTIALS: '{ "type": "service_account", "project_id": "...", "private_key_id": "...", "private_key": "...", "client_email": "...", "client_id": "...", "auth_uri": "...", "token_uri": "...", "auth_provider_x509_cert_url": "...", "client_x509_cert_url": "..." }'
AWS_ACCESS_KEY_ID: ''
AWS_SECRET_ACCESS_KEY: ''
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://project-id.appspot.com/api.
Because that the app-engine.environment.yaml contains sensitive information, is recommended to add it to .gitignore.
backend/.gitignore
node_modules/
/data
dist/
app-engine.staging.yml
app-engine.production.yml
Files ignored by Git are different from the ones ignored by the Google Cloud Platform, so you must create a .gcloudignore file.
backend/.gcloudignore
.gcloudignore
.git
.gitignore
node_modules/
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
.Before deploying, you must install the dependencies and build the application:
npm install
npm run build
App Engine uses the start script to run the application, but our start script also compiles and watches for changes, which isn't needed for production.
Let's replace the original for
npm run start:watch
, that we will run on localhost, and leave the npm start
for the App Engine.backend/package.json
{
"scripts": {
...
"start": "node ./dist/server.js",
"start:watch": "nodemon --watch \"src/**/*.ts\" -e ts,json --exec \"ts-node --transpile-only ./src/server.ts\"",
...
}
}
To deploy the application you must run this command:
gcloud app deploy app-engine.staging.yaml --project PROJECT-ID
Let's create deployment scripts to automate this:
backend/package.json
{
...
"scripts": {
...
"deploy:staging": "npm run build && gcloud app deploy app-engine.staging.yaml --project PROJECT-ID-STAGING",
"deploy:production": "npm run build && gcloud app deploy app-engine.production.yaml --project PROJECT-ID-PRODUCTION"
}
...
}
Now run:
npm run deploy:staging
// or
npm run deploy:production
Last modified 2yr ago