Skip to content

Instantly share code, notes, and snippets.

@martincarlin87
Last active October 2, 2021 20:39
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save martincarlin87/4e223248721e2d26a2b2aa6ffe452666 to your computer and use it in GitHub Desktop.
Save martincarlin87/4e223248721e2d26a2b2aa6ffe452666 to your computer and use it in GitHub Desktop.
Deployer Laravel 5 Example
<?php
namespace Deployer;
require 'recipe/laravel.php';
// Configuration
set('ssh_type', 'native');
set('repository', '[REPO URL]');
set('branch', 'master');
set('keep_releases', 5);
set('shared_files', ['.env']);
set('shared_dirs', ['storage']);
set('writable_dirs', ['storage', 'vendor']);
// Servers
server('production', '[SERVER URL]')
->user('martin')
->set('deploy_path', '[DIRECTORY PATH]')
->set('bin/npm', function () {
return run('which npm')->toString();
});
/**
* Create cache folder in bootstrap directory
*/
task('deploy:cache_folder', function() {
run('mkdir {{release_path}}/bootstrap/cache');
})->desc('Manually create cache folder');
/**
* Run migrations
*/
task('migration', function() {
run('php {{release_path}}/artisan migrate --force');
})->desc('Artisan migrations');
/**
* Install npm
*/
task('npm:install', function () {
$npm_folder_exists = run('if [ ! -L {{deploy_path}}/shared/node_modules ] && [ -d {{deploy_path}}/shared/node_modules ]; then echo true; fi')->toBool();
if (!$npm_folder_exists) {
run('cd {{deploy_path}}/current; {{bin/npm}} install');
run('mv {{deploy_path}}/current/node_modules {{deploy_path}}/shared');
}
run('ln -s {{deploy_path}}/shared/node_modules {{deploy_path}}/current');
})->desc('Execute npm install');
/**
* Assets compilation
*/
task('assets:generate', function() {
cd('{{release_path}}');
run('gulp --production');
})->desc('Assets generation');
/**
* Upload .env.production file as .env
*/
task('environment', function () {
upload('.env.production', '{{release_path}}/.env');
})->desc('Environment setup');
/**
* Clear cache
*/
task('artisan:cache:clear', function () {
$output = run('{{bin/php}} {{deploy_path}}/current/artisan cache:clear');
writeln('<info>' . $output . '</info>');
})->desc('Execute artisan cache:clear');
/**
* Clear compiled views
*/
task('artisan:cache:clear', function () {
$output = run('{{bin/php}} {{deploy_path}}/current/artisan view:clear');
writeln('<info>' . $output . '</info>');
})->desc('Execute artisan view:clear');
/**
* Recreate route cache file
*/
task('artisan:route:cache', function () {
$output = run('{{bin/php}} {{deploy_path}}/current/artisan route:cache');
writeln('<info>' . $output . '</info>');
})->desc('Execute artisan route:cache');
/**
* Recreate config cache
*/
task('artisan:config:cache', function () {
$output = run('{{bin/php}} {{deploy_path}}/current/artisan config:cache');
writeln('<info>' . $output . '</info>');
})->desc('Execute artisan config:cache');
/**
* Fix permissions
*/
task('permissions:reset', function () {
run('cd {{deploy_path}}');
run('sudo chown -R [USER]:[GROUP] {{deploy_path}}');
run('sudo find . -type f -exec chmod 644 {} \;');
run('sudo find . -type d -exec chmod 755 {} \;');
run('sudo chmod 775 {{deploy_path}}/shared/node_modules');
run('sudo chmod 775 {{deploy_path}}/shared/storage');
run('cd {{release_path}}');
run('sudo chmod 775 {{release_path}}/vendor');
})->desc('Fix permissions');
/**
* Create storage folders not in version control
*/
task('deploy:storage_folder', function() {
run('mkdir -p {{release_path}}/storage/app/public');
run('mkdir -p {{release_path}}/storage/framework/cache');
run('mkdir -p {{release_path}}/storage/framework/session');
run('mkdir -p {{release_path}}/storage/framework/views');
run('mkdir -p {{release_path}}/storage/logs');
})->desc('Manually create cache folder');
task('deploy', [
'deploy:prepare',
'deploy:release',
'deploy:update_code',
'deploy:cache_folder',
'deploy:vendors',
// 'deploy:writable',
'deploy:shared',
'deploy:symlink',
'npm:install',
'assets:generate',
'environment',
'migration',
'artisan:cache:clear',
'artisan:view:clear',
'artisan:route:cache'
'artisan:config:cache',
'deploy:storage_folder',
'cleanup',
'permissions:reset',
])->desc('Deploy the app');
after('deploy', 'success');
@waqas-mehmood-pk
Copy link

Thank @martincarlin87 for your kind effort, can you please update this with deployer v6.0 especially server tag.

// Servers
server('production', '[SERVER URL]')
  ->user('martin')
  ->set('deploy_path', '[DIRECTORY PATH]')
  ->set('bin/npm', function () {
    return run('which npm')->toString();
  });

In v6 of deployer server change to host.

@martincarlin87
Copy link
Author

Hi @whfamily2006,

I've just saw your comment, thanks for pointing that out, I've not used Deployer for a while so I've not kept up with any changes.

I'll have a look and see if I can improve and update the script.

Cheers,
Martin

@eyramm
Copy link

eyramm commented Jul 19, 2018

Any progress?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment