2016-11-28 11:11:23 +01:00
|
|
|
<?php
|
|
|
|
namespace Deployer;
|
|
|
|
|
|
|
|
require_once __DIR__ . '/common.php';
|
|
|
|
|
2020-11-10 23:36:38 +01:00
|
|
|
use Deployer\Exception\RunException;
|
2020-12-08 00:02:08 +01:00
|
|
|
use Deployer\Host\Host;
|
2020-11-10 23:36:38 +01:00
|
|
|
|
|
|
|
const CONFIG_IMPORT_NEEDED_EXIT_CODE = 2;
|
|
|
|
const DB_UPDATE_NEEDED_EXIT_CODE = 2;
|
|
|
|
const MAINTENANCE_MODE_ACTIVE_OUTPUT_MSG = 'maintenance mode is active';
|
|
|
|
|
2020-10-25 16:00:05 +01:00
|
|
|
add('recipes', ['magento2']);
|
|
|
|
|
2016-11-28 11:11:23 +01:00
|
|
|
// Configuration
|
2020-04-25 23:00:08 +03:00
|
|
|
|
2020-10-08 01:53:45 +02:00
|
|
|
// By default setup:static-content:deploy uses `en_US`.
|
2021-03-05 11:51:18 +01:00
|
|
|
// To change that, simply put `set('static_content_locales', 'en_US de_DE');`
|
2020-04-25 23:00:08 +03:00
|
|
|
// in you deployer script.
|
|
|
|
set('static_content_locales', 'en_US');
|
|
|
|
|
2020-12-08 00:02:08 +01:00
|
|
|
set('content_version', function () {
|
|
|
|
return time();
|
|
|
|
});
|
|
|
|
|
2017-01-19 11:38:37 +07:00
|
|
|
set('shared_files', [
|
|
|
|
'app/etc/env.php',
|
|
|
|
'var/.maintenance.ip',
|
|
|
|
]);
|
|
|
|
set('shared_dirs', [
|
2019-05-26 03:30:18 -04:00
|
|
|
'var/composer_home',
|
2017-01-19 11:38:37 +07:00
|
|
|
'var/log',
|
2019-05-26 03:30:18 -04:00
|
|
|
'var/export',
|
|
|
|
'var/report',
|
2020-11-10 23:36:38 +01:00
|
|
|
'var/import',
|
2019-05-26 03:30:18 -04:00
|
|
|
'var/import_history',
|
|
|
|
'var/session',
|
|
|
|
'var/importexport',
|
2017-01-19 11:38:37 +07:00
|
|
|
'var/backups',
|
2019-05-26 03:30:18 -04:00
|
|
|
'var/tmp',
|
2020-11-10 23:36:38 +01:00
|
|
|
'pub/sitemap',
|
2019-05-26 03:30:18 -04:00
|
|
|
'pub/media'
|
2017-01-19 11:38:37 +07:00
|
|
|
]);
|
|
|
|
set('writable_dirs', [
|
|
|
|
'var',
|
|
|
|
'pub/static',
|
|
|
|
'pub/media',
|
2019-07-05 23:27:27 +02:00
|
|
|
'generated'
|
2017-01-19 11:38:37 +07:00
|
|
|
]);
|
|
|
|
set('clear_paths', [
|
2019-07-05 23:27:27 +02:00
|
|
|
'generated/*',
|
2019-05-26 03:30:18 -04:00
|
|
|
'pub/static/_cache/*',
|
2017-01-19 11:38:37 +07:00
|
|
|
'var/generation/*',
|
|
|
|
'var/cache/*',
|
2019-05-26 03:30:18 -04:00
|
|
|
'var/page_cache/*',
|
|
|
|
'var/view_preprocessed/*'
|
2017-01-19 11:38:37 +07:00
|
|
|
]);
|
2016-11-28 11:11:23 +01:00
|
|
|
|
2020-11-10 23:36:38 +01:00
|
|
|
set('magento_version', function () {
|
|
|
|
// detect version
|
2021-04-03 13:05:45 +01:00
|
|
|
$versionOutput = run('{{bin/php}} {{release_or_current_path}}/bin/magento --version');
|
2020-11-10 23:36:38 +01:00
|
|
|
preg_match('/(\d+\.?)+$/', $versionOutput, $matches);
|
|
|
|
return $matches[0] ?? "2.0";
|
|
|
|
});
|
|
|
|
|
|
|
|
set('maintenance_mode_status_active', function () {
|
|
|
|
// detect maintenance mode active
|
|
|
|
$maintenanceModeStatusOutput = run("{{bin/php}} {{current_path}}/bin/magento maintenance:status");
|
|
|
|
return strpos($maintenanceModeStatusOutput, MAINTENANCE_MODE_ACTIVE_OUTPUT_MSG) !== false;
|
|
|
|
});
|
|
|
|
|
2016-11-28 11:11:23 +01:00
|
|
|
// Tasks
|
|
|
|
desc('Compile magento di');
|
|
|
|
task('magento:compile', function () {
|
2021-04-03 13:05:45 +01:00
|
|
|
run('cd {{release_or_current_path}} && {{bin/composer}} dump-autoload -o');
|
|
|
|
run("{{bin/php}} {{release_or_current_path}}/bin/magento setup:di:compile");
|
|
|
|
run('cd {{release_or_current_path}} && {{bin/composer}} dump-autoload -o');
|
2016-11-28 11:11:23 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
desc('Deploy assets');
|
|
|
|
task('magento:deploy:assets', function () {
|
2021-04-03 13:05:45 +01:00
|
|
|
run("{{bin/php}} {{release_or_current_path}}/bin/magento setup:static-content:deploy --content-version={{content_version}} {{static_content_locales}}");
|
2016-11-28 11:11:23 +01:00
|
|
|
});
|
|
|
|
|
2020-12-08 00:02:08 +01:00
|
|
|
desc('Sync content version');
|
|
|
|
task('magento:sync:content_version', function () {
|
|
|
|
$timestamp = time();
|
|
|
|
on(select('all'), function (Host $host) use ($timestamp) {
|
|
|
|
$host->set('content_version', $timestamp);
|
|
|
|
});
|
|
|
|
})->once();
|
|
|
|
|
|
|
|
before('magento:deploy:assets', 'magento:sync:content_version');
|
|
|
|
|
2016-11-28 11:11:23 +01:00
|
|
|
desc('Enable maintenance mode');
|
|
|
|
task('magento:maintenance:enable', function () {
|
2020-10-09 01:35:42 +02:00
|
|
|
run("if [ -d $(echo {{current_path}}) ]; then {{bin/php}} {{current_path}}/bin/magento maintenance:enable; fi");
|
2016-11-28 11:11:23 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
desc('Disable maintenance mode');
|
|
|
|
task('magento:maintenance:disable', function () {
|
2020-10-09 01:35:42 +02:00
|
|
|
run("if [ -d $(echo {{current_path}}) ]; then {{bin/php}} {{current_path}}/bin/magento maintenance:disable; fi");
|
2016-11-28 11:11:23 +01:00
|
|
|
});
|
|
|
|
|
2020-11-10 23:36:38 +01:00
|
|
|
desc('Config Import');
|
|
|
|
task('magento:config:import', function () {
|
|
|
|
$configImportNeeded = false;
|
|
|
|
|
|
|
|
if(version_compare(get('magento_version'), '2.2.0', '<')) {
|
|
|
|
//app:config:import command does not exist in 2.0.x and 2.1.x branches
|
|
|
|
$configImportNeeded = false;
|
|
|
|
} elseif(version_compare(get('magento_version'), '2.2.4', '<')) {
|
|
|
|
//app:config:status command does not exist until 2.2.4, so proceed with config:import in every deploy
|
|
|
|
$configImportNeeded = true;
|
|
|
|
} else {
|
|
|
|
try {
|
2021-04-03 13:05:45 +01:00
|
|
|
run('{{bin/php}} {{release_or_current_path}}/bin/magento app:config:status');
|
2020-11-10 23:36:38 +01:00
|
|
|
} catch (RunException $e) {
|
|
|
|
if ($e->getExitCode() == CONFIG_IMPORT_NEEDED_EXIT_CODE) {
|
|
|
|
$configImportNeeded = true;
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($configImportNeeded) {
|
|
|
|
if (!get('maintenance_mode_status_active')) {
|
|
|
|
invoke('magento:maintenance:enable');
|
|
|
|
}
|
|
|
|
|
2021-04-03 13:05:45 +01:00
|
|
|
run('{{bin/php}} {{release_or_current_path}}/bin/magento app:config:import --no-interaction');
|
2020-11-10 23:36:38 +01:00
|
|
|
|
|
|
|
if (!get('maintenance_mode_status_active')) {
|
|
|
|
invoke('magento:maintenance:disable');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-28 11:11:23 +01:00
|
|
|
desc('Upgrade magento database');
|
|
|
|
task('magento:upgrade:db', function () {
|
2020-11-10 23:36:38 +01:00
|
|
|
$databaseUpgradeNeeded = false;
|
|
|
|
|
|
|
|
try {
|
2021-04-03 13:05:45 +01:00
|
|
|
run('{{bin/php}} {{release_or_current_path}}/bin/magento setup:db:status');
|
2020-11-10 23:36:38 +01:00
|
|
|
} catch (RunException $e) {
|
|
|
|
if ($e->getExitCode() == DB_UPDATE_NEEDED_EXIT_CODE) {
|
|
|
|
$databaseUpgradeNeeded = true;
|
|
|
|
} else {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($databaseUpgradeNeeded) {
|
|
|
|
if (!get('maintenance_mode_status_active')) {
|
|
|
|
invoke('magento:maintenance:enable');
|
|
|
|
}
|
|
|
|
|
2021-04-11 16:49:46 +02:00
|
|
|
run("{{bin/php}} {{release_or_current_path}}/bin/magento setup:upgrade --keep-generated --no-interaction");
|
2020-11-10 23:36:38 +01:00
|
|
|
|
|
|
|
if (!get('maintenance_mode_status_active')) {
|
|
|
|
invoke('magento:maintenance:disable');
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 11:11:23 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
desc('Flush Magento Cache');
|
|
|
|
task('magento:cache:flush', function () {
|
2021-04-03 13:05:45 +01:00
|
|
|
run("{{bin/php}} {{release_or_current_path}}/bin/magento cache:flush");
|
2016-11-28 11:11:23 +01:00
|
|
|
});
|
|
|
|
|
2017-01-19 11:38:37 +07:00
|
|
|
desc('Magento2 deployment operations');
|
|
|
|
task('deploy:magento', [
|
2021-11-07 22:50:16 +01:00
|
|
|
'magento:build',
|
2020-11-10 23:36:38 +01:00
|
|
|
'magento:config:import',
|
2017-01-19 11:38:37 +07:00
|
|
|
'magento:upgrade:db',
|
2021-11-07 22:50:16 +01:00
|
|
|
'magento:cache:flush',
|
|
|
|
]);
|
|
|
|
|
|
|
|
desc('Magento2 build operations');
|
|
|
|
task('magento:build', [
|
|
|
|
'magento:compile',
|
|
|
|
'magento:deploy:assets',
|
2017-01-19 11:38:37 +07:00
|
|
|
]);
|
|
|
|
|
2016-11-28 11:11:23 +01:00
|
|
|
desc('Deploy your project');
|
|
|
|
task('deploy', [
|
2020-10-08 01:53:45 +02:00
|
|
|
'deploy:prepare',
|
2016-11-28 11:11:23 +01:00
|
|
|
'deploy:vendors',
|
|
|
|
'deploy:clear_paths',
|
2017-01-19 11:38:37 +07:00
|
|
|
'deploy:magento',
|
2020-10-08 01:53:45 +02:00
|
|
|
'deploy:publish',
|
2016-11-28 11:11:23 +01:00
|
|
|
]);
|
2017-05-30 10:42:37 +02:00
|
|
|
|
|
|
|
after('deploy:failed', 'magento:maintenance:disable');
|