#!/bin/bash # Runs on the server: pulls, installs, migrates, and restarts the WS server and worker. set -e SELF="$0" APP=(docker compose exec -T app) echo "==> Starting deploy script..." # Re-run the pulled version of this script so changes to it apply right away. if [ "${1:-}" != "--after-pull" ]; then git pull --rebase --stat exec "$SELF" --after-pull fi if [ ! -f .env ]; then echo "==> .env missing — creating it from .env.example with generated secrets" cp .env.example .env sed -i \ -e "s|^DB_PASSWORD=.*|DB_PASSWORD=$(openssl rand -hex 24)|" \ -e "s|^PUSHER_APP_SECRET=.*|PUSHER_APP_SECRET=$(openssl rand -hex 24)|" .env fi mkdir -p docker-data/mysql storage/framework/cache storage/framework/views storage/logs bootstrap/cache echo "==> Starting containers..." docker compose up -d echo "==> Waiting for MySQL..." for _ in $(seq 1 60); do [ "$(docker inspect -f '{{.State.Health.Status}}' "$(docker compose ps -q mysql)")" = healthy ] && break sleep 2 done echo "==> Installing composer dependencies..." "${APP[@]}" bash -c "COMPOSER_HOME=/tmp/composer composer install --no-interaction --prefer-dist --optimize-autoloader --no-dev" if ! grep -q '^APP_KEY=base64:' .env; then "${APP[@]}" php artisan key:generate --force # The running container read .env at start; recreate it so it sees the key. docker compose up -d --force-recreate app fi "${APP[@]}" chown -R www-data:www-data storage bootstrap/cache # The worker still runs the old code: stop it before the seeder schedules events it may # not know yet (it gives up on an event after three failures). Restarted below. "${APP[@]}" supervisorctl stop game-events || true echo "==> Migrating..." "${APP[@]}" php artisan migrate --force "${APP[@]}" php artisan db:seed --force "${APP[@]}" php artisan optimize:clear # Not `optimize`: there are no views to cache in an API-only app. "${APP[@]}" php artisan config:cache "${APP[@]}" php artisan route:cache "${APP[@]}" php artisan event:cache # Both daemons cache code at boot. echo "==> Restarting WS server and game:events worker..." "${APP[@]}" supervisorctl reread "${APP[@]}" supervisorctl update "${APP[@]}" supervisorctl restart websocket game-events echo "==> Deployment complete!"