Skip to content

Instantly share code, notes, and snippets.

@ab-budaev
Created June 1, 2017 10:01

Revisions

  1. Aleksei Budaev created this gist Jun 1, 2017.
    181 changes: 181 additions & 0 deletions deploy.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,181 @@
    <?php

    namespace Deployer;

    require 'recipe/laravel.php';

    // Configuration
    set('ssh_type', 'native');
    set('ssh_multiplexing', true);
    set('git_tty', true); // [Optional] Allocate tty for git on first deployment

    set('writable_mode', 'chmod');
    set('writable_chmod_mode', 777);
    set('writable_use_sudo', true);

    set('remote_user', 'REMOTE_USER');
    set('remote_host', 'REMOTE_HOST');
    set('remote_dir', 'REMOTE_DIR');

    set('allow_anonymous_stats', false);

    set('repository', 'git@bitbucket.org:REPOSITORY');
    set('branch', 'master');

    // Servers

    // Laravel shared dirs
    set('shared_dirs', [
    'bootstrap/cache',
    'storage',
    'public/uploads',
    ]);
    // Laravel shared file
    set('shared_files', [
    '.env',
    ]);
    // Laravel writable dirs
    set('writable_dirs', [
    'bootstrap/cache',
    'storage',
    'storage/app',
    'storage/app/public',
    'storage/framework',
    'storage/framework/cache',
    'storage/framework/sessions',
    'storage/framework/views',
    'storage/logs',
    'node_modules',
    'public/uploads',
    ]);

    host(get('remote_host'))
    ->stage('production')
    ->set('deploy_path', get('remote_dir'));

    /**
    * Helper tasks
    */
    desc('Disable maintenance mode');
    task('artisan:up', function () {
    $output = run('if [ -f {{deploy_path}}/current/artisan ]; then {{bin/php}} {{deploy_path}}/current/artisan up; fi');
    writeln('<info>' . $output . '</info>');
    });
    desc('Enable maintenance mode');
    task('artisan:down', function () {
    $output = run('if [ -f {{deploy_path}}/current/artisan ]; then {{bin/php}} {{deploy_path}}/current/artisan down; fi');
    writeln('<info>' . $output . '</info>');
    });
    desc('Execute artisan migrate');
    task('artisan:migrate', function () {
    run('{{bin/php}} {{release_path}}/artisan migrate --force');
    });
    desc('Execute artisan migrate:rollback');
    task('artisan:migrate:rollback', function () {
    $output = run('{{bin/php}} {{release_path}}/artisan migrate:rollback --force');
    writeln('<info>' . $output . '</info>');
    });
    desc('Execute artisan migrate:status');
    task('artisan:migrate:status', function () {
    $output = run('{{bin/php}} {{release_path}}/artisan migrate:status');
    writeln('<info>' . $output . '</info>');
    });
    desc('Execute artisan db:seed');
    task('artisan:db:seed', function () {
    $output = run('{{bin/php}} {{release_path}}/artisan db:seed --force');
    writeln('<info>' . $output . '</info>');
    });
    desc('Execute artisan cache:clear');
    task('artisan:cache:clear', function () {
    run('{{bin/php}} {{release_path}}/artisan cache:clear');
    });
    desc('Execute artisan config:cache');
    task('artisan:config:cache', function () {
    run('{{bin/php}} {{release_path}}/artisan config:cache');
    });
    desc('Execute artisan view:clear');
    task('artisan:view:clear', function () {
    run('{{bin/php}} {{release_path}}/artisan view:clear');
    });
    desc('Execute artisan optimize');
    task('artisan:optimize', function () {
    run('{{bin/php}} {{release_path}}/artisan optimize');
    });

    desc('Execute bower install');
    task('bower:install', function () {
    run('cd {{release_path}} && bower install');
    });
    desc('Execute npm run production');
    task('npm:run', function () {
    run('cd {{release_path}} && npm run production');
    });

    desc('Composer dump autoload');
    task('composer:dump:autoload', function () {
    run('cd {{release_path}} && composer dump-autoload');
    });

    task('reload:php-fpm', function () {
    run('sudo service php7.0-fpm restart');
    });

    task('npm:install', function () {
    run('cd {{release_path}} && npm install');
    });

    task('npm:run:production', function () {
    run('cd {{release_path}} && npm run production');
    });

    task('artisan:migrate', function () {
    run('{{bin/php}} && {{release_path}}/artisan migrate');
    });

    desc('Build project after deploy');
    task('build', [
    'composer:dump:autoload',
    'bower:install',
    'npm:install',
    'npm:run:production',
    'reload:php-fpm',
    ]);

    /**
    * Task deploy:public_disk support the public disk.
    * To run this task automatically, please add below line to your deploy.php file
    *
    * before('deploy:symlink', 'deploy:public_disk');
    *
    * @see https://laravel.com/docs/5.2/filesystem#configuration
    */
    desc('Make symlink for public disk');
    task('deploy:public_disk', function () {
    // Remove from source.
    run('if [ -d $(echo {{release_path}}/public/storage) ]; then rm -rf {{release_path}}/public/storage; fi');
    // Create shared dir if it does not exist.
    run('mkdir -p {{deploy_path}}/shared/storage/app/public');
    // Symlink shared dir to release dir
    run('{{bin/symlink}} {{deploy_path}}/shared/storage/app/public {{release_path}}/public/storage');
    });
    /**
    * Main task
    */
    desc('Deploy your project');
    task('deploy', [
    'deploy:prepare',
    'deploy:lock',
    'deploy:release',
    'deploy:update_code',
    'deploy:shared',
    'deploy:vendors',
    'deploy:writable',
    'artisan:view:clear',
    'artisan:cache:clear',
    'artisan:config:cache',
    'artisan:optimize',
    'deploy:symlink',
    'deploy:unlock',
    'cleanup',
    ]);
    after('deploy', 'build');