cinema-studio-pro/deploy-optical.sh
Vadym Samoilenko 59e71b0bcc fix: safer deploy — build first, up only on success, --no-cache
Old: `up -d --build` stops old container before building → if build fails, service is down
New:
  1. `build --no-cache` — full rebuild, fails without touching running container
  2. `up -d --force-recreate` — only runs if build succeeded
  3. health check after start to catch silent failures

--no-cache ensures PHP/JS file changes are always picked up (no stale layer cache)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-24 18:16:33 +01:00

96 lines
3.8 KiB
Bash
Executable file

#!/bin/bash
################################################################################
# Lux Studio — Optical-Prod Deployment Script
#
# Usage:
# cd /opt/cinema-studio-pro
# ./deploy-optical.sh
#
# Target: https://optical-prod.oliver.solutions/lux-studio/
#
# What it does:
# 1. git pull (plaiground branch)
# 2. docker compose build + up
# 3. Adds Apache include once (idempotent on re-deploy)
# 4. Reloads Apache
################################################################################
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
APACHE_VHOST="/etc/apache2/sites-enabled/optical-prod.oliver.solutions.conf"
APACHE_INCLUDE="Include /opt/cinema-studio-pro/deploy/apache-lux-studio.conf"
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; BLUE='\033[0;34m'; NC='\033[0m'
step() { echo ""; echo -e "${YELLOW}[$1] $2...${NC}"; }
ok() { echo -e "${GREEN}$1${NC}"; }
warn() { echo -e "${YELLOW}$1${NC}"; }
fail() { echo -e "${RED}$1${NC}"; exit 1; }
echo ""
echo -e "${BLUE}================================================"
echo -e " Lux Studio — optical-prod deploy"
echo -e "================================================${NC}"
echo " Dir: $SCRIPT_DIR"
echo " URL: https://optical-prod.oliver.solutions/lux-studio/"
echo ""
# ── 1. git pull ───────────────────────────────────────────────────────────────
step "1/4" "git pull"
cd "$SCRIPT_DIR"
git pull origin plaiground
ok "Code up to date ($(git rev-parse --short HEAD))"
# ── 2. Docker build ───────────────────────────────────────────────────────────
step "2/4" "Building image (no-cache)"
[ -f "backend/.env.optical" ] || fail "backend/.env.optical not found"
# Build first — if build fails the running container stays up (service never goes down)
docker compose -f docker-compose.prod.yml build --no-cache
ok "Image built"
# Only replace running container after successful build
docker compose -f docker-compose.prod.yml up -d --force-recreate
ok "Container up"
# Brief health check
sleep 3
if docker compose -f docker-compose.prod.yml ps | grep -q "Up"; then
ok "Container is running"
else
fail "Container failed to start — check: docker compose -f docker-compose.prod.yml logs"
fi
# ── 3. Apache include (idempotent) ────────────────────────────────────────────
step "3/4" "Apache include"
if grep -qF "$APACHE_INCLUDE" "$APACHE_VHOST" 2>/dev/null; then
ok "Include already present — skipping"
else
if [ -f "$APACHE_VHOST" ]; then
# Insert include before closing </VirtualHost>
sudo sed -i "s|</VirtualHost>| $APACHE_INCLUDE\n</VirtualHost>|" "$APACHE_VHOST"
ok "Include added to $APACHE_VHOST"
else
warn "$APACHE_VHOST not found — add manually:"
warn " $APACHE_INCLUDE"
fi
fi
# ── 4. Reload Apache ──────────────────────────────────────────────────────────
step "4/4" "Reloading Apache"
sudo apache2ctl configtest 2>&1 | grep -v "^$" || true
sudo systemctl reload apache2
ok "Apache reloaded"
echo ""
echo -e "${GREEN} Deploy complete!${NC}"
echo -e " → https://optical-prod.oliver.solutions/lux-studio/"
echo ""
echo -e "${BLUE} Useful commands:${NC}"
echo " Logs: docker compose -f docker-compose.prod.yml logs -f"
echo " Restart: docker compose -f docker-compose.prod.yml restart"
echo " Re-deploy: cd $SCRIPT_DIR && ./deploy-optical.sh"
echo ""