File size: 3,313 Bytes
fd26b3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
# Deploy using Cloud Build (no local Docker required)
# This uses Google Cloud Build service to build and deploy

set -e

PROJECT_ID=${GCP_PROJECT_ID:-"light-quest-475608-k7"}
REGION=${GCP_REGION:-"us-central1"}
SERVICE_NAME="router-agent"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${GREEN}πŸš€ Deploying Router Agent to Cloud Run using Cloud Build${NC}"

# Check if gcloud is installed
if ! command -v gcloud &> /dev/null; then
    echo -e "${RED}❌ gcloud CLI not found. Please install it: https://cloud.google.com/sdk/docs/install${NC}"
    exit 1
fi

# Set project
echo -e "${YELLOW}πŸ“‹ Setting project to ${PROJECT_ID}...${NC}"
gcloud config set project ${PROJECT_ID}

# Check if Cloud Build API is enabled
echo -e "${YELLOW}πŸ“‹ Checking Cloud Build API...${NC}"
if ! gcloud services list --enabled --filter="name:cloudbuild.googleapis.com" --format="value(name)" | grep -q cloudbuild; then
    echo -e "${YELLOW}⚠️  Cloud Build API not enabled. Attempting to enable...${NC}"
    gcloud services enable cloudbuild.googleapis.com run.googleapis.com containerregistry.googleapis.com || {
        echo -e "${RED}❌ Failed to enable APIs. Please enable them manually:${NC}"
        echo -e "${YELLOW}   https://console.cloud.google.com/apis/library/cloudbuild.googleapis.com?project=${PROJECT_ID}${NC}"
        exit 1
    }
fi

# Submit build to Cloud Build
echo -e "${GREEN}πŸ“¦ Submitting build to Cloud Build...${NC}"
COMMIT_SHA=$(git rev-parse --short HEAD 2>/dev/null || echo "latest")

gcloud builds submit --config=cloudbuild.yaml \
    --substitutions=_SERVICE_NAME=${SERVICE_NAME},_REGION=${REGION},COMMIT_SHA=${COMMIT_SHA} \
    . || {
    echo -e "${YELLOW}⚠️  Cloud Build failed. Trying alternative approach...${NC}"
    
    # Alternative: Build and deploy separately
    echo -e "${GREEN}πŸ“¦ Building image with Cloud Build...${NC}"
    gcloud builds submit --tag gcr.io/${PROJECT_ID}/${SERVICE_NAME}:latest . || {
        echo -e "${RED}❌ Build failed. Please check:${NC}"
        echo -e "${YELLOW}   1. Cloud Build API is enabled${NC}"
        echo -e "${YELLOW}   2. You have permissions (roles/cloudbuild.builds.editor)${NC}"
        echo -e "${YELLOW}   3. Billing is enabled for the project${NC}"
        exit 1
    }
    
    echo -e "${GREEN}πŸš€ Deploying to Cloud Run...${NC}"
    gcloud run deploy ${SERVICE_NAME} \
        --image gcr.io/${PROJECT_ID}/${SERVICE_NAME}:latest \
        --platform managed \
        --region ${REGION} \
        --allow-unauthenticated \
        --port 7860 \
        --memory 8Gi \
        --cpu 4 \
        --timeout 3600 \
        --max-instances 10 \
        --set-env-vars "GRADIO_SERVER_NAME=0.0.0.0,GRADIO_SERVER_PORT=7860" \
        --quiet || {
        echo -e "${RED}❌ Deployment failed. Please check:${NC}"
        echo -e "${YELLOW}   1. Cloud Run API is enabled${NC}"
        echo -e "${YELLOW}   2. You have permissions (roles/run.admin)${NC}"
        exit 1
    }
}

echo -e "${GREEN}βœ… Deployment complete!${NC}"
SERVICE_URL=$(gcloud run services describe ${SERVICE_NAME} --platform managed --region ${REGION} --format 'value(status.url)' 2>/dev/null || echo "Check Cloud Console")
echo -e "${GREEN}🌐 Service URL: ${SERVICE_URL}${NC}"