170 lines
4.7 KiB
Bash
Executable File
170 lines
4.7 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# CopperTone.Tech Deployment Script
|
|
# Pulls latest from main, rebuilds, and restarts all containers
|
|
#
|
|
# Usage: ./scripts/deploy.sh [options]
|
|
# Options:
|
|
# --no-pull Skip git pull (useful for local testing)
|
|
# --service Only rebuild/restart specific service (e.g., --service frontend)
|
|
# --help Show this help message
|
|
#
|
|
|
|
set -e
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Script directory (for running from anywhere)
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
|
|
|
|
# Default options
|
|
DO_PULL=true
|
|
SPECIFIC_SERVICE=""
|
|
|
|
# Parse arguments
|
|
while [[ $# -gt 0 ]]; do
|
|
case $1 in
|
|
--no-pull)
|
|
DO_PULL=false
|
|
shift
|
|
;;
|
|
--service)
|
|
SPECIFIC_SERVICE="$2"
|
|
shift 2
|
|
;;
|
|
--help)
|
|
head -14 "$0" | tail -10
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo -e "${RED}Unknown option: $1${NC}"
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
cd "$PROJECT_DIR"
|
|
|
|
echo -e "${BLUE}======================================${NC}"
|
|
echo -e "${BLUE} CopperTone.Tech Deployment Script ${NC}"
|
|
echo -e "${BLUE}======================================${NC}"
|
|
echo ""
|
|
|
|
# Step 1: Git Pull (if enabled)
|
|
if [ "$DO_PULL" = true ]; then
|
|
echo -e "${YELLOW}[1/4] Pulling latest changes from main...${NC}"
|
|
|
|
# Ensure we're on main branch
|
|
CURRENT_BRANCH=$(git branch --show-current)
|
|
if [ "$CURRENT_BRANCH" != "main" ]; then
|
|
echo -e "${YELLOW} Switching from $CURRENT_BRANCH to main...${NC}"
|
|
git checkout main
|
|
fi
|
|
|
|
# Pull latest
|
|
git pull origin main
|
|
echo -e "${GREEN} ✓ Git pull complete${NC}"
|
|
else
|
|
echo -e "${YELLOW}[1/4] Skipping git pull (--no-pull)${NC}"
|
|
fi
|
|
echo ""
|
|
|
|
# Step 2: Build containers
|
|
echo -e "${YELLOW}[2/4] Building containers...${NC}"
|
|
if [ -n "$SPECIFIC_SERVICE" ]; then
|
|
echo -e "${YELLOW} Building only: $SPECIFIC_SERVICE${NC}"
|
|
podman-compose build "$SPECIFIC_SERVICE"
|
|
else
|
|
podman-compose build
|
|
fi
|
|
echo -e "${GREEN} ✓ Build complete${NC}"
|
|
echo ""
|
|
|
|
# Step 3: Stop and remove old containers
|
|
echo -e "${YELLOW}[3/4] Stopping and removing old containers...${NC}"
|
|
if [ -n "$SPECIFIC_SERVICE" ]; then
|
|
podman-compose stop "$SPECIFIC_SERVICE" 2>/dev/null || true
|
|
podman-compose rm -f "$SPECIFIC_SERVICE" 2>/dev/null || true
|
|
else
|
|
podman-compose down
|
|
fi
|
|
echo -e "${GREEN} ✓ Old containers removed${NC}"
|
|
echo ""
|
|
|
|
# Step 4: Start new containers
|
|
echo -e "${YELLOW}[4/4] Starting new containers...${NC}"
|
|
if [ -n "$SPECIFIC_SERVICE" ]; then
|
|
podman-compose up -d "$SPECIFIC_SERVICE"
|
|
else
|
|
podman-compose up -d
|
|
fi
|
|
echo -e "${GREEN} ✓ Containers started${NC}"
|
|
echo ""
|
|
|
|
# Wait for services to be ready
|
|
echo -e "${YELLOW}Waiting for services to initialize...${NC}"
|
|
sleep 5
|
|
|
|
# Reload nginx to pick up any config changes
|
|
echo -e "${YELLOW}Reloading nginx...${NC}"
|
|
if /docker/www/scripts/nginx-reload.sh 2>/dev/null; then
|
|
echo -e "${GREEN} ✓ Nginx reloaded${NC}"
|
|
else
|
|
echo -e "${YELLOW} ! Nginx reload failed (non-fatal)${NC}"
|
|
fi
|
|
echo ""
|
|
|
|
# Health check
|
|
echo -e "${YELLOW}Running health checks...${NC}"
|
|
echo ""
|
|
|
|
check_service() {
|
|
local name=$1
|
|
local port=$2
|
|
local endpoint=${3:-"/health"}
|
|
|
|
if curl -s --connect-timeout 3 "http://127.0.0.1:$port$endpoint" > /dev/null 2>&1; then
|
|
echo -e " ${GREEN}✓${NC} $name (port $port)"
|
|
return 0
|
|
else
|
|
# Some services return 404 for /health but are still running
|
|
if curl -s --connect-timeout 3 "http://127.0.0.1:$port/" > /dev/null 2>&1; then
|
|
echo -e " ${GREEN}✓${NC} $name (port $port)"
|
|
return 0
|
|
else
|
|
echo -e " ${RED}✗${NC} $name (port $port) - not responding"
|
|
return 1
|
|
fi
|
|
fi
|
|
}
|
|
|
|
FAILED=0
|
|
check_service "Frontend" 8090 "/" || FAILED=1
|
|
check_service "Auth Service" 8082 "/" || FAILED=1
|
|
check_service "Blog Service" 8085 "/health" || FAILED=1
|
|
check_service "Work Management" 8083 "/" || FAILED=1
|
|
check_service "Payment Service" 8084 "/" || FAILED=1
|
|
|
|
echo ""
|
|
|
|
if [ $FAILED -eq 0 ]; then
|
|
echo -e "${GREEN}======================================${NC}"
|
|
echo -e "${GREEN} Deployment completed successfully! ${NC}"
|
|
echo -e "${GREEN}======================================${NC}"
|
|
else
|
|
echo -e "${YELLOW}======================================${NC}"
|
|
echo -e "${YELLOW} Deployment complete with warnings ${NC}"
|
|
echo -e "${YELLOW} Some services may need more time ${NC}"
|
|
echo -e "${YELLOW}======================================${NC}"
|
|
fi
|
|
|
|
echo ""
|
|
echo -e "${BLUE}Container Status:${NC}"
|
|
podman ps --filter "label=io.podman.compose.project=coppertonetech" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
|