Deploy with Cloud Run
You can deploy your Genkit Dart flows to Cloud Run as a containerized service. This guide shows you how to deploy a Genkit server built with GenkitRouter from package:genkit/io.dart.
Before you begin
Section titled “Before you begin”- Install the Google Cloud CLI.
- Ensure you have a Genkit Dart project set up. If not, follow the Get Started guide.
1. Set up a Google Cloud project
Section titled “1. Set up a Google Cloud project”- Create a new Google Cloud project in the Cloud console.
- Link the project to a billing account.
- Configure the Google Cloud CLI:
gcloud init2. Prepare your Dart project
Section titled “2. Prepare your Dart project”Ensure your project is configured to listen on the port defined by the PORT environment variable, which Cloud Run uses.
Update bin/server.dart
Section titled “Update bin/server.dart”GenkitRouter.serve() listens on all interfaces and reads the port from PORT (falling back to 3400), which is what Cloud Run expects.
import 'package:genkit/genkit.dart';import 'package:genkit/io.dart';// import 'package:your_app/flows.dart'; // Import your flows
void main() async { final ai = Genkit();
// Define or import your flows here final myFlow = ai.defineFlow( name: 'myFlow', fn: (String input, _) async => 'Processed $input', inputSchema: .string(), outputSchema: .string(), );
final genkit = GenkitRouter()..addAction(myFlow); // POST /myFlow
// Uses the PORT environment variable that Cloud Run sets. await genkit.serve( // Only needed if browser apps on other origins call the service. cors: const CorsOptions(allowedOrigins: ['https://myapp.dev']), );}If your server is a Shelf app, mount the router with genkit_shelf instead (see the Shelf tutorial) and bind your Shelf server to Platform.environment['PORT'].
Create a Dockerfile
Section titled “Create a Dockerfile”Create a Dockerfile in the root of your project. This multi-stage build compiles your Dart application into a minimal executable.
# Specify the Dart SDK base image version using dart:<version> (ex: dart:2.12)FROM dart:stable AS build
# Resolve app dependencies.WORKDIR /appCOPY pubspec.* ./RUN dart pub get
# Copy app source code and AOT compile it.COPY . .# Ensure generated files are builtRUN dart run build_runner build -dRUN dart compile exe bin/server.dart -o bin/server
# Build minimal serving image from AOT-compiled `/server` and required system# libraries and configuration files stored in `/runtime/` from the build stage.FROM scratchCOPY --from=build /runtime/ /COPY --from=build /app/bin/server /app/bin/
# Start server.CMD ["/app/bin/server"]3. Deploy to Cloud Run
Section titled “3. Deploy to Cloud Run”Deploy your application using the gcloud tool.
Make API credentials available
Section titled “Make API credentials available”Most flows require API keys (like GOOGLE_API_KEY for Gemini). You should use Secret Manager to securely store these and expose them to your service.
- Create a secret for your API key:
gcloud secrets create google-api-key --data-file=-# (Press Enter, paste your API key, then press Ctrl+D)- Deploy the service, referencing the secret:
gcloud run deploy genkit-server \ --source . \ --allow-unauthenticated \ --region us-central1 \ --set-secrets GOOGLE_API_KEY=google-api-key:latestCloud Run injects a PORT environment variable (8080 by default) and serve() listens on it, so you don’t need to pass --port.
4. Test your deployment
Section titled “4. Test your deployment”After successful deployment, gcloud will print the URL of your service.
curl -X POST https://<your-service-url>/myFlow \ -H "Content-Type: application/json" \ -d '{"data": "Dart on Cloud Run"}'