deploy.sh•5.05 kB
#!/bin/bash
# Deployment script for Finizi B4B MCP Server to Cloud Run
# Usage: ./deploy.sh [OPTIONS]
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Default values
PROJECT_ID=""
REGION="us-central1"
SERVICE_NAME="finizi-b4b-mcp"
B4B_API_BASE_URL=""
LOG_LEVEL="INFO"
ALLOW_UNAUTHENTICATED="true"
# Function to print colored output
print_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# Function to display usage
usage() {
cat << EOF
Usage: $0 [OPTIONS]
Deploy Finizi B4B MCP Server to Google Cloud Run
OPTIONS:
-p, --project PROJECT_ID Google Cloud Project ID (required)
-r, --region REGION Cloud Run region (default: us-central1)
-s, --service SERVICE_NAME Service name (default: finizi-b4b-mcp)
-b, --b4b-api-url URL B4B API base URL (required)
-l, --log-level LEVEL Log level (default: INFO)
--no-auth Allow unauthenticated access (default: true)
--require-auth Require authentication
-h, --help Display this help message
EXAMPLES:
# Deploy with required parameters
./deploy.sh -p my-project -b https://api.finizi.ai
# Deploy to specific region with authentication required
./deploy.sh -p my-project -b https://api.finizi.ai -r europe-west1 --require-auth
# Deploy with debug logging
./deploy.sh -p my-project -b https://api.finizi.ai -l DEBUG
EOF
exit 1
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-p|--project)
PROJECT_ID="$2"
shift 2
;;
-r|--region)
REGION="$2"
shift 2
;;
-s|--service)
SERVICE_NAME="$2"
shift 2
;;
-b|--b4b-api-url)
B4B_API_BASE_URL="$2"
shift 2
;;
-l|--log-level)
LOG_LEVEL="$2"
shift 2
;;
--no-auth)
ALLOW_UNAUTHENTICATED="true"
shift
;;
--require-auth)
ALLOW_UNAUTHENTICATED="false"
shift
;;
-h|--help)
usage
;;
*)
print_error "Unknown option: $1"
usage
;;
esac
done
# Validate required parameters
if [ -z "$PROJECT_ID" ]; then
print_error "Project ID is required"
usage
fi
if [ -z "$B4B_API_BASE_URL" ]; then
print_error "B4B API base URL is required"
usage
fi
# Print deployment configuration
print_info "Deployment Configuration:"
echo " Project ID: $PROJECT_ID"
echo " Region: $REGION"
echo " Service Name: $SERVICE_NAME"
echo " B4B API URL: $B4B_API_BASE_URL"
echo " Log Level: $LOG_LEVEL"
echo " Authentication: $([ "$ALLOW_UNAUTHENTICATED" = "true" ] && echo "Not Required" || echo "Required")"
echo ""
# Check if gcloud is installed
if ! command -v gcloud &> /dev/null; then
print_error "gcloud CLI is not installed. Please install it first."
exit 1
fi
# Set the project
print_info "Setting Google Cloud project to $PROJECT_ID..."
gcloud config set project "$PROJECT_ID"
# Enable required APIs
print_info "Enabling required Google Cloud APIs..."
gcloud services enable \
run.googleapis.com \
containerregistry.googleapis.com \
cloudbuild.googleapis.com
# Build the Docker image
print_info "Building Docker image..."
IMAGE_NAME="gcr.io/$PROJECT_ID/$SERVICE_NAME:$(date +%Y%m%d-%H%M%S)"
docker build -t "$IMAGE_NAME" .
# Push the image to Google Container Registry
print_info "Pushing image to Google Container Registry..."
docker push "$IMAGE_NAME"
# Deploy to Cloud Run
print_info "Deploying to Cloud Run..."
gcloud run deploy "$SERVICE_NAME" \
--image "$IMAGE_NAME" \
--region "$REGION" \
--platform managed \
$([ "$ALLOW_UNAUTHENTICATED" = "true" ] && echo "--allow-unauthenticated" || echo "--no-allow-unauthenticated") \
--max-instances 10 \
--min-instances 0 \
--memory 512Mi \
--cpu 1 \
--timeout 60s \
--set-env-vars "B4B_API_BASE_URL=$B4B_API_BASE_URL,LOG_LEVEL=$LOG_LEVEL"
# Get the service URL
SERVICE_URL=$(gcloud run services describe "$SERVICE_NAME" --region "$REGION" --format 'value(status.url)')
print_info "Deployment complete!"
echo ""
echo "Service URL: $SERVICE_URL"
echo "MCP Endpoint: $SERVICE_URL/mcp"
echo "Health Check: $SERVICE_URL/health"
echo ""
# Test the deployment
print_info "Testing health endpoint..."
if curl -s "$SERVICE_URL/health" | grep -q "healthy"; then
print_info "Health check passed ✓"
else
print_warning "Health check failed. Please check the logs."
fi
echo ""
print_info "View logs with: gcloud run logs read $SERVICE_NAME --region $REGION"
print_info "View service: gcloud run services describe $SERVICE_NAME --region $REGION"