This is a starter project for Google Cloud App Engine Flexible Environment based on nodejs. It is implemented in JavaScript and provides a basic http service with cors, linting, unit tests, system tests and logging.
-
Before you can start coding, make sure that gcloud is installed.
-
This starter also requires Node.js.
-
Fork this template project.
If you already have installed the prerequisites, then you're all set.
-
Open the file package.json and modify the name and version.
-
Edit config.service in package.json and service in app.yaml to change the service name.
-
Modify engines.nodes in package.json and replace all entries of gcr.io/cloud-builders/npm:node-X.X.X in cloudbuild.yaml with your required node version. Also update runtime in app.yaml accordingly.
-
Run
npm install
to apply the changes and update package-lock.json.
-
Install package and its dependencies:
npm install
-
Run eslint:
npm run lint
-
Run unit tests:
npm test
-
Start local instance:
npm start
-
Open browser and navigate to http://localhost:8080/ or http://localhost:8080/gcp to invoke the service.
-
Billing must be enabled.
-
Configure project for gcloud:
gcloud config set project [PROJECT_ID]
Note: Replace
[PROJECT_ID]
with the Google Cloud project id. -
Enable App Engine Flexible Environment.
-
Create an App Engine application:
gcloud app create
Note: Choose an App Engine location based on our requirements.
-
Deploy to google cloud:
npm run deploy
Or you can deploy a promoted version to receive all traffic:
npm run deploy:promote
Also consider to set a version:
npm run deploy -- --version 1-0-0
-
Run system test:
export BASE_URL="[BASE_URL]" npm run test:system
Note: Replace
[BASE_URL]
with the target url to the app engine service, e.g. https://VERSION_ID-dot-SERVICE_ID-dot-PROJECT_ID.REGION_ID.r.appspot.com or https://PROJECT_ID.REGION_ID.r.appspot.com. -
Configure traffic to a version:
npm run split -- --splits='[VERSION_ID]=1'
Or split the traffic across two (or more) versions:
npm run split -- --splits='[VERSION_ID_1]=.5,[VERSION_ID_2]=.5'
Note: Replace
[VERSION_ID]
or[VERSION_ID_X]
with the version of the service. If you are unsure, than list your existing versions:gcloud app versions list
. Also consider to manage versions with Cloud Console. -
Open service in browser: https://VERSION_ID-dot-SERVICE_ID-dot-PROJECT_ID.REGION_ID.r.appspot.com or https://PROJECT_ID.REGION_ID.r.appspot.com. Alternatively use the following command:
gcloud app browse
-
The build process depends on Cloud Build. Please enable the Cloud Build API.
-
The service account of Cloud Build requires access to App Engine and Service Accounts. Open Cloud Build Settings to set the required permissions. Please also follow the instructions of the additional steps and enable the App Engine Admin API.
Test, build and deploy the application as part of your automated pipeline:
npm run cicd
By default, info
level messages are logged. Modify the envrionment variable APP_LOG_LEVEL in app.yaml to change the logging level. The default logName is "projects/[PROJECT_ID]/logs/[PACKAGE_JSON_NAME]_log".
This starter uses winston as logging library. Please read more about how to
write log entries and about all possible logging levels on the project website.
Distributed under the MIT License. See LICENSE for more information.