Files
absdev_site/deploy.php
T
Mark Gregory 96b577f3f3
Deploy ABSDEV Production / Deploy Laravel to ABSDEV (push) Successful in 1m4s
style fixes
2026-11-20 14:07:57 +00:00

501 lines
12 KiB
PHP

<?php
namespace Deployer;
require 'recipe/laravel.php';
/*
|--------------------------------------------------------------------------
| 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', '[email protected]:mag/absdev_site.git');
// Keep the complete Git repository in each release.
set('update_code_strategy', 'clone');
set('keep_releases', 5);
set('allow_anonymous_stats', false);
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', '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('<info>Docker deployment preflight passed.</info>');
});
/*
|--------------------------------------------------------------------------
| 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('<info>Production Vite assets built.</info>');
});
/*
|--------------------------------------------------------------------------
| 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('<info>Laravel permissions verified.</info>');
});
/*
|--------------------------------------------------------------------------
| 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('<info>Laravel release prepared.</info>');
});
/*
|--------------------------------------------------------------------------
| 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(
'<info>Release revision: '
.$revision
.'</info>'
);
});
/*
|--------------------------------------------------------------------------
| 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('<info>Release files validated.</info>');
});
/*
|--------------------------------------------------------------------------
| 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('<info>Docker PHP-FPM restarted.</info>');
});
/*
|--------------------------------------------------------------------------
| 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('<info>Production HTTP health check passed.</info>');
});
/*
|--------------------------------------------------------------------------
| 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');