Skip to main content

recipe/common.php

<?php
namespace Deployer;

require 'recipe/common.php';

inventory('hosts.yml');

set('default_timeout', 3600);

set('application', 'Powerbuy');

set('keep_releases', 3);

set('repository', 'git@bitbucket.org:centraltechnology/powerbuy-mdc.git');

set('git_tty', false);

set('shared_files', [
    'app/etc/env.php',
    'app/etc/config.php'
]);

set('shared_dirs', [
    'var',
    'pub/media',
]);

set('writable_dirs', [
    'pub/static',
]);

set('clear_paths', [
    'var/generation/*',
    'var/cache/*',
]);

desc('Compile magento di');
task('magento:compile', function () {
    run("{{bin/php}} {{release_path}}/bin/magento setup:di:compile");
    run('cd {{release_path}} && {{bin/composer}} dump-autoload -o');
});

desc('Deploy assets');
task('magento:deploy:assets', function () {
    run("printf '{{bin/php}} {{release_path}}/bin/magento setup:static-content:deploy --theme Central/default --theme Central/backend --jobs=1 -v -l en_US | tr -d '.' \\n"
        . "{{bin/php}} {{release_path}}/bin/magento setup:static-content:deploy --theme Central/default --theme Central/backend --jobs=1 -v -l th_TH | tr -d '.' \\n' | xargs -I CMD -P 0 bash -c CMD");
    //$languages = ['en_US','th_TH'];
    //foreach($languages as $lang) {
    //    run("{{bin/php}} {{release_path}}/bin/magento setup:static-content:deploy --theme Central/default --theme Central/backend --jobs=1 -v -l " . $lang .  " | tr -d '.'");
    //}
});

desc('Enable maintenance mode');
task('maintenance:enable', function () {
    //run("if [ -d $(echo {{deploy_path}}/current) ]; then {{bin/php}} {{deploy_path}}/current/bin/magento maintenance:enable; fi");
});

desc('Disable maintenance mode');
task('maintenance:disable', function () {
    //run("if [ -d $(echo {{deploy_path}}/current) ]; then {{bin/php}} {{deploy_path}}/current/bin/magento maintenance:disable; fi");
});

desc('Enable all modules for deployment');
task('magento:module:enable', function() {
  run("{{bin/php}} {{release_path}}/bin/magento module:enable --all");
});

desc('Upgrade magento database');
task('magento:setup:upgrade', function () {
    run("mv {{release_path}}/var/di {{release_path}}/var/di_");
    run("{{bin/php}} {{release_path}}/bin/magento setup:upgrade --keep-generated");
    run("mv {{release_path}}/var/di_ {{release_path}}/var/di");
});


desc('Setup magento cron');
task('magento:setup:cron', function () {
    $crons = [
        '* * * * * {{bin/php}} {{deploy_path}}/current/bin/magento cron:run >> {{deploy_path}}/shared/var/log/magento.cron.log',
        '* * * * * {{bin/php}} {{deploy_path}}/current/update/cron.php >> {{deploy_path}}/shared/var/log/update.cron.log',
        '* * * * * {{bin/php}} {{deploy_path}}/current/bin/magento setup:cron:run >> {{deploy_path}}/shared/var/log/setup.cron.log'
    ];

    run('crontab -r || true');
    foreach($crons as $cron) {
        run('crontab -l | { cat; echo "' . $cron . '"; } | crontab -');
    }
});


desc('Flush Magento Cache');
task('magento:cache:flush', function () {
    run("{{bin/php}} {{release_path}}/bin/magento cache:flush");
});

desc('Apply patches');
task('magento:patch', function () {
    run("cd {{release_path}} && printf 'for i in m2-hotfixes/*; do git apply \$i; done' | xargs -I CMD -P 0 bash -c CMD");
});

desc('Restart PHP-FPM');
task('deploy:restart-php-fpm', function () {
    run("sudo service php-fpm restart");
});

desc('Restart PHP-FPM');
task('deploy:restart-php-fpm-local', function () {
    runLocally("sudo service php-fpm restart");
});

desc('Restart Varnish');
task('deploy:restart-varnish', function () {
    run("sudo service varnish restart");
});

desc('Enable cache');
task('magento:enable-cache', function() {
   run("{{bin/php}} cache:enable");
});

desc('Magento2 maintenance operations');
task('magento:maintenance', function() {
    $dbVersionIsValid = (int) run('{{bin/php}} {{release_path}}/deployment/check-db-version.php');

    writeln('Need downtime: ' . $dbVersionIsValid ? "No" : "Yes");

    if (!$dbVersionIsValid) {
        invoke('maintenance:enable');
        invoke('magento:setup:upgrade');
    }
});

desc('update code using copy current');
task('deploy:update_code-dev', function() {
    run("cp -R {{deploy_path}}/current/.git {{release_path}}/.git");
    within("{{release_path}}", function() {
        run("git fetch origin develop");
        run("git checkout -f origin/develop");
    });   
});

desc('AWS CodeDeploy status');
task('aws:codedeploy:status', function () {
    run("export status=$(find /var/log/magento-deployment/* | sort -Vr | head -n1); test -f \$status && cat \$status");
});

desc('Trigger AWS CodeDeploy service');
task('deploy:aws', function () {
    run("sudo /usr/local/sbin/deploy-new-revision");
});

desc('Magento2 deployment operations');
task('deploy:magento', [
    'magento:patch',
    'magento:module:enable',
    'magento:compile',
    'magento:deploy:assets',
    'magento:maintenance',
    'magento:cache:flush',
]);

desc('Deploy your project');
task('deploy', [
    'deploy:info',
    'deploy:prepare',
    'deploy:lock',
    'deploy:release',
    'deploy:update_code',
    'deploy:shared',
    'deploy:writable',
    'deploy:vendors',
    'deploy:clear_paths',
    'deploy:magento',
    'deploy:symlink',
    'deploy:aws',
    'maintenance:disable',
    'deploy:unlock',
    'cleanup',
    'success'
]);

desc('Handle deployment error');
task('deploy:recover', 'deploy:unlock');

after('deploy:failed', 'deploy:recover');