set('hostname', '217.154.36.101') ->set('remote_user', 'mag') ->set('branch', 'main') ->set('deploy_path', '/opt/laravel-sites/absdev-net/deploy') ->set('labels', ['stage' => 'production']); /* |-------------------------------------------------------------------------- | Helpers |-------------------------------------------------------------------------- */ function absdevCompose(): string { $root = get('absdev_root'); return 'docker compose' . ' -f ' . $root . '/compose.yaml' . ' -f ' . $root . '/compose.deployer.yaml'; } function absdevReleasePath(): string { $hostPath = get('release_path'); $deployPath = get('deploy_path'); $containerRoot = get('absdev_container_root'); if (!str_starts_with($hostPath, $deployPath . '/releases/')) { throw new \RuntimeException( 'Unexpected release path: ' . $hostPath ); } return $containerRoot . substr($hostPath, strlen($deployPath)); } function absdevArtisan(string $command): void { $path = escapeshellarg(absdevReleasePath()); run( absdevCompose() . ' exec -T -u www-data php' . ' sh -c ' . escapeshellarg( 'cd ' . $path . ' && php artisan ' . $command ) ); } /* |-------------------------------------------------------------------------- | Production preflight |-------------------------------------------------------------------------- */ desc('Verify release-aware Docker deployment is active'); task('absdev:preflight', function (): void { $root = get('absdev_root'); $deploy = get('deploy_path'); $compose = absdevCompose(); run('test -f ' . $root . '/compose.deployer.yaml'); run('test -f ' . $root . '/nginx.deployer.conf'); run('test -L ' . $deploy . '/current'); run('test -f ' . $deploy . '/shared/.env'); run('test -d ' . $deploy . '/shared/storage'); run($compose . ' config --quiet'); foreach (['php', 'web'] as $service) { $check = 'test -f /var/www/deploy/current/public/index.php'; run( $compose . ' exec -T ' . $service . ' sh -c ' . escapeshellarg($check) ); } // Ensure the PHP container is using the release tree. run( $compose . ' exec -T php sh -c ' . escapeshellarg( 'test -f /var/www/deploy/current/artisan' ) ); // Ensure Nginx is serving the release-aware document root. run( $compose . ' exec -T web nginx -T 2>&1' . ' | grep -Fq "root /var/www/deploy/current/public;"' ); writeln('Docker deployment preflight passed.'); }); /* |-------------------------------------------------------------------------- | Composer |-------------------------------------------------------------------------- | | Host PHP 8.5 and Composer are available. | Avoid Composer scripts running Artisan outside Docker. | */ desc('Install production Composer dependencies'); task('absdev:composer', function (): void { run( 'cd {{release_path}}' . ' && composer install' . ' --no-dev' . ' --prefer-dist' . ' --no-interaction' . ' --no-progress' . ' --optimize-autoloader' . ' --no-scripts' ); }); /* |-------------------------------------------------------------------------- | Vite |-------------------------------------------------------------------------- */ desc('Build production Vite assets'); task('absdev:build', function (): void { /* * Deployer uses a non-interactive SSH shell. * Explicitly load NVM so Node and npm are available. */ $node = <<<'SH' export NVM_DIR="$HOME/.nvm" if [ -s "$NVM_DIR/nvm.sh" ]; then . "$NVM_DIR/nvm.sh" nvm use 26 fi if ! command -v node >/dev/null 2>&1; then echo "ERROR: Node.js is not available in the deployment shell" exit 1 fi if ! command -v npm >/dev/null 2>&1; then echo "ERROR: npm is not available in the deployment shell" exit 1 fi node --version npm --version SH; run( $node . "\ncd {{release_path}}" . "\nnpm ci --no-audit --no-fund" . "\nnpm run build" ); run( 'test -f {{release_path}}/public/build/manifest.json' ); writeln('Production Vite assets built.'); }); /* |-------------------------------------------------------------------------- | Release permissions |-------------------------------------------------------------------------- | | Only bootstrap/cache needs release-local write access. | Shared storage permissions are prepared during first cutover. | */ desc('Prepare Laravel writable directories'); task('absdev:permissions', function (): void { $path = escapeshellarg(absdevReleasePath()); run( absdevCompose() . ' exec -T -u root php sh -c ' . escapeshellarg( 'mkdir -p ' . $path . '/bootstrap/cache' . ' && chown -R www-data:www-data ' . $path . '/bootstrap/cache' . ' && chmod -R u+rwX,g+rwX ' . $path . '/bootstrap/cache' ) ); run( absdevCompose() . ' exec -T -u www-data php sh -c ' . escapeshellarg( 'test -w ' . $path . '/storage' . ' && test -w ' . $path . '/bootstrap/cache' ) ); }); /* |-------------------------------------------------------------------------- | Laravel preparation |-------------------------------------------------------------------------- */ desc('Prepare Laravel release inside PHP 8.5 container'); task('absdev:laravel:prepare', function (): void { absdevArtisan('package:discover --ansi'); absdevArtisan('config:clear'); absdevArtisan('view:clear'); absdevArtisan('route:clear'); absdevArtisan('event:clear'); absdevArtisan('config:cache'); writeln('Laravel release prepared.'); }); /* |-------------------------------------------------------------------------- | Revision information |-------------------------------------------------------------------------- */ desc('Write Git revision metadata'); task('absdev:revision', function (): void { run( 'cd {{release_path}}' . ' && git rev-parse HEAD > REVISION' . ' && date -u +%Y-%m-%dT%H:%M:%SZ > DEPLOYED_AT' ); }); /* |-------------------------------------------------------------------------- | Release validation |-------------------------------------------------------------------------- */ desc('Validate release before publication'); task('absdev:validate', function (): void { run('test -f {{release_path}}/artisan'); run('test -f {{release_path}}/vendor/autoload.php'); run('test -f {{release_path}}/public/build/manifest.json'); run('test -L {{release_path}}/.env'); run('test -L {{release_path}}/storage'); writeln('Release files validated.'); }); /* |-------------------------------------------------------------------------- | Database migrations |-------------------------------------------------------------------------- | | Explicit command, not automatic during deployment. | | Usage: | dep absdev:migrate prod | */ desc('Run production database migrations'); task('absdev:migrate', function (): void { run( absdevCompose() . ' exec -T -u www-data php' . ' php artisan migrate --force' ); }); /* |-------------------------------------------------------------------------- | PHP-FPM |-------------------------------------------------------------------------- | | Restart the website PHP container only. | PostgreSQL and the Nginx container are not restarted. | */ desc('Restart Docker PHP-FPM to clear OPcache'); task('absdev:php:restart', function (): void { run(absdevCompose() . ' restart php'); writeln('Docker PHP-FPM restarted.'); }); /* |-------------------------------------------------------------------------- | Post-deployment |-------------------------------------------------------------------------- */ desc('Refresh Laravel caches and workers'); task('absdev:refresh', function (): void { $compose = absdevCompose(); run( $compose . ' exec -T -u www-data php' . ' php artisan view:clear' ); run( $compose . ' exec -T -u www-data php' . ' php artisan queue:restart' ); }); /* |-------------------------------------------------------------------------- | Health check |-------------------------------------------------------------------------- | | Check the origin directly to avoid Cloudflare HTML cache. | */ desc('Check production HTTP response'); task('absdev:health', function (): void { run( 'curl --fail --silent --show-error' . ' --max-time 20' . ' -H ' . escapeshellarg('Host: absdev.net') . ' -H ' . escapeshellarg('Cache-Control: no-cache') . ' -o /dev/null' . ' http://127.0.0.1:8101/' ); writeln('Production HTTP health check passed.'); }); /* |-------------------------------------------------------------------------- | Deployment sequence |-------------------------------------------------------------------------- | | Do not use Laravel's default deploy task. | It runs host Artisan and automatic migrations. | */ desc('Deploy ABSDEV to production'); task('deploy', [ 'absdev:preflight', 'deploy:setup', 'deploy:lock', 'deploy:release', 'deploy:update_code', 'deploy:shared', 'absdev:composer', 'absdev:build', 'absdev:permissions', 'absdev:laravel:prepare', 'absdev:revision', 'absdev:validate', 'deploy:symlink', 'absdev:php:restart', 'absdev:refresh', 'absdev:health', 'deploy:unlock', 'deploy:cleanup', 'deploy:success', ]); after('deploy:failed', 'deploy:unlock'); // Rollback changes the symlink, so refresh PHP-FPM too. after('rollback', 'absdev:php:restart'); after('rollback', 'absdev:health');