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,9 +250,36 @@ 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..."
# 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" cd "$(dirname "$0")/terraform"
# Check if terraform.tfvars exists, if not copy from example # Check if terraform.tfvars exists, if not copy from example
@ -274,59 +292,18 @@ if [ "$PROVISION" = true ]; then
exit 0 exit 0
fi fi
terraform init # If we're building, ensure the latest image exists before deploying
terraform apply if [ "$BUILD" = true ]; then
LATEST_IMAGE_NAME="gcr.io/$PROJECT_ID/$IMAGE_NAME:latest"
cd - > /dev/null # Check if image exists
echo "Provisioning completed." if ! docker manifest inspect "$LATEST_IMAGE_NAME" > /dev/null 2>&1; then
fi echo "ERROR: Image $LATEST_IMAGE_NAME not found after build. Something went wrong."
exit 1
# Build and push Docker image fi
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
fi fi
# Deploy using Terraform echo "Applying Terraform configuration..."
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 init
terraform apply -auto-approve terraform apply -auto-approve