Skip to main content

Deployment Guide – Google Cloud Run

This document explains how to build, deploy, and run the Dart backend on Google Cloud Run using Docker.


Requirements

  • Google Cloud SDK installed and authenticated
  • A Google Cloud project created
  • gcloud CLI configured (gcloud init)
  • Docker installed

Docker Setup

Ensure you have the following Dockerfile in your project root:

FROM dart:stable AS build

WORKDIR /app

COPY pubspec.* ./
RUN dart pub get

COPY . .

RUN dart compile exe bin/server.dart -o bin/server

FROM scratch

COPY --from=build /runtime/ /
COPY --from=build /app/bin/server /app/bin/server
COPY --from=build /app/.env /app/.env
COPY --from=build /app/.env.local /app/.env.local

EXPOSE 8080

ENTRYPOINT ["/app/bin/server"]

Update path to the server if needed.


Local Docker Test (optional)

To test locally:

docker build -t hudi-backend .
docker run -p 8080:8080 hudi-backend

Deploy to Google Cloud Run

1. Build and push to Google Artifact Registry

gcloud builds submit --tag gcr.io/YOUR_PROJECT_ID/hudi-backend

Replace YOUR_PROJECT_ID with your actual GCP project ID.

2. Deploy to Cloud Run

gcloud run deploy hudi-backend \
--image gcr.io/YOUR_PROJECT_ID/hudi-backend \
--platform managed \
--region YOUR_REGION \
--allow-unauthenticated \
--set-env-vars API_TOKEN=your_token_here,FIREBASE_PROJECT_ID=your_project_id_here,MONGO_DB_URI=your_mongo_uri_here

Replace YOUR_PROJECT_ID, YOUR_REGION and the env values accordingly.

You can also link environment variables via Google Secret Manager for better security.


After Deployment

Note the service URL printed in the terminal after deployment.

You can test routes with Postman or curl.


Production Notes

  • Never push .env files to Git.
  • Prefer using Secret Manager for managing environment variables.
  • Ensure only required routes are --allow-unauthenticated, others should be protected with Firebase or API key.

Documentation last updated: 2025-04-01