From 2029dc34b4584faeb7a94151a03f32957e780f6a Mon Sep 17 00:00:00 2001 From: Mark Gregory Date: Fri, 20 Nov 2026 09:40:10 +0000 Subject: [PATCH] new design with nice layout --- deploy.php | 471 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 391 insertions(+), 80 deletions(-) diff --git a/deploy.php b/deploy.php index 02463db..f17f268 100644 --- a/deploy.php +++ b/deploy.php @@ -4,104 +4,415 @@ namespace Deployer; require 'recipe/laravel.php'; -set('writable_use_sudo', false); -set('writable_mode', 'skip'); -set('keep_releases', 5); +/* +|-------------------------------------------------------------------------- +| ABSDEV — Laravel 13 / PHP 8.5 / Docker / PostgreSQL +|-------------------------------------------------------------------------- +| +| Deployment runs from macOS through SSH. +| Composer and Vite build on the production host. +| Artisan runs inside the website PHP container. +| +| IMPORTANT: +| The initial Docker release-aware cutover must be completed first. +| This recipe refuses to deploy while the original mounts are active. +| +*/ + set('repository', 'git@git.absdev.net:mag/absdev_site.git'); -set('node_version', '22.12.0'); + +set('keep_releases', 5); set('allow_anonymous_stats', false); -add('shared_files', ['.env']); -add('shared_dirs', ['storage']); -add('writable_dirs', ['storage', 'bootstrap/cache']); +set('writable_mode', 'skip'); +set('writable_use_sudo', false); +set('clear_use_sudo', false); +set('cleanup_use_sudo', false); + +set('shared_files', ['.env']); +set('shared_dirs', ['storage']); + +set('absdev_root', '/opt/laravel-sites/absdev-net'); +set('absdev_container_root', '/var/www/deploy'); host('prod') ->set('hostname', '217.154.36.101') ->set('remote_user', 'mag') ->set('branch', 'main') - ->set('deploy_path', '/opt/laravel-sites/absdev-net/deploy'); + ->set('deploy_path', '/opt/laravel-sites/absdev-net/deploy') + ->set('labels', ['stage' => 'production']); /* - * Install the root npm tree, then every module that owns its own lock file, - * then run ONE root Vite build. Because Vite is compiling a module source file, - * normal Node resolution finds that module's own node_modules first. - */ -task('build', function (): void { - $node = 'export NVM_DIR="$HOME/.nvm"' - .' && [ -s "$NVM_DIR/nvm.sh" ]' - .' && . "$NVM_DIR/nvm.sh"' - .' && nvm use {{node_version}}'; +|-------------------------------------------------------------------------- +| Helpers +|-------------------------------------------------------------------------- +*/ - run( - $node - .' && cd {{release_path}}' - .' && npm ci --no-audit --no-fund' - ); +function absdevCompose(): string +{ + $root = get('absdev_root'); - run( - $node - .' && cd {{release_path}}' - .' && for lock in Modules/*/package-lock.json; do' - .' [ -f "$lock" ] || continue;' - .' module_dir="$(dirname "$lock")";' - .' echo "[Modules] npm ci: $module_dir";' - .' (cd "$module_dir" && npm ci --no-audit --no-fund);' - .' done' - ); + return 'docker compose' + . ' -f ' . $root . '/compose.yaml' + . ' -f ' . $root . '/compose.deployer.yaml'; +} - run( - $node - .' && cd {{release_path}}' - .' && npm run build' - ); -}); +function absdevReleasePath(): string +{ + $hostPath = get('release_path'); + $deployPath = get('deploy_path'); + $containerRoot = get('absdev_container_root'); -task('queue:restart', function (): void { - run('cd {{release_path}} && php artisan queue:restart'); -}); - -task('deploy:write_revision_file', function (): void { - run('cd {{release_path}} && echo "{{branch}}" > BRANCH'); - run('cd {{release_path}} && date -u +"%Y-%m-%dT%H:%M:%SZ" > DEPLOYED_AT'); -}); - -task('artisan:clear', function (): void { - run('cd {{release_path}} && php artisan view:clear'); - run('cd {{release_path}} && php artisan optimize:clear'); - run('cd {{release_path}} && php artisan config:clear'); - run('cd {{release_path}} && php artisan route:clear'); -}); - -task('services:restart', function (): void { - // Validate and reload nginx. - run('sudo -n nginx -t'); - run('sudo -n systemctl reload nginx'); - - // Detect the PHP-FPM socket configured in nginx. - $socket = trim(run( - "sudo -n nginx -T 2>/dev/null" - . " | grep -oE 'php[0-9]+\\.[0-9]+-fpm\\.sock'" - . " | sort -Vu" - . " | tail -n 1" - )); - - if ($socket === '') { + if (!str_starts_with($hostPath, $deployPath . '/releases/')) { throw new \RuntimeException( - 'Unable to detect the PHP-FPM version from nginx configuration.' + 'Unexpected release path: ' . $hostPath ); } - // Convert php8.5-fpm.sock into php8.5-fpm. - $service = str_replace('.sock', '', $socket); - writeln("Detected PHP-FPM service: {$service}"); - // Restart the detected PHP-FPM service. - run("sudo -n systemctl restart {$service}"); - writeln("PHP-FPM restarted successfully."); + + 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.'); }); -after('deploy:update_code', 'deploy:write_revision_file'); -after('deploy:vendors', 'build'); -after('deploy:symlink', 'artisan:clear'); -after('deploy:symlink', 'queue:restart'); + +/* +|-------------------------------------------------------------------------- +| 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 { + run( + 'cd {{release_path}}' + . ' && npm ci --no-audit --no-fund' + . ' && npm run build' + ); + + run( + 'test -f {{release_path}}/public/build/manifest.json' + ); + + writeln('Production 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'); -after('deploy:symlink', 'services:restart'); + +// Rollback changes the symlink, so refresh PHP-FPM too. +after('rollback', 'absdev:php:restart'); +after('rollback', 'absdev:health');