This commit is contained in:
johnpccd 2025-05-24 18:49:51 +02:00
parent fe997f48d9
commit 68da24b20b

View File

@ -20,14 +20,16 @@ fi
function show_help { function show_help {
echo "Usage: $0 [options]" echo "Usage: $0 [options]"
echo "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 " --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 " --destroy Destroy cloud resources with Terraform"
echo " --list List all Cloud Run services in the project" 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 " --help Show this help message"
echo "" 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 "Additional scripts:"
echo " ./deployment/cleanup-images.sh Clean up container images (not managed by Terraform)" echo " ./deployment/cleanup-images.sh Clean up container images (not managed by Terraform)"
exit 0 exit 0
@ -39,7 +41,6 @@ if [ $# -eq 0 ]; then
fi fi
# Process arguments # Process arguments
PROVISION=false
BUILD=false BUILD=false
DEPLOY=false DEPLOY=false
DESTROY=false DESTROY=false
@ -47,10 +48,6 @@ LIST=false
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case "$1" in case "$1" in
--provision)
PROVISION=true
shift
;;
--build) --build)
BUILD=true BUILD=true
shift shift
@ -67,12 +64,6 @@ while [[ $# -gt 0 ]]; do
LIST=true LIST=true
shift shift
;; ;;
--all)
PROVISION=true
BUILD=true
DEPLOY=true
shift
;;
--help) --help)
show_help show_help
;; ;;
@ -259,29 +250,11 @@ if [ "$DESTROY" = true ]; then
exit 0 exit 0
fi fi
# Provision resources with Terraform # Deploy workflow
if [ "$PROVISION" = true ]; then if [ "$DEPLOY" = true ]; then
echo "Provisioning cloud resources with Terraform..." echo "Starting deployment workflow..."
cd "$(dirname "$0")/terraform"
# Check if terraform.tfvars exists, if not copy from example # Step 1: Build and push Docker image if --build flag is provided
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 if [ "$BUILD" = true ]; then
echo "Building and pushing Docker image..." echo "Building and pushing Docker image..."
@ -304,29 +277,33 @@ if [ "$BUILD" = true ]; then
echo "Image built and pushed successfully." echo "Image built and pushed successfully."
fi fi
# Deploy to Cloud Run # Step 2: Deploy to Cloud Run via Terraform
if [ "$DEPLOY" = true ]; then
echo "Deploying to Cloud Run via Terraform..." echo "Deploying to Cloud Run via Terraform..."
# Ensure the latest image exists 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
# 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" LATEST_IMAGE_NAME="gcr.io/$PROJECT_ID/$IMAGE_NAME:latest"
# Check if image exists # Check if image exists
if ! docker manifest inspect "$LATEST_IMAGE_NAME" > /dev/null 2>&1; then if ! docker manifest inspect "$LATEST_IMAGE_NAME" > /dev/null 2>&1; then
echo "ERROR: Image $LATEST_IMAGE_NAME not found. Please run --build first." echo "ERROR: Image $LATEST_IMAGE_NAME not found after build. Something went wrong."
exit 1 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 fi
echo "Applying Terraform configuration to deploy Cloud Run service..." echo "Applying Terraform configuration..."
terraform init terraform init
terraform apply -auto-approve terraform apply -auto-approve