-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·109 lines (93 loc) · 3.78 KB
/
deploy.sh
File metadata and controls
executable file
·109 lines (93 loc) · 3.78 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
# Cloud Run deployment script for Next.js application
# Configuration
PROJECT_ID="${GCP_PROJECT_ID:-your-project-id}"
SERVICE_NAME="nextjs-cloud-run-demo"
REGION="${CLOUD_RUN_REGION:-us-central1}"
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}🚀 Starting Cloud Run deployment...${NC}"
# Check if gcloud is installed
if ! command -v gcloud &> /dev/null; then
echo -e "${RED}❌ gcloud CLI is not installed. Please install it first.${NC}"
echo "Visit: https://cloud.google.com/sdk/docs/install"
exit 1
fi
# Check if PROJECT_ID is set
if [ "$PROJECT_ID" = "your-project-id" ]; then
echo -e "${YELLOW}⚠️ Please set your GCP project ID:${NC}"
echo "export GCP_PROJECT_ID=your-actual-project-id"
echo "Or edit this script and replace 'your-project-id' with your actual project ID"
exit 1
fi
# Set the project
echo -e "${GREEN}📋 Setting project to ${PROJECT_ID}...${NC}"
gcloud config set project ${PROJECT_ID}
# Enable required APIs
echo -e "${GREEN}🔧 Enabling required APIs...${NC}"
gcloud services enable cloudbuild.googleapis.com
gcloud services enable run.googleapis.com
gcloud services enable containerregistry.googleapis.com
gcloud services enable secretmanager.googleapis.com
# Build the Docker image
echo -e "${GREEN}🏗️ Building Docker image...${NC}"
# Check if running on ARM architecture (Apple Silicon)
if [[ $(uname -m) == "arm64" ]]; then
echo -e "${YELLOW}⚠️ Detected ARM architecture, building for linux/amd64...${NC}"
docker buildx build --platform linux/amd64 -t ${IMAGE_NAME} .
else
docker build -t ${IMAGE_NAME} .
fi
# Push to Google Container Registry
echo -e "${GREEN}📤 Pushing image to GCR...${NC}"
docker push ${IMAGE_NAME}
# Create a demo secret if it doesn't exist
echo -e "${GREEN}🔐 Setting up demo secret...${NC}"
if ! gcloud secrets describe demo-secret --project=${PROJECT_ID} >/dev/null 2>&1; then
echo "demo-secret-value" | gcloud secrets create demo-secret \
--data-file=- \
--project=${PROJECT_ID}
echo -e "${GREEN}✅ Created demo-secret${NC}"
else
echo -e "${YELLOW}ℹ️ demo-secret already exists${NC}"
fi
# Deploy to Cloud Run
echo -e "${GREEN}🚀 Deploying to Cloud Run...${NC}"
gcloud run deploy ${SERVICE_NAME} \
--image ${IMAGE_NAME} \
--platform managed \
--region ${REGION} \
--allow-unauthenticated \
--memory 512Mi \
--cpu 1 \
--timeout 60 \
--concurrency 80 \
--max-instances 100 \
--min-instances 0 \
--set-env-vars="GOOGLE_CLOUD_PROJECT=${PROJECT_ID}"
# Grant Secret Manager access to the Cloud Run service
echo -e "${GREEN}🔑 Granting Secret Manager access...${NC}"
SERVICE_ACCOUNT=$(gcloud run services describe ${SERVICE_NAME} \
--region=${REGION} \
--format='value(spec.template.spec.serviceAccountName)')
if [ -n "$SERVICE_ACCOUNT" ]; then
gcloud secrets add-iam-policy-binding demo-secret \
--member="serviceAccount:${SERVICE_ACCOUNT}" \
--role="roles/secretmanager.secretAccessor" \
--project=${PROJECT_ID}
echo -e "${GREEN}✅ Granted Secret Manager access to ${SERVICE_ACCOUNT}${NC}"
fi
# Get the service URL
SERVICE_URL=$(gcloud run services describe ${SERVICE_NAME} --platform managed --region ${REGION} --format 'value(status.url)')
echo -e "${GREEN}✅ Deployment complete!${NC}"
echo -e "${GREEN}🌐 Your service is available at: ${SERVICE_URL}${NC}"
echo ""
echo -e "${YELLOW}📊 To view logs:${NC}"
echo "gcloud logging read \"resource.type=cloud_run_revision AND resource.labels.service_name=${SERVICE_NAME}\" --limit 50"
echo ""
echo -e "${YELLOW}📈 To view metrics in Cloud Console:${NC}"
echo "https://console.cloud.google.com/run/detail/${REGION}/${SERVICE_NAME}/metrics"