#!/bin/bash set -e # Configuration PROJECT_ID=$(gcloud config get-value project) IMAGE_NAME="sereact-api" REGION="us-central1" SERVICE_NAME="sereact" # Help function function show_help { echo "Usage: $0 [options]" echo "Options:" echo " --provision Run Terraform to provision cloud resources" echo " --build Build and push Docker image" echo " --deploy Deploy to Cloud Run" echo " --destroy Destroy cloud resources with Terraform" echo " --all Do all of the above (except destroy)" echo " --help Show this help message" exit 0 } # Check if any arguments were provided if [ $# -eq 0 ]; then show_help fi # Process arguments PROVISION=false BUILD=false DEPLOY=false DESTROY=false while [[ $# -gt 0 ]]; do case "$1" in --provision) PROVISION=true shift ;; --build) BUILD=true shift ;; --deploy) DEPLOY=true shift ;; --destroy) DESTROY=true shift ;; --all) PROVISION=true BUILD=true DEPLOY=true shift ;; --help) show_help ;; *) echo "Unknown option: $1" show_help ;; esac done # Ensure gcloud is configured if [ -z "$PROJECT_ID" ]; then echo "ERROR: No Google Cloud project is set. Run 'gcloud config set project YOUR_PROJECT_ID'" exit 1 fi echo "Using Google Cloud project: $PROJECT_ID" echo "===================================" # Destroy resources with Terraform if [ "$DESTROY" = true ]; then echo "WARNING: This will destroy all cloud resources managed by Terraform!" echo "This includes Cloud Run services, Firestore database, storage buckets, and all data!" echo "This action cannot be undone." read -p "Are you sure you want to continue? (yes/no): " confirm if [ "$confirm" != "yes" ]; then echo "Destroy operation cancelled." exit 0 fi echo "Destroying cloud resources with Terraform..." cd "$(dirname "$0")/terraform" # Check if terraform.tfvars exists if [ ! -f terraform.tfvars ]; then echo "ERROR: terraform.tfvars not found. Cannot proceed with destroy." echo "Make sure you have provisioned resources first." exit 1 fi terraform init # Import existing resources if they exist and aren't in state echo "Importing any existing resources not managed by Terraform..." # Try to import Cloud Run service terraform import google_cloud_run_service.sereact "locations/$REGION/namespaces/$PROJECT_ID/services/sereact" 2>/dev/null || echo "Cloud Run service import skipped (already in state or doesn't exist)" # Try to import Firestore database terraform import google_firestore_database.database "projects/$PROJECT_ID/databases/(default)" 2>/dev/null || echo "Firestore database import skipped (already in state or doesn't exist)" # Destroy everything through Terraform terraform destroy -auto-approve cd - > /dev/null echo "All resources destroyed successfully via Terraform." exit 0 fi # Provision resources with Terraform if [ "$PROVISION" = true ]; then echo "Provisioning cloud resources with Terraform..." cd "$(dirname "$0")/terraform" # Check if terraform.tfvars exists, if not copy from example if [ ! -f terraform.tfvars ]; then echo "Creating terraform.tfvars from example..." cp terraform.tfvars.example terraform.tfvars # Replace project ID in tfvars file sed -i "s/your-gcp-project-id/$PROJECT_ID/g" terraform.tfvars echo "Please review and edit terraform.tfvars with your desired values" exit 0 fi terraform init terraform apply cd - > /dev/null echo "Provisioning completed." fi # Build and push Docker image if [ "$BUILD" = true ]; then echo "Building and pushing Docker image..." # Enable Docker to authenticate to GCR gcloud auth configure-docker gcr.io # Build the image with timestamp tag TAG=$(date +%Y%m%d-%H%M%S) FULL_IMAGE_NAME="gcr.io/$PROJECT_ID/$IMAGE_NAME:$TAG" LATEST_IMAGE_NAME="gcr.io/$PROJECT_ID/$IMAGE_NAME:latest" echo "Building image: $FULL_IMAGE_NAME" docker build -t "$FULL_IMAGE_NAME" -t "$LATEST_IMAGE_NAME" -f Dockerfile . echo "Pushing images to Container Registry..." docker push "$FULL_IMAGE_NAME" docker push "$LATEST_IMAGE_NAME" echo "Image built and pushed successfully." fi # Deploy to Cloud Run if [ "$DEPLOY" = true ]; then echo "Deploying to Cloud Run via Terraform..." # Ensure the latest image exists LATEST_IMAGE_NAME="gcr.io/$PROJECT_ID/$IMAGE_NAME:latest" # Check if image exists if ! docker manifest inspect "$LATEST_IMAGE_NAME" > /dev/null 2>&1; then echo "ERROR: Image $LATEST_IMAGE_NAME not found. Please run --build first." exit 1 fi # Deploy using Terraform cd "$(dirname "$0")/terraform" # Check if terraform.tfvars exists if [ ! -f terraform.tfvars ]; then echo "ERROR: terraform.tfvars not found. Please run --provision first." exit 1 fi echo "Applying Terraform configuration to deploy Cloud Run service..." terraform init terraform apply -auto-approve cd - > /dev/null # Get service URL SERVICE_URL=$(gcloud run services describe "sereact" --region="$REGION" --format='value(status.url)') echo "Deployment completed successfully." echo "Service URL: $SERVICE_URL" fi echo "All operations completed."