Back to Documentation

5-build.sh

Source Code / Script
Modified: 1/4/2026
Size: 4.6 KB
#!/bin/bash

# AudivaMax Build & Deployment Pipeline
# Automates dependency installation, monorepo build via Turbo, and PM2 service restart
# with comprehensive checks, logging, and error handling for production environments

set -Ee

# --- CONSTANTS ---
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(cd -- "$SCRIPT_DIR/.." && pwd)"
LOG_FILE="${ROOT_DIR}/.build-$(date +'%Y%m%d-%H%M%S').log"
START_TIME=$(date +%s)

# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

# --- FUNCTIONS ---
log() {
    local msg="$1"
    local timestamp=$(date +'%Y-%m-%d %H:%M:%S')
    echo -e "${CYAN}[${timestamp}]${NC} ${msg}" | tee -a "$LOG_FILE"
}

error() {
    local line=$1
    local msg="${2:-Build failed}"
    echo -e "${RED}✘ Error at line $line: ${msg}${NC}" | tee -a "$LOG_FILE"
    elapsed_time
    exit 1
}

warn() {
    echo -e "${YELLOW}⚠ $1${NC}" | tee -a "$LOG_FILE"
}

success() {
    echo -e "${GREEN}✓ $1${NC}" | tee -a "$LOG_FILE"
}

section() {
    echo -e "\n${BLUE}=== $1 ===${NC}" | tee -a "$LOG_FILE"
}

elapsed_time() {
    local end_time=$(date +%s)
    local duration=$((end_time - START_TIME))
    local minutes=$((duration / 60))
    local seconds=$((duration % 60))
    echo -e "${CYAN}Elapsed time: ${minutes}m ${seconds}s${NC}" | tee -a "$LOG_FILE"
}

check_command() {
    local cmd=$1
    local install_hint=$2
    if ! command -v "$cmd" >/dev/null 2>&1; then
        error "$LINENO" "$cmd not found. $install_hint"
    fi
}

check_file() {
    local file=$1
    local desc=$2
    if [ ! -f "$file" ]; then
        error "$LINENO" "$desc not found: $file"
    fi
}

check_space() {
    local required_mb=500
    local available=$(df "$ROOT_DIR" | awk 'NR==2 {print int($4/1024)}')
    if [ "$available" -lt "$required_mb" ]; then
        error "$LINENO" "Insufficient disk space. Required: ${required_mb}MB, Available: ${available}MB"
    fi
    success "Disk space check passed (${available}MB available)"
}

trap 'error $LINENO' ERR
cd "$ROOT_DIR" || error "$LINENO" "Failed to cd to $ROOT_DIR"

# --- HEADER ---
echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -e "     AudivaMax Build & Deployment Pipeline"
echo -e "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
log "Starting build in: $ROOT_DIR"
log "Log file: $LOG_FILE"

# --- PREFLIGHT CHECKS ---
section "Preflight Checks"
check_command "node" "Install Node.js"
check_command "pnpm" "Run: npm install -g pnpm"
check_command "pm2" "Run: npm install -g pm2"
check_file "package.json" "Root package.json"
check_file "ecosystem.config.js" "PM2 ecosystem config"
check_file "pnpm-workspace.yaml" "Workspace configuration"
if [ ! -f "node_modules/.bin/turbo" ]; then
    warn "Turbo not found in node_modules (will be installed during pnpm install)"
fi
check_space
success "All preflight checks passed"

# --- INSTALL DEPENDENCIES ---
section "Installing Dependencies"
log "Running: pnpm install"
pnpm install --frozen-lockfile 2>&1 | tee -a "$LOG_FILE"
success "Dependencies installed"

# --- BUILD MONOREPO ---
section "Building Packages & Applications"
log "Running: pnpm build (via turbo)"
pnpm build 2>&1 | tee -a "$LOG_FILE"
success "Build completed successfully"

# --- RESTART SERVICES ---
section "Restarting Services"
log "Reloading PM2 applications..."

if pm2 reload ecosystem.config.js --update-env 2>&1 | tee -a "$LOG_FILE"; then
    success "Services reloaded"
else
    warn "Reload failed, attempting fresh start..."
    pm2 start ecosystem.config.js 2>&1 | tee -a "$LOG_FILE"
    success "Services started"
fi

# --- HEALTH CHECK ---
section "Service Health Check"
sleep 3
log "Checking service status..."
pm2 status | tee -a "$LOG_FILE"

# Verify critical services are running
for service in "audivamax-api" "audivamax-web" "audivamax-admin"; do
    status=$(pm2 status "$service" 2>/dev/null | grep -E "online|errored" | tail -1)
    if echo "$status" | grep -q "online"; then
        success "$service is online"
    else
        warn "$service status: $status"
    fi
done

# --- COMPLETION ---
elapsed_time
echo -e "\n${GREEN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo -e "     ✓ Build & Deployment Completed!"
echo -e "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
log "Full log saved to: $LOG_FILE"

This is a read-only view of 5-build.sh. Changes must be made in the source repository.