#!/bin/bash # Docker build script for all services set -e # Color codes for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Function to print colored output print_status() { echo -e "${GREEN}[BUILD]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } # Get the directory of this script SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" # Change to project root cd "$PROJECT_ROOT" # Check if Docker is installed if ! command -v docker &> /dev/null; then print_error "Docker is not installed. Please install Docker first." exit 1 fi # Default values TAG="latest" REGISTRY="" PUSH=false BUILD_PARALLEL=false # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in -t|--tag) TAG="$2" shift 2 ;; -r|--registry) REGISTRY="$2" shift 2 ;; -p|--push) PUSH=true shift ;; --parallel) BUILD_PARALLEL=true shift ;; -h|--help) echo "Usage: $0 [OPTIONS]" echo "Options:" echo " -t, --tag TAG Tag for the images (default: latest)" echo " -r, --registry URL Registry URL (e.g., ghcr.io/username)" echo " -p, --push Push images to registry after build" echo " --parallel Build images in parallel" echo " -h, --help Show this help message" exit 0 ;; *) print_error "Unknown option: $1" exit 1 ;; esac done # Services to build SERVICES=( "api-gateway" "orchestrator" "claude-agent" "gramjs-adapter" "safety-guard" "analytics" "compliance-guard" "ab-testing" "workflow" "webhook" "template" "i18n" "user-management" "scheduler" "logging" ) # Build function build_service() { local service=$1 local image_name="${service}" if [ -n "$REGISTRY" ]; then image_name="${REGISTRY}/${service}" fi print_status "Building ${service}..." if [ -f "services/${service}/Dockerfile" ]; then if docker build -t "${image_name}:${TAG}" -t "${image_name}:latest" "services/${service}"; then print_status "✓ ${service} built successfully" if [ "$PUSH" = true ] && [ -n "$REGISTRY" ]; then print_status "Pushing ${service} to registry..." docker push "${image_name}:${TAG}" docker push "${image_name}:latest" print_status "✓ ${service} pushed successfully" fi else print_error "Failed to build ${service}" return 1 fi else print_warning "Dockerfile not found for ${service}, skipping..." fi } # Build frontend build_frontend() { print_status "Building frontend..." local image_name="frontend" if [ -n "$REGISTRY" ]; then image_name="${REGISTRY}/frontend" fi if docker build -t "${image_name}:${TAG}" -t "${image_name}:latest" "frontend"; then print_status "✓ Frontend built successfully" if [ "$PUSH" = true ] && [ -n "$REGISTRY" ]; then print_status "Pushing frontend to registry..." docker push "${image_name}:${TAG}" docker push "${image_name}:latest" print_status "✓ Frontend pushed successfully" fi else print_error "Failed to build frontend" return 1 fi } # Main build process print_status "Starting Docker build process..." print_status "Tag: ${TAG}" if [ -n "$REGISTRY" ]; then print_status "Registry: ${REGISTRY}" fi # Build services if [ "$BUILD_PARALLEL" = true ]; then print_status "Building services in parallel..." # Build all services in parallel for service in "${SERVICES[@]}"; do build_service "$service" & done # Build frontend in parallel build_frontend & # Wait for all background jobs to complete wait else # Build services sequentially for service in "${SERVICES[@]}"; do build_service "$service" || exit 1 done # Build frontend build_frontend || exit 1 fi print_status "Docker build process completed!" # Summary echo "" print_status "Build Summary:" print_status "- Tag: ${TAG}" if [ -n "$REGISTRY" ]; then print_status "- Registry: ${REGISTRY}" fi print_status "- Services built: ${#SERVICES[@]} + frontend" if [ "$PUSH" = true ]; then print_status "- Images pushed to registry: ✓" fi