set('hostname', '217.154.36.101') ->set('remote_user', 'absdev') ->set('branch', 'main') ->set('deploy_path', '/opt/laravel-sites/absdev-net/deploy') ->set('labels', ['stage' => 'production']); /* |-------------------------------------------------------------------------- | System 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 { $script = <<<'SH' set -euo pipefail export NVM_DIR="$HOME/.nvm" if [ ! -s "$NVM_DIR/nvm.sh" ]; then echo "ERROR: NVM installation missing" exit 1 fi . "$NVM_DIR/nvm.sh" nvm use 26 cd {{release_path}} echo "Node: $(node --version)" echo "npm: $(npm --version)" if [ ! -f package.json ]; then echo "ERROR: package.json missing" exit 1 fi # Lockfile is deliberately excluded from Git. # Generate it within this deployment release. if [ ! -f package-lock.json ]; then npm install --package-lock-only --ignore-scripts --no-audit --no-fund fi npm ci --no-audit --no-fund npm run build test -f public/build/manifest.json SH; run($script); 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()); $script = 'set -eu; ' .'dir='.$path.'/bootstrap/cache; ' .'mkdir -p "$dir"; ' .'owner=$(stat -c %u '.$path.'); ' .'chown -R "$owner":33 "$dir"; ' .'chmod -R ug+rwX "$dir"; ' .'chmod g+s "$dir"'; run( absdevCompose() .' exec -T -u root php sh -c ' .escapeshellarg($script) ); run( absdevCompose() .' exec -T -u www-data php sh -c ' .escapeshellarg( 'test -w '.$path.'/storage' .' && test -w '.$path.'/bootstrap/cache' ) ); writeln('Laravel permissions verified.'); }); /* |-------------------------------------------------------------------------- | 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 { $revision = get('release_revision'); if (empty($revision)) { throw new \RuntimeException( 'Deployer did not provide a Git revision.' ); } run( 'printf %s\\n ' .escapeshellarg($revision) .' > {{release_path}}/REVISION' ); run( 'date -u +%Y-%m-%dT%H:%M:%SZ' .' > {{release_path}}/DEPLOYED_AT' ); writeln( 'Release revision: ' .$revision .'' ); }); /* |-------------------------------------------------------------------------- | 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'); /* |-------------------------------------------------------------------------- | Repair Laravel cache ownership before release cleanup |-------------------------------------------------------------------------- | | Laravel writes cache files as www-data (UID 33). | Deployer deletes release directories as mag. | Keep mag as the directory owner and www-data as the group. | */ desc('Repair cache permissions across Deployer releases'); task('absdev:cache:repair', function (): void { $script = <<<'SH' set -eu for release in /var/www/deploy/releases/[0-9]*; do cache="$release/bootstrap/cache" [ -d "$cache" ] || continue owner=$(stat -c %u "$release") chown -R "$owner":33 "$cache" chmod -R u+rwX,g+rwX "$cache" find "$cache" -type d -exec chmod g+s {} + echo "Repaired: $cache" done SH; run( absdevCompose() .' exec -T -u root php sh -c ' .escapeshellarg($script) ); }); before('deploy:cleanup', 'absdev:cache:repair'); after('absdev:laravel:prepare', 'absdev:cache:repair');