-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·107 lines (87 loc) · 4.94 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·107 lines (87 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env bash
set -euo pipefail
# ── Flags ──────────────────────────────────────────────────────────────────────
# --build Force a Docker image rebuild (use after Dockerfile changes)
# --fresh Drop and re-run all migrations (destructive — prompts for confirmation)
# --seed Run seeders after migrations
BUILD=false
FRESH=false
SEED=false
for arg in "$@"; do
case $arg in
--build) BUILD=true ;;
--fresh) FRESH=true ;;
--seed) SEED=true ;;
esac
done
COMPOSE="docker compose -f docker-compose.prod.yml"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting deploy..."
# ── Maintenance mode ───────────────────────────────────────────────────────────
if $COMPOSE ps --status running 2>/dev/null | grep -q 'eonmap_app'; then
echo "==> Enabling maintenance mode..."
$COMPOSE exec -T app php artisan down --retry=30 || true
fi
# ── Code ───────────────────────────────────────────────────────────────────────
echo "==> Pulling latest code..."
git pull origin "$(git branch --show-current)"
# ── Docker ─────────────────────────────────────────────────────────────────────
if [ "$BUILD" = true ]; then
echo "==> Rebuilding Docker images..."
$COMPOSE build --no-cache
fi
echo "==> Starting containers..."
$COMPOSE up -d
echo "==> Restarting app container to clear OPcache..."
$COMPOSE restart app
# ── Wait for MySQL ─────────────────────────────────────────────────────────────
echo "==> Waiting for MySQL..."
until $COMPOSE exec -T mysql mysqladmin ping -h localhost --silent 2>/dev/null; do
sleep 2
done
echo " MySQL ready."
# ── Dependencies & assets ──────────────────────────────────────────────────────
echo "==> Installing Composer dependencies..."
$COMPOSE exec -T app composer install --no-dev --optimize-autoloader --no-interaction
echo "==> Building frontend assets..."
$COMPOSE exec -T app npm ci
$COMPOSE exec -T app npm run build
# ── Database ───────────────────────────────────────────────────────────────────
if [ "$FRESH" = true ]; then
echo "WARNING: Fresh migration will drop all tables."
read -p "Are you sure? (yes/no): " confirm
if [ "$confirm" = "yes" ]; then
$COMPOSE exec -T app php artisan migrate:fresh --force
else
echo "Skipping fresh migration."
fi
else
echo "==> Running migrations..."
$COMPOSE exec -T app php artisan migrate --force
fi
if [ "$SEED" = true ]; then
echo "==> Running seeders..."
$COMPOSE exec -T app php artisan db:seed --force
fi
# ── Framework caches ───────────────────────────────────────────────────────────
echo "==> Caching config, routes, views, and events..."
$COMPOSE exec -T app php artisan config:cache
$COMPOSE exec -T app php artisan route:cache
$COMPOSE exec -T app php artisan view:cache
$COMPOSE exec -T app php artisan event:cache
# ── Storage ────────────────────────────────────────────────────────────────────
echo "==> Setting permissions..."
$COMPOSE exec -T app chown -R www-data:www-data storage bootstrap/cache
$COMPOSE exec -T app chmod -R 775 storage bootstrap/cache
# ── Sitemap ────────────────────────────────────────────────────────────────────
echo "==> Regenerating sitemap..."
$COMPOSE exec -T app php artisan sitemap:generate
# ── Done ───────────────────────────────────────────────────────────────────────
echo "==> Disabling maintenance mode..."
$COMPOSE exec -T app php artisan up
COMMIT=$(git rev-parse --short HEAD)
echo ""
echo "========================================="
echo "Deploy complete!"
echo "Time: $(date '+%Y-%m-%d %H:%M:%S')"
echo "Commit: $COMMIT"
echo "========================================="