Skip to main content

recipe/common.php

<?php
namespace Deployer;

require 'recipe/common.php';

function getBranchName() {
    $stringfromfile = file('.git/HEAD', FILE_USE_INCLUDE_PATH);
    $firstLine = $stringfromfile[0]; //get the string from the array
    $explodedString = explode("/", $firstLine, 3); //seperate out by the "/" in the string
    $branchName = trim($explodedString[2]); //get the one that is always the branch name
     
     return $branchName;
}

// Project name
set('application', 'regts');

// Project repository
set('repository', 'git@bitbucket.org:guapamedia/regtsm2.git');

// [Optional] Allocate tty for git clone. Default value is false.
set('git_tty', true);
set('default_timeout', 5000);

set('allow_anonymous_stats', false);

// Hosts

host('guapa.hypernode.io')
    ->stage('production')
    ->user('app')
    ->set('deploy_path', '~/deployer');


set('release_name', function () {
    return (string) getBranchName();
});


//Config
set('shared_files', [
    'app/etc/env.php',
    'app/etc/config.php',
    'var/.maintenance.ip',
]);
set('shared_dirs', [
    'var/log',
    'var/backups',
    'pub/media',
]);
set('writable_dirs', [
    'var',
    'pub/static',
    'pub/media',
]);
set('clear_paths', [
    'var/generation/*',
    'var/cache/*',
]);

// Tasks
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("{{bin/php}} {{release_path}}/bin/magento setup:static-content:deploy");
});

desc('Enable maintenance mode');
task('magento: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('magento:maintenance:disable', function () {
    run("if [ -d $(echo {{deploy_path}}/current) ]; then {{bin/php}} {{deploy_path}}/current/bin/magento maintenance:disable; fi");
});

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

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


desc('Set production');
task('magento:production', function () {
    run("{{bin/php}} {{release_path}}/bin/magento deploy:mode:set production");
});

desc('Compile CSS');    
task('magento:css', function () {

    //NPM install gives errors
     try {
       $res=run('cd {{release_path}}/vendor/snowdog/frontools && npm install --silent');
        writeln("result: $res");  
       } catch (\Deployer\Exception\RuntimeException $e) {
            writeln("Error in NPM install. Continue!");
       }

       try {
           $res=run('cd {{release_path}}/vendor/snowdog/frontools && npm install gulp-sass@3.1.0 gulp gulp-load-plugins gulp-if gulp-less gulp-logger browser-sync cssnano gulp-postcss gulp-sourcemaps gulp-plumber gulp-notify run-sequence globby fs fs-extra marked marked-terminal merge-stream autoprefixer gulp-sass-error gulp-rename gulp-util gulp-task-loader node-sass@4.6.1');
            writeln("result: $res");
       } catch (\Deployer\Exception\RuntimeException $e) {
            writeln("Error in NPM install (custom). Continue!");
       }
   
    run('cp {{release_path}}/app/design/frontend/Regts/default/dev/task/imports.js {{release_path}}/vendor/snowdog/frontools/task/imports.js');
    run('cd {{release_path}}/vendor/snowdog/frontools && ~/node_modules/.bin/gulp setup');
});


desc('Gulp ');    
task('magento:gulp:styles', function () {
    run('cd {{release_path}}/tools && ~/node_modules/.bin/gulp imports');
    run('cd {{release_path}}/tools && ~/node_modules/.bin/gulp styles');
});



desc('Magento2 deployment operations');
task('deploy:magento', [
    'magento:maintenance:enable',  
    'magento:upgrade:db',
    'magento:cache:flush',
    'magento:production',
    'magento:css',
    'magento:gulp:styles',
    'magento:maintenance:disable'
]);

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

task('q_what_branch', function () {
    $branch = ask('What branch to deploy? Checked out: ' . getBranchName() );
   
    set('branch', $branch);
    writeln("Using " . $branch);

});

//To save time, maybe you want to copy frontools instead of installing
task('q_frontools', function () {
    $answer = ask('Do you want to (re)install frontools?');
    if($answer == 'y' or $answer == 'yes'){
        set('reinstall_frontools', $answer);
        writeln("Deployer will (re)install frontools");
    }

});

before('deploy', 'q_what_branch');
after('deploy:failed', 'magento:maintenance:disable');