diff --git a/deployment/deploy.sh b/deployment/deploy.sh index e3e9d21..16b7274 100644 --- a/deployment/deploy.sh +++ b/deployment/deploy.sh @@ -20,14 +20,16 @@ fi 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 via Terraform" + echo " --build Build and push Docker image before deploying (use with --deploy)" echo " --destroy Destroy cloud resources with Terraform" echo " --list List all Cloud Run services in the project" - echo " --all Do all of the above (except destroy)" echo " --help Show this help message" echo "" + echo "Examples:" + echo " $0 --deploy # Deploy with existing Docker image" + echo " $0 --deploy --build # Build Docker image then deploy" + echo "" echo "Additional scripts:" echo " ./deployment/cleanup-images.sh Clean up container images (not managed by Terraform)" exit 0 @@ -39,7 +41,6 @@ if [ $# -eq 0 ]; then fi # Process arguments -PROVISION=false BUILD=false DEPLOY=false DESTROY=false @@ -47,10 +48,6 @@ LIST=false while [[ $# -gt 0 ]]; do case "$1" in - --provision) - PROVISION=true - shift - ;; --build) BUILD=true shift @@ -67,12 +64,6 @@ while [[ $# -gt 0 ]]; do LIST=true shift ;; - --all) - PROVISION=true - BUILD=true - DEPLOY=true - shift - ;; --help) show_help ;; @@ -259,9 +250,36 @@ if [ "$DESTROY" = true ]; then exit 0 fi -# Provision resources with Terraform -if [ "$PROVISION" = true ]; then - echo "Provisioning cloud resources with Terraform..." +# Deploy workflow +if [ "$DEPLOY" = true ]; then + echo "Starting deployment workflow..." + + # Step 1: Build and push Docker image if --build flag is provided + if [ "$BUILD" = true ]; then + echo "Building and pushing Docker image..." + + # Note: Docker authentication should be configured externally via: + # gcloud auth configure-docker gcr.io + # or by using service account key files + + # 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 + + # Step 2: Deploy to Cloud Run via Terraform + echo "Deploying to Cloud Run via Terraform..." + cd "$(dirname "$0")/terraform" # Check if terraform.tfvars exists, if not copy from example @@ -274,59 +292,18 @@ if [ "$PROVISION" = true ]; then 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..." - - # Note: Docker authentication should be configured externally via: - # gcloud auth configure-docker gcr.io - # or by using service account key files - - # 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 + # If we're building, ensure the latest image exists before deploying + if [ "$BUILD" = true ]; then + 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 after build. Something went wrong." + exit 1 + fi 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..." + echo "Applying Terraform configuration..." terraform init terraform apply -auto-approve