forked from deployphp/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagento2.php
190 lines (160 loc) · 5.45 KB
/
magento2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace Deployer;
require_once __DIR__ . '/common.php';
use Deployer\Exception\RunException;
use Deployer\Host\Host;
const CONFIG_IMPORT_NEEDED_EXIT_CODE = 2;
const DB_UPDATE_NEEDED_EXIT_CODE = 2;
const MAINTENANCE_MODE_ACTIVE_OUTPUT_MSG = 'maintenance mode is active';
add('recipes', ['magento2']);
// Configuration
// By default setup:static-content:deploy uses `en_US`.
// To change that, simply put `set('static_content_locales', 'en_US de_DE');`
// in you deployer script.
set('static_content_locales', 'en_US');
set('content_version', function () {
return time();
});
set('shared_files', [
'app/etc/env.php',
'var/.maintenance.ip',
]);
set('shared_dirs', [
'var/composer_home',
'var/log',
'var/export',
'var/report',
'var/import',
'var/import_history',
'var/session',
'var/importexport',
'var/backups',
'var/tmp',
'pub/sitemap',
'pub/media'
]);
set('writable_dirs', [
'var',
'pub/static',
'pub/media',
'generated'
]);
set('clear_paths', [
'generated/*',
'pub/static/_cache/*',
'var/generation/*',
'var/cache/*',
'var/page_cache/*',
'var/view_preprocessed/*'
]);
set('magento_version', function () {
// detect version
$versionOutput = run('{{bin/php}} {{release_or_current_path}}/bin/magento --version');
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;
});
// Tasks
desc('Compile magento di');
task('magento:compile', function () {
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');
});
desc('Deploy assets');
task('magento:deploy:assets', function () {
run("{{bin/php}} {{release_or_current_path}}/bin/magento setup:static-content:deploy --content-version={{content_version}} {{static_content_locales}}");
});
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');
desc('Enable maintenance mode');
task('magento:maintenance:enable', function () {
run("if [ -d $(echo {{current_path}}) ]; then {{bin/php}} {{current_path}}/bin/magento maintenance:enable; fi");
});
desc('Disable maintenance mode');
task('magento:maintenance:disable', function () {
run("if [ -d $(echo {{current_path}}) ]; then {{bin/php}} {{current_path}}/bin/magento maintenance:disable; fi");
});
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 {
run('{{bin/php}} {{release_or_current_path}}/bin/magento app:config:status');
} 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');
}
run('{{bin/php}} {{release_or_current_path}}/bin/magento app:config:import --no-interaction');
if (!get('maintenance_mode_status_active')) {
invoke('magento:maintenance:disable');
}
}
});
desc('Upgrade magento database');
task('magento:upgrade:db', function () {
$databaseUpgradeNeeded = false;
try {
run('{{bin/php}} {{release_or_current_path}}/bin/magento setup:db:status');
} 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');
}
run("{{bin/php}} {{release_or_current_path}}/bin/magento setup:upgrade --keep-generated --no-interaction");
if (!get('maintenance_mode_status_active')) {
invoke('magento:maintenance:disable');
}
}
});
desc('Flush Magento Cache');
task('magento:cache:flush', function () {
run("{{bin/php}} {{release_or_current_path}}/bin/magento cache:flush");
});
desc('Magento2 deployment operations');
task('deploy:magento', [
'magento:compile',
'magento:deploy:assets',
'magento:config:import',
'magento:upgrade:db',
'magento:cache:flush'
]);
desc('Deploy your project');
task('deploy', [
'deploy:prepare',
'deploy:vendors',
'deploy:clear_paths',
'deploy:magento',
'deploy:publish',
]);
after('deploy:failed', 'magento:maintenance:disable');