#!/usr/bin/env bash
# Copyright 2026 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# Deploy to Fly.io
# =================
#
# Deploys the Genkit endpoints app to Fly.io using the Containerfile.
# Fly.io provides global edge deployment with auto-scaling.
#
# Prerequisites:
# - flyctl CLI installed (https://fly.io/docs/flyctl/install/)
# - Authenticated: flyctl auth login
# - GEMINI_API_KEY set in your environment
#
# Usage:
# ./deploy_flyio.sh # Default app name
# ./deploy_flyio.sh --app=my-genkit-app # Custom app name
# ./deploy_flyio.sh --region=lhr # Deploy to London
set -euo pipefail
cd "$(dirname "$0")"
source "$(dirname "$0")/scripts/_common.sh"
APP_NAME="${APP_NAME:-genkit-asgi}"
REGION="${REGION:-iad}"
# Parse arguments.
for arg in "$@"; do
case "$arg" in
--app=*) APP_NAME="${arg#*=}" ;;
--region=*) REGION="${arg#*=}" ;;
--help|-h)
echo "Usage: ./deploy_flyio.sh [--app=NAME] [--region=REGION]"
echo ""
echo "Environment variables:"
echo " GEMINI_API_KEY Required. Your Gemini API key."
echo " APP_NAME Fly.io app name (default: genkit-asgi)."
echo " REGION Fly.io region code (default: iad)."
echo ""
echo "Options:"
echo " --app=NAME Fly.io app name."
echo " --region=REGION Fly.io region (run 'flyctl platform regions' for list)."
echo ""
echo "Common regions: iad (Virginia), lhr (London), nrt (Tokyo), syd (Sydney)"
exit 0
;;
esac
done
# ── Prerequisites ──────────────────────────────────────────────────────
# 1. Check flyctl CLI is installed.
check_flyctl_installed || exit 1
# 2. Check GEMINI_API_KEY (interactive prompt if missing).
check_env_var "GEMINI_API_KEY" "https://aistudio.google.com/apikey" || exit 1
# Generate fly.toml if it doesn't exist.
FLY_TOML="fly.toml"
if [[ ! -f "$FLY_TOML" ]]; then
echo "📝 Generating ${FLY_TOML}..."
cat > "$FLY_TOML" << EOF
# Fly.io configuration for the FastAPI + Genkit sample.
# Generated by deploy_flyio.sh — edit as needed.
app = "${APP_NAME}"
primary_region = "${REGION}"
[build]
dockerfile = "Containerfile"
[env]
PORT = "8080"
[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = "stop"
auto_start_machines = true
min_machines_running = 0
[[http_service.checks]]
grace_period = "10s"
interval = "30s"
method = "GET"
path = "/health"
timeout = "5s"
[[vm]]
memory = "512mb"
cpu_kind = "shared"
cpus = 1
EOF
echo " Created ${FLY_TOML}"
fi
echo "🚀 Deploying ${APP_NAME} to Fly.io (${REGION})..."
echo ""
# Create the app if it doesn't exist yet.
if ! flyctl apps list --json 2>/dev/null | grep -q "\"${APP_NAME}\""; then
echo "📦 Creating Fly.io app: ${APP_NAME}..."
flyctl apps create "${APP_NAME}" --machines || true
fi
# Set the API key as a secret (not in fly.toml for security).
echo "🔑 Setting GEMINI_API_KEY secret..."
echo "${GEMINI_API_KEY}" | flyctl secrets set GEMINI_API_KEY=- --app "${APP_NAME}" 2>/dev/null || \
flyctl secrets set "GEMINI_API_KEY=${GEMINI_API_KEY}" --app "${APP_NAME}"
echo ""
echo "🏗️ Building and deploying..."
flyctl deploy --app "${APP_NAME}" --region "${REGION}"
echo ""
echo "✅ Deployed! Your app is available at:"
echo " https://${APP_NAME}.fly.dev"
echo ""
echo " Dashboard: https://fly.io/apps/${APP_NAME}"
echo " Logs: flyctl logs --app ${APP_NAME}"