MCPcopy Index your code
hub / github.com/GoogleCloudPlatform/cloud-run-microservice-template-nodejs

github.com/GoogleCloudPlatform/cloud-run-microservice-template-nodejs @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
6 symbols 19 edges 7 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Cloud Run Template Microservice

A template repository for a Cloud Run microservice, written in Node.js.

Run on Google Cloud

Prerequisite

  • Enable the Cloud Run API via the console or CLI:
gcloud services enable run.googleapis.com

Features

  • Express: Web server framework
  • Buildpack support Tooling to build production-ready container images from source code and without a Dockerfile
  • Dockerfile: Container build instructions, if needed to replace buildpack for custom build
  • SIGTERM handler: Catch termination signal for cleanup before Cloud Run stops the container
  • Service metadata: Access service metadata, project Id and region, at runtime
  • Local development utilities: Auto-restart with changes and prettify logs
  • Structured logging w/ Log Correlation JSON formatted logger, parsable by Cloud Logging, with automatic correlation of container logs to a request log.
  • Unit and System tests Basic unit and system tests setup for the microservice

Local Development

Cloud Code

This template works with Cloud Code, an IDE extension to let you rapidly iterate, debug, and run code on Kubernetes and Cloud Run.

Learn how to use Cloud Code for:

CLI tooling

Local development

  1. Set Project Id: bash export GOOGLE_CLOUD_PROJECT=<GCP_PROJECT_ID>
  2. Start the server with hot reload: bash npm run dev

Deploying a Cloud Run service

  1. Set Project Id: bash export GOOGLE_CLOUD_PROJECT=<GCP_PROJECT_ID>

  2. Enable the Artifact Registry API: bash gcloud services enable artifactregistry.googleapis.com

  3. Create an Artifact Registry repo: bash export REPOSITORY="samples" export REGION=us-central1 gcloud artifacts repositories create $REPOSITORY --location $REGION --repository-format "docker"

  4. Use the gcloud credential helper to authorize Docker to push to your Artifact Registry: bash gcloud auth configure-docker

  5. Build the container using a buildpack: bash npm run build-image

  6. Deploy to Cloud Run: bash npm run deploy

Run sample tests

  1. Pass credentials via GOOGLE_APPLICATION_CREDENTIALS env var: bash export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"

  2. Set Project Id: bash export GOOGLE_CLOUD_PROJECT=<GCP_PROJECT_ID>

  3. Run unit tests bash npm run test

  4. Run system tests bash gcloud builds submit \ --config test/advance.cloudbuild.yaml \ --substitutions 'COMMIT_SHA=manual' The Cloud Build configuration file will build and deploy the containerized service to Cloud Run, run tests managed by NPM, then clean up testing resources. This configuration restricts public access to the test service. Therefore, service accounts need to have the permission to issue Id tokens for request authorization:

    • Enable Cloud Run, Cloud Build, Artifact Registry, and IAM APIs: bash gcloud services enable run.googleapis.com cloudbuild.googleapis.com iamcredentials.googleapis.com artifactregistry.googleapis.com

    • Set environment variables. bash export PROJECT_ID="$(gcloud config get-value project)" export PROJECT_NUMBER="$(gcloud projects describe $(gcloud config get-value project) --format='value(projectNumber)')"

    • Create an Artifact Registry repo (or use another already created repo): bash export REPOSITORY="samples" export REGION=us-central1 gcloud artifacts repositories create $REPOSITORY --location $REGION --repository-format "docker"

    • Create service account token-creator with Service Account Token Creator and Cloud Run Invoker roles. ```bash gcloud iam service-accounts create token-creator

      gcloud projects add-iam-policy-binding $PROJECT_ID \ --member="serviceAccount:token-creator@$PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/iam.serviceAccountTokenCreator" gcloud projects add-iam-policy-binding $PROJECT_ID \ --member="serviceAccount:token-creator@$PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/run.invoker" ```

    • Add Service Account Token Creator role to the Cloud Build service account. bash gcloud projects add-iam-policy-binding $PROJECT_ID \ --member="serviceAccount:$PROJECT_NUMBER@cloudbuild.gserviceaccount.com" \ --role="roles/iam.serviceAccountTokenCreator"

Maintenance & Support

This repo performs basic periodic testing for maintenance. Please use the issue tracker for bug reports, features requests and submitting pull requests.

Contributions

Please see the contributing guidelines

License

This library is licensed under Apache 2.0. Full license text is available in LICENSE.

Core symbols most depended-on inside this repo

main
called by 1
index.js
fetchProjectId
called by 1
utils/metadata.js
initLogCorrelation
called by 1
utils/logging.js
fetchServiceRegion
called by 0
utils/metadata.js
authenticatedRequest
called by 0
utils/metadata.js
level
called by 0
utils/logging.js

Shape

Function 6

Languages

TypeScript100%

Modules by API surface

utils/metadata.js3 symbols
utils/logging.js2 symbols
index.js1 symbols

For agents

$ claude mcp add cloud-run-microservice-template-nodejs \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact