Last active
February 13, 2024 16:39
-
-
Save nickdenardis/c1e2e9f880b198c157ce6bf80273cdbf to your computer and use it in GitHub Desktop.
Zero downtime local build Laravel 5 deploys with Deployer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Deployer; | |
require 'recipe/laravel.php'; | |
require 'vendor/deployer/recipes/local.php'; | |
require 'vendor/deployer/recipes/rsync.php'; | |
require 'vendor/deployer/recipes/npm.php'; | |
// Configuration | |
set('ssh_type', 'native'); | |
set('ssh_multiplexing', true); | |
set('writable_mode', 'chmod'); | |
set('default_stage', 'dev'); | |
set('repository', '[email protected]:username/repository.git'); | |
add('shared_files', []); | |
add('shared_dirs', []); | |
add('writable_dirs', []); | |
add('rsync', [ | |
'exclude' => [ | |
'.git', | |
'deploy.php', | |
'node_modules', | |
], | |
]); | |
// RSYNC files from /tmp/deployer instead of vendor/deployer/recipes/ | |
set('rsync_src', function() { | |
$local_src = get('local_release_path'); | |
if(is_callable($local_src)){ | |
$local_src = $local_src(); | |
} | |
return $local_src; | |
}); | |
// Servers | |
server('production', 'domain.com') | |
->user('username') | |
->identityFile() | |
->set('branch', 'master') | |
->set('deploy_path', '/var/www/domain.com') | |
->stage('production'); | |
server('dev', 'dev.domain.com') | |
->user('username') | |
->identityFile() | |
->set('branch', 'develop') | |
->set('deploy_path', '/var/www/domain.com') | |
->stage(['dev', 'production']); | |
// Build assets locally | |
task('npm:local:build', function () { | |
runLocally("cd {{local_release_path}} && {{local/bin/npm}} run production"); | |
}); | |
// Run tests | |
task('local:phpunit', function () { | |
runLocally("cd {{local_release_path}} && ~/.composer/vendor/bin/phpunit"); | |
}); | |
// Tasks | |
task('deploy', [ | |
'local:prepare', // Create releases and shared dirs locally | |
'local:release', // Release number locally | |
'local:update_code', // git clone locally | |
'local:vendors', // composer install locally | |
'local:phpunit', // phpunit tests locally | |
'npm:local:install', // npm install locally | |
'npm:local:build', // Build locally | |
'local:symlink', // Symlink /current locally | |
'deploy:prepare', // Create releases and shared dirs on server | |
'deploy:lock', // Lock deploys on server | |
'deploy:release', // Release number on server | |
'rsync', // Send files to server | |
'deploy:writable', // Ensure shared paths are writable on server | |
'deploy:shared', // Shared and .env linking on server | |
'artisan:view:clear', // Optimze on server | |
'artisan:cache:clear', // Optimze on server | |
'artisan:config:cache', // Optimze on server | |
'artisan:optimize', // Optimze on server | |
'artisan:migrate', // Migrate DB on server | |
'deploy:symlink', // Symlink /current on server | |
'deploy:unlock', // Unlock deploys on server | |
'cleanup', // Cleanup old releases on server | |
'local:cleanup' // Cleanup old releases locally | |
])->desc('Deploy your project'); | |
// [Optional] if deploy fails automatically unlock. | |
after('deploy:failed', 'deploy:unlock'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment