/
deploy.php
316 lines (272 loc) · 10.8 KB
/
deploy.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
// Create by Rafael Corrêa Gomes
// Installing deployer and dependencies
//
// curl -LO https://deployer.org/deployer.phar && sudo mv deployer.phar /usr/local/bin/dep && sudo chmod +x /usr/local/bin/dep
// composer require deployer/recipes --dev
// composer require rafaelstz/deployer-magento2 dev-master --dev
namespace Deployer;
require 'recipe/common.php';
require 'vendor/deployer/recipes/recipe/cachetool.php';
require 'vendor/deployer/recipes/recipe/slack.php';
# ----- Deployment properties ---
set('forwardAgent', true);
set('git_tty', false); // [Optional] Allocate tty for git clone. Default value is false.
set('ssh_multiplexing', true);
set('php', '/usr/local/bin/php');
set('magerun', '/usr/local/bin/n98-magerun2');
set('composer', '/usr/local/bin/composer');
// set('default_timeout', 360);
set('verbose', '--quiet'); // Use --quite or -v or -vvv
set('release_name', function () {
return date('YmdHis');
});
# ----- Magento properties -------
set('is_production', 0);
set('compile_UAT', 1);
set('languages', 'en_US');
set('magento_dir', '/');
set('magento_bin', '{{magento_dir}}bin/magento');
set('shared_files', [
'{{magento_dir}}app/etc/env.php',
'{{magento_dir}}var/.maintenance.ip'
// '{{magento_dir}}pub/robots.txt',
// '{{magento_dir}}pub/sitemap.xml'
]);
set('shared_dirs', [
'{{magento_dir}}var/composer_home',
'{{magento_dir}}var/log',
'{{magento_dir}}var/cache',
'{{magento_dir}}var/export',
'{{magento_dir}}var/report',
'{{magento_dir}}var/import_history',
'{{magento_dir}}var/session',
'{{magento_dir}}var/importexport',
'{{magento_dir}}var/backups',
'{{magento_dir}}var/tmp',
'{{magento_dir}}pub/sitemaps',
'{{magento_dir}}pub/media'
]);
set('writable_dirs', [
'{{magento_dir}}var',
'{{magento_dir}}pub/static',
'{{magento_dir}}pub/media',
'{{magento_dir}}generation'
]);
set('clear_paths', [
'{{magento_dir}}pub/static/_cache',
'{{magento_dir}}var/cache',
'{{magento_dir}}var/page_cache',
'{{magento_dir}}var/view_preprocessed',
'{{magento_dir}}var/tmp'
]);
// Check Magento version
set('magento_version', function (){
return run("{{magerun}} sys:info version --root-dir={{release_path}}");
});
# ----- Magento 2 Tasks -------
require_once __DIR__ . '/recipes/backup.php';
desc('Composer Install');
task('composer:install', function () {
if (get('is_production')) {
run("cd {{release_path}}{{magento_dir}} && {{composer}} install --no-dev --optimize-autoloader {{verbose}}");
} else {
run("cd {{release_path}}{{magento_dir}} && {{composer}} install --optimize-autoloader {{verbose}}");
}
// Deprecated since 1.2.3
// run('cd {{release_path}}{{magento_dir}} && {{composer}} dump-autoload --no-interaction --optimize {{verbose}} 2>&1');
});
desc('Composer Update');
task('composer:update', function () {
if (get('is_production')) {
run("cd {{release_path}}{{magento_dir}} && {{composer}} update --no-dev --optimize-autoloader {{verbose}}");
} else {
run("cd {{release_path}}{{magento_dir}} && {{composer}} update --optimize-autoloader {{verbose}}");
}
// Deprecated since 1.2.3
// run('cd {{release_path}}{{magento_dir}} && {{composer}} dump-autoload --no-interaction --optimize {{verbose}} 2>&1');
});
desc('Composer Clear Cache');
task('composer:clearcache', function () {
run("cd {{release_path}}{{magento_dir}} && {{composer}} clearcache");
run("cd {{release_path}}{{magento_dir}} && rm -rf var/composer_home/cache/");
run("cd {{release_path}}{{magento_dir}} && rm -r composer.lock");
});
desc('Compile Magento DI');
task('magento:compile', function () {
if (get('is_production') || get('compile_UAT')) {
run("{{php}} {{release_path}}{{magento_bin}} setup:di:compile {{verbose}}");
} else {
write("Not running the DI Compile for UAT");
}
});
desc('Deploy assets');
task('magento:deploy:assets', function () {
if (get('is_production')) {
run("{{php}} {{release_path}}{{magento_bin}} setup:static-content:deploy {{verbose}}");
} elseif (get('compile_UAT')) {
run("{{php}} {{release_path}}{{magento_bin}} setup:static-content:deploy --force {{verbose}}");
} else {
write("Not running the Static Content deploy for UAT");
}
});
desc('Enable maintenance mode');
task('magento:maintenance:enable', function () {
run("if [ -d $(echo {{release_path}}{{magento_dir}}bin) ]; then {{php}} {{release_path}}{{magento_bin}} maintenance:enable {{verbose}}; fi");
});
desc('Disable maintenance mode');
task('magento:maintenance:disable', function () {
run("if [ -d $(echo {{release_path}}{{magento_dir}}bin) ]; then {{php}} {{release_path}}{{magento_bin}} maintenance:disable {{verbose}}; fi");
});
desc('Upgrade magento database');
task('magento:upgrade:db', function () {
// Check if need update DB
$isDbUpdated = test('[ "$({{php}} {{release_path}}{{magento_bin}} setup:db:status --no-ansi -n)" == "All modules are up to date." ]');
if (!$isDbUpdated) {
run("if [ -d $(echo {{release_path}}{{magento_dir}}bin) ]; then {{php}} {{release_path}}{{magento_bin}} maintenance:enable {{verbose}}; fi");
run("{{php}} {{magerun}} setup:upgrade --keep-generated --root-dir={{release_path}} {{verbose}}");
run("{{php}} {{magerun}} sys:setup:downgrade-versions --root-dir={{release_path}} {{verbose}}");
run("if [ -d $(echo {{release_path}}{{magento_dir}}bin) ]; then {{php}} {{release_path}}{{magento_bin}} maintenance:disable {{verbose}}; fi");
}
write("All modules are up to date.");
});
desc('Flush Magento Cache');
task('magento:cache:flush', function () {
run("{{php}} {{release_path}}{{magento_bin}} cache:flush {{verbose}}");
});
desc('Enable allow symlink config in Magento Panel');
task('magento:config', function () {
if (test("[ -f {{release_path}}{{magento_dir}}app/etc/env.php ]")) {
run("cd {{release_path}} && {{php}} {{magerun}} config:store:set dev/template/allow_symlink 1 {{verbose}}");
run("cd {{release_path}} && {{php}} {{release_path}}{{magento_bin}} module:disable Magento_Version {{verbose}}");
if (get('is_production')) {
run("cd {{release_path}} && {{php}} {{magerun}} config:store:set design/search_engine_robots/default_robots INDEX,FOLLOW {{verbose}}");
} else {
run("cd {{release_path}} && {{php}} {{magerun}} config:store:set design/search_engine_robots/default_robots NOINDEX,NOFOLLOW {{verbose}}");
}
}
});
desc('Remove the content of the generated folder');
task('magento:clean:generated', function () {
run("cd {{release_path}}; rm -rf generated/*");
});
desc('Set deploy mode set');
task('magento:deploy:mode:set', function () {
if (get('is_production')) {
run("{{php}} -f {{release_path}}{{magento_bin}} deploy:mode:set production --skip-compilation {{verbose}}");
} else {
run("{{php}} -f {{release_path}}{{magento_bin}} deploy:mode:set developer {{verbose}}");
}
});
desc('Set right permissions to folders and files');
task('magento:setup:permissions', function () {
// run("find {{release_path}}{{magento_dir}} -type d -exec chmod 755 {} \;");
// run("find {{release_path}}{{magento_dir}} -type f -exec chmod 644 {} \;");
run("chmod -R 755 {{release_path}}");
run("chmod -R 777 {{release_path}}{{magento_dir}}var");
run("chmod -R 777 {{release_path}}{{magento_dir}}generated");
run("chmod -R 777 {{release_path}}{{magento_dir}}pub/static");
run("chmod +x {{release_path}}{{magento_bin}}");
});
desc('Lock the previous release with the maintenance flag');
task('deploy:previous', function () {
$releases = get('releases_list');
if ($releases[1]) {
run("{{php}} {{deploy_path}}/releases/{$releases[1]}{{magento_bin}} maintenance:enable {{verbose}}");
}
});
// Magento 2 Logs
desc('Check Magento system log');
task('magento:log:system', function () {
write(run("tail -n 50 {{deploy_path}}/shared/var/log/system.log"));
});
desc('Check Magento debug log');
task('magento:log:debug', function () {
write(run("tail -n 50 {{deploy_path}}/shared/var/log/debug.log"));
});
desc('Check Magento exception log');
task('magento:log:exception', function () {
write(run("tail -n 50 {{deploy_path}}/shared/var/log/exception.log"));
});
desc('Clear the Magento logs');
task('magento:log:clear', function () {
run("rm -rf {{deploy_path}}/shared/var/log/*.log");
});
// OPCache and Redis
desc('Redis cache flush');
task('redis:flush', function () {
run("redis-cli -n 0 flushall");
});
desc('OPCache cache flush');
task('opcache:flush', function () {
run("{{php}} -r 'opcache_reset();'");
});
// ======= Disable symlink
desc('Change the copy of the folder for a symlink');
task('deploy:symlink:create', function () {
$releases = get('releases_list');
run("cd {{deploy_path}} && mv current current-folder");
run("cd {{deploy_path}} && ln -s releases/{$releases[0]} current");
});
desc('Change symlink for a copy of the folder');
task('deploy:symlink:remove', function () {
$releases = get('releases_list');
if ($releases[0]) {
run("if [ -d $(echo {{deploy_path}}/current-folder) ]; then cd {{deploy_path}} && rm -rf current-folder; fi");
run("cd {{deploy_path}} && cp -as $(pwd)/releases/{$releases[0]} current-folder");
run("cd {{deploy_path}} && mv current current-symlink");
run("cd {{deploy_path}} && mv current-folder current");
}
});
// before('deploy', 'deploy:symlink:create');
// after('success', 'deploy:symlink:remove');
desc('Magento2 deployment operations');
task('deploy:magento', [
'magento:setup:permissions',
'magento:config',
'magento:deploy:mode:set',
'magento:deploy:assets',
'magento:clean:generated',
'magento:upgrade:db',
'magento:compile',
'magento:cache:flush',
'magento:setup:permissions'
]);
desc('Deploying...');
task('deploy', [
'deploy:info',
'deploy:prepare',
'deploy:lock',
'deploy:release',
'deploy:update_code',
'deploy:shared',
'deploy:vendors',
'deploy:clear_paths',
// 'deploy:writable',
// Deprecated since 1.2.2
// 'composer:update',
'deploy:magento',
'deploy:symlink',
'opcache:flush',
'redis:flush',
'deploy:unlock',
'deploy:previous',
'cleanup',
'success'
]);
after('deploy:failed', 'deploy:unlock');
// after('deploy:failed', 'deploy:magento');
after('deploy:failed', 'magento:maintenance:disable');
// before('rollback', 'rollback:validate');
after('rollback', 'deploy:magento');
after('rollback', 'magento:maintenance:disable');
after('rollback', 'magento:cache:flush');
// composer require deployer/recipes --dev
// ======= Cachetool
// after('deploy:symlink', 'cachetool:clear:opcache');
// or
// after('deploy:symlink', 'cachetool:clear:apc');
// ======= Slack
//before('deploy', 'slack:notify');
//after('success', 'slack:notify:success');
//after('deploy:failed', 'slack:notify:failure');