fix(deploy): improve deployment robustness and reliability
All checks were successful
Test Runner / test-basic (push) Successful in 8s
Test Runner / test-php (push) Successful in 7s
Deploy Application / deploy (push) Successful in 1m35s

- Add docker volume prune to deploy.sh to prevent stale code issues
- Add automatic migrations and cache warmup to staging entrypoint
- Fix nginx race condition by waiting for PHP-FPM before starting
- Improve PHP healthcheck to use php-fpm-healthcheck
- Add curl to production nginx Dockerfile for healthchecks
- Add ensureSeedsTable() to SeedRepository for automatic table creation
- Update SeedCommand to ensure seeds table exists before operations

This prevents 502 Bad Gateway errors during deployment and ensures
fresh code is deployed without volume cache issues.
This commit is contained in:
2025-11-25 17:44:44 +01:00
parent 7785e65d08
commit 85e2360a90
11 changed files with 121 additions and 20 deletions

View File

@@ -170,11 +170,25 @@ services:
echo ""
echo " GIT_REPOSITORY_URL not set, using code from image"
fi
echo ""
echo "📊 Environment variables:"
env | grep -E "DB_|APP_" | grep -v "PASSWORD|KEY|SECRET" || true
# Run database migrations
if [ -f /var/www/html/console.php ]; then
echo ""
echo "🗄️ Running database migrations..."
cd /var/www/html
php console.php db:migrate --force || echo "⚠️ Migration warning (may be OK if already migrated)"
fi
# Warm up caches
echo ""
echo "🔥 Warming up caches..."
cd /var/www/html
php console.php cache:warm 2>/dev/null || echo " Cache warmup skipped (command may not exist)"
echo ""
echo "🛠️ Adjusting filesystem permissions..."
chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache 2>/dev/null || true
@@ -191,11 +205,13 @@ services:
echo "REDIS_PASSWORD_FILE: ${REDIS_PASSWORD_FILE:-NOT SET}"
exec php-fpm
healthcheck:
test: ["CMD-SHELL", "php-fpm-healthcheck || true"]
interval: 30s
timeout: 10s
# Use HTTP liveness check via php-fpm (not via nginx)
# This checks if the PHP application is actually responding
test: ["CMD-SHELL", "php-fpm-healthcheck || exit 1"]
interval: 15s
timeout: 5s
retries: 3
start_period: 40s
start_period: 60s
depends_on:
redis:
condition: service_started
@@ -265,6 +281,26 @@ services:
fi
done
# Wait for PHP-FPM to be ready before starting nginx
# This prevents 502 Bad Gateway errors during startup
echo "⏳ [staging-nginx] Waiting for PHP-FPM to be ready..."
MAX_WAIT=30
WAITED=0
while [ $$WAITED -lt $$MAX_WAIT ]; do
# Check if PHP-FPM is accepting connections on port 9000
if nc -z php 9000 2>/dev/null; then
echo "✅ [staging-nginx] PHP-FPM is ready on php:9000"
break
fi
echo " [staging-nginx] PHP-FPM not ready yet... ($$WAITED/$$MAX_WAIT)"
sleep 1
WAITED=$$((WAITED + 1))
done
if [ $$WAITED -ge $$MAX_WAIT ]; then
echo "⚠️ [staging-nginx] PHP-FPM did not become ready within $$MAX_WAIT seconds, starting anyway..."
fi
# Start nginx only (no PHP-FPM, no Git clone - php container handles that)
echo "🚀 [staging-nginx] Starting nginx..."
exec nginx -g "daemon off;"
@@ -280,11 +316,12 @@ services:
# Network
- "traefik.docker.network=traefik-public"
healthcheck:
test: ["CMD-SHELL", "curl -f http://127.0.0.1/health || exit 1"]
interval: 30s
timeout: 10s
# Use /health/live endpoint for lightweight liveness check
test: ["CMD-SHELL", "curl -sf http://127.0.0.1/health/live || exit 1"]
interval: 15s
timeout: 5s
retries: 3
start_period: 10s
start_period: 30s
depends_on:
php:
condition: service_started