deploy-cloud-run.shโข3.15 kB
#!/bin/bash
# Deployment script for Google Cloud Run
# Usage: ./deploy-cloud-run.sh [project-id] [region]
set -e
# Configuration
PROJECT_ID=${1:-${GOOGLE_CLOUD_PROJECT}}
REGION=${2:-us-central1}
SERVICE_NAME="langchain-agent-mcp-server"
IMAGE_NAME="gcr.io/${PROJECT_ID}/${SERVICE_NAME}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}๐ Deploying LangChain Agent MCP Server to Google Cloud Run${NC}"
echo ""
# Check if PROJECT_ID is set
if [ -z "$PROJECT_ID" ]; then
echo -e "${RED}โ Error: PROJECT_ID not set${NC}"
echo "Usage: ./deploy-cloud-run.sh [project-id] [region]"
echo "Or set GOOGLE_CLOUD_PROJECT environment variable"
exit 1
fi
echo -e "${YELLOW}Configuration:${NC}"
echo " Project ID: $PROJECT_ID"
echo " Region: $REGION"
echo " Service Name: $SERVICE_NAME"
echo ""
# Check if gcloud is installed
if ! command -v gcloud &> /dev/null; then
echo -e "${RED}โ Error: gcloud CLI not found${NC}"
echo "Please install Google Cloud SDK: https://cloud.google.com/sdk/docs/install"
exit 1
fi
# Check if docker is installed
if ! command -v docker &> /dev/null; then
echo -e "${RED}โ Error: Docker not found${NC}"
echo "Please install Docker: https://docs.docker.com/get-docker/"
exit 1
fi
# Set the project
echo -e "${YELLOW}๐ Setting GCP project...${NC}"
gcloud config set project $PROJECT_ID
# Enable required APIs
echo -e "${YELLOW}๐ง Enabling required APIs...${NC}"
gcloud services enable cloudbuild.googleapis.com
gcloud services enable run.googleapis.com
gcloud services enable containerregistry.googleapis.com
# Build the Docker image
echo -e "${YELLOW}๐๏ธ Building Docker image...${NC}"
docker build -t $IMAGE_NAME .
# Push the image to Container Registry
echo -e "${YELLOW}๐ค Pushing image to Container Registry...${NC}"
docker push $IMAGE_NAME
# Deploy to Cloud Run
echo -e "${YELLOW}๐ Deploying to Cloud Run...${NC}"
gcloud run deploy $SERVICE_NAME \
--image $IMAGE_NAME \
--platform managed \
--region $REGION \
--allow-unauthenticated \
--memory 2Gi \
--cpu 2 \
--timeout 300 \
--max-instances 10 \
--min-instances 0 \
--set-env-vars "OPENAI_MODEL=gpt-4o-mini,MAX_ITERATIONS=10,VERBOSE=false" \
--port 8000
# Get the service URL
SERVICE_URL=$(gcloud run services describe $SERVICE_NAME --platform managed --region $REGION --format 'value(status.url)')
echo ""
echo -e "${GREEN}โ
Deployment complete!${NC}"
echo ""
echo -e "${GREEN}Service URL:${NC} $SERVICE_URL"
echo ""
echo -e "${YELLOW}โ ๏ธ Important: Set your OPENAI_API_KEY as a secret:${NC}"
echo " gcloud run services update $SERVICE_NAME \\"
echo " --update-secrets=OPENAI_API_KEY=openai-api-key:latest \\"
echo " --region $REGION"
echo ""
echo -e "${YELLOW}Or set it as an environment variable:${NC}"
echo " gcloud run services update $SERVICE_NAME \\"
echo " --set-env-vars OPENAI_API_KEY=your-key-here \\"
echo " --region $REGION"
echo ""
echo -e "${GREEN}Test your deployment:${NC}"
echo " curl $SERVICE_URL/health"
echo " curl $SERVICE_URL/mcp/manifest"