Browse by type
A template repository for a Cloud Run microservice, written in Go.
gcloud services enable run.googleapis.com
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:
.template/templates.json allows for use as an app template) - VSCode, IntelliJSet Project Id:
bash
export GOOGLE_CLOUD_PROJECT=<GCP_PROJECT_ID>
Build and Start the server:
bash
go build -o server && ./server
Set Project Id:
bash
export GOOGLE_CLOUD_PROJECT=<GCP_PROJECT_ID>
Enable the Artifact Registry API:
bash
gcloud services enable artifactregistry.googleapis.com
Create an Artifact Registry repo:
bash
export REPOSITORY="samples"
export REGION=us-central1
gcloud artifacts repositories create $REPOSITORY --location $REGION --repository-format "docker"
Use the gcloud credential helper to authorize Docker to push to your Container Registry:
bash
gcloud auth configure-docker
Build and push the container using docker:
bash
gcloud builds submit --pack image=us-central1-docker.pkg.dev/$GOOGLE_CLOUD_PROJECT/samples/microservice-template:manual
Deploy to Cloud Run:
bash
gcloud run deploy microservice-template \
--image us-central1-docker.pkg.dev/$GOOGLE_CLOUD_PROJECT/samples/microservice-template:manual
Pass credentials via GOOGLE_APPLICATION_CREDENTIALS env var:
bash
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
Set Project Id:
bash
export GOOGLE_CLOUD_PROJECT=<GCP_PROJECT_ID>
Run unit tests
bash
go test ./...
Run system tests
bash
gcloud builds submit \
--config advance.cloudbuild.yaml \
--substitutions=COMMIT_SHA=manual,REPO_NAME=manual
The Cloud Build configuration file will build and deploy the containerized
service to Cloud Run, run tests, 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"
This repo performs basic periodic testing for maintenance. Please use the issue tracker for bug reports, features requests and submitting pull requests.
Please see the contributing guidelines
This library is licensed under Apache 2.0. Full license text is available in LICENSE.
$ claude mcp add cloud-run-microservice-template-go \
-- python -m otcore.mcp_server <graph>