image_management_api/deployment/fix-cloud-run-deployment.sh
2025-05-25 00:32:06 +02:00

135 lines
4.1 KiB
Bash

#!/bin/bash
set -e
# Configuration
PROJECT_ID="gen-lang-client-0424120530"
REGION="us-central1"
SERVICE_NAME="sereact"
echo "=== Cloud Run Deployment Fix Script ==="
echo "Project: $PROJECT_ID"
echo "Region: $REGION"
echo "Service: $SERVICE_NAME"
echo ""
# Function to check if gcloud is authenticated
check_auth() {
if ! gcloud auth list --filter=status:ACTIVE --format="value(account)" | grep -q .; then
echo "ERROR: No active gcloud authentication found."
echo "Please run: gcloud auth login"
exit 1
fi
}
# Function to clean up problematic revisions
cleanup_revisions() {
echo "Step 1: Cleaning up problematic revisions..."
# Get all revisions for the service
REVISIONS=$(gcloud run revisions list --service=$SERVICE_NAME --region=$REGION --project=$PROJECT_ID --format="value(metadata.name)" 2>/dev/null || echo "")
if [ -z "$REVISIONS" ]; then
echo "No revisions found or service doesn't exist."
return 0
fi
echo "Found revisions:"
echo "$REVISIONS"
echo ""
# Delete old revisions (keep the latest one for now)
REVISION_COUNT=$(echo "$REVISIONS" | wc -l)
if [ $REVISION_COUNT -gt 1 ]; then
echo "Deleting old revisions to prevent conflicts..."
echo "$REVISIONS" | head -n -1 | while read revision; do
if [ ! -z "$revision" ]; then
echo "Deleting revision: $revision"
gcloud run revisions delete "$revision" --region=$REGION --project=$PROJECT_ID --quiet || echo "Failed to delete $revision (may be in use)"
fi
done
fi
}
# Function to force a new deployment
force_redeploy() {
echo "Step 2: Forcing a new deployment with Terraform..."
cd "$(dirname "$0")/terraform"
# Taint the Cloud Run service to force recreation
echo "Tainting Cloud Run service to force recreation..."
terraform taint google_cloud_run_service.sereact || echo "Service not in state or already tainted"
# Apply the configuration
echo "Applying Terraform configuration..."
terraform apply -auto-approve
cd - > /dev/null
}
# Function to verify deployment
verify_deployment() {
echo "Step 3: Verifying deployment..."
# Wait a moment for the service to be ready
sleep 10
# Check service status
SERVICE_URL=$(gcloud run services describe $SERVICE_NAME --region=$REGION --project=$PROJECT_ID --format="value(status.url)" 2>/dev/null || echo "")
if [ ! -z "$SERVICE_URL" ]; then
echo "✅ Service deployed successfully!"
echo "Service URL: $SERVICE_URL"
# Test the service
echo "Testing service health..."
if curl -s -o /dev/null -w "%{http_code}" "$SERVICE_URL/health" | grep -q "200"; then
echo "✅ Service is responding correctly!"
else
echo "⚠️ Service deployed but health check failed. Check logs:"
echo "gcloud logging read \"resource.type=cloud_run_revision AND resource.labels.service_name=$SERVICE_NAME\" --limit=10 --project=$PROJECT_ID"
fi
else
echo "❌ Service deployment failed."
exit 1
fi
}
# Main execution
main() {
echo "Starting Cloud Run deployment fix..."
echo ""
check_auth
cleanup_revisions
force_redeploy
verify_deployment
echo ""
echo "=== Deployment Fix Complete ==="
echo "If you continue to have issues, try:"
echo "1. Building a new image: ./deployment/deploy.sh --deploy --build"
echo "2. Checking logs: gcloud logging read \"resource.type=cloud_run_revision AND resource.labels.service_name=$SERVICE_NAME\" --limit=10 --project=$PROJECT_ID"
}
# Help function
show_help() {
echo "Usage: $0 [--help]"
echo ""
echo "This script fixes Cloud Run deployment issues by:"
echo "1. Cleaning up problematic revisions"
echo "2. Forcing a new deployment with Terraform"
echo "3. Verifying the deployment"
echo ""
echo "Options:"
echo " --help Show this help message"
}
# Parse arguments
if [ "$1" = "--help" ]; then
show_help
exit 0
fi
# Run main function
main