-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.php
executable file
·359 lines (302 loc) · 10.4 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
namespace Deployer;
use Exception;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Dependencies
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
require 'recipe/common.php';
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Environments
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
define('ENV_PATH', realpath(getcwd()) . '/');
define('ENV_FILE', ENV_PATH . 'deploy.json');
// Define the project root
set('abspath', ENV_PATH);
if (file_exists(ENV_FILE) == false) {
throw new Exception('Unable to find configuration file at ' . ENV_FILE);
} else {
$json = file_get_contents(ENV_FILE);
if ($json == false) {
throw new Exception(
'Unable to read ' . $env_path . 'config.json. Check your current user has permission to read the file'
);
}
$json = json_decode($json, true, 12);
// register the repo
set('repository', $json['git_repo']);
set('config', $json);
// Collect the WordPress config
$wp_json = $json['wordpress'];
// Collect the environment config
$env_json = $json['environments'];
// Set the name
set('application', $wp_json['wp_sitename']);
// register the local wp url
set('local_url', $wp_json['wp_home_url']);
// Register hosts and associated metadata
if (!empty($env_json)) {
if (isset($env_json['local'])) {
$local_json = $env_json['local'];
$host = localhost();
$host->hostname('localhost');
$host->set('local', true);
unset($env_json['local']);
}
foreach ($env_json as $env => $env_config) {
$host = host($env);
$host->set('stage', $env);
$host->set('forwardAgent', true);
foreach ($env_config as $key => $value) {
if ($key == 'hostname') {
$host->hostname($value);
} elseif ($key == 'deploy_user') {
$host->user($value);
} else {
$host->set($key, $value);
}
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Misc. Helper Functions
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Searches $file for $before and replaces it with $after
*
* @param string $file
* @param string $before
* @param string $after
* @return bool
*/
function searchreplaceinfile(string $file, string $before, string $after)
{
$seperator = '/';
$before = str_replace($seperator, '\\' . $seperator, $before);
$after = str_replace($seperator, '\\' . $seperator, $after);
$cmd = "{{bin/sed}} 's" . $seperator . $before . $seperator . $after . $seperator . "g' \"$file\"";
$stage = get('stage', 'local');
if ($stage == 'local') {
return runLocally($cmd);
} else {
return run($cmd);
}
}
/**
* Get an array of environment vars
*
* @param string $stage
* @return array
*/
function getenvbag(string $stage)
{
$config = get('config');
return $config['environments'][$stage];
}
/**
* Get an array of default configuration options
*
* @return array
*/
function getconfig()
{
$config = get('config');
return $config['wordpress'];
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Below be dragons - tread carefully!
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Detect the current operator
set('user', function () {
try {
return runLocally('whoami');
} catch (\Throwable $exception) {
return 'no_user';
}
});
// Can a user interactive with the TTY?
set('allow_input', function () {
$user = get('user');
if ($user != 'runner') {
// Do not allow interaction for when used with CI/CD
return true;
} else {
return false;
}
});
// Define a list of files that should be shared between deployments
set('shared_files', ['wp-config.php', '.htaccess', 'robots.txt']);
// Define a list of files that should be copied from the 'templates' folder to the root
set('templates', ['.htaccess', 'robots.txt']);
// Should a TTY be opened for Git?
set('git_tty', get('allow_input'));
// Set the directory where shared files are set
set('shared_dir', function () {
if (get('stage', 'local') == 'local') {
return get('abspath');
}
return get('deploy_path') . '/shared/';
});
// Define a directory that is shared between deployments
set('shared_dirs', [
'content/uploads',
'content/w3tc-config' // W3 Total Cache wants to write it's own config to disk
]);
// Define web user writeable directories
set('writable_dirs', [
'content/uploads',
'content/w3tc-config' // W3 Total Cache wants to write it's own config to disk
]);
// Use ACL to extend existing permissions
set('writable_mode', 'chgrp'); // chmod, chown, chgrp or acl.
set('writable_chmod_mode', '775');
// Default to only shallow clone
set('git_recursive', false);
// Set apache config options
set('http_user', function () {
if ($webuser = run("cat /etc/apache2/envvars | grep 'APACHE_RUN_USER'")) {
$www = explode('=', $webuser);
return end($www);
}
return 'nobody';
});
set('http_group', function () {
if ($webgroup = run("cat /etc/apache2/envvars | grep 'APACHE_RUN_GROUP'")) {
$www = explode('=', $webgroup);
return end($www);
}
return 'nobody';
});
// Detect which version of sed is being used on the target
set('bin/sed', function () {
if (!commandExist('sed')) {
throw new Exception("sed was not detected in your \$PATH");
}
$sed = run("command -v 'sed' || which 'sed' || type -p 'sed'");
$which_sed = run($sed . ' --version | head -n 1');
if (strstr($which_sed, 'GNU sed')) {
return "$sed -i";
} else {
return "$sed -i ''";
}
});
// Detect which version of curl is being used
set('bin/curl', function () {
if (!commandExist('curl')) {
throw new Exception("curl was not detected in your \$PATH");
}
return run("command -v 'curl' || which 'curl' || type -p 'curl'");
});
// Detect which version of sed is being used on the target
set('bin/wp', function () {
if (!commandExist('wp')) {
throw new Exception("wp-cli was not detected in your \$PATH");
}
return run("command -v 'wp' || which 'wp' || type -p 'wp'");
});
// Set WP bin to local path
set('bin/wpl', function () {
return runLocally("command -v 'wp' || which 'wp' || type -p 'wp'");
});
// Detect which version of npm is being used on the target
set('bin/npm', function () {
if (!commandExist('npm')) {
throw new Exception("npm was not detected in your \$PATH");
}
return run("command -v 'npm' || which 'npm' || type -p 'npm'");
});
// Returns Composer binary path if found
set('bin/composer', function () {
if (!commandExist('composer')) {
throw new Exception("composer was not detected in your \$PATH");
}
return run("command -v 'composer' || which 'composer' || type -p 'composer'");
});
// Every release should be datetime stamped
set('release_name', date('YmdHis'));
// Try to use git cache where applicable
set('git_cache', true);
// Disable usage data
set('allow_anonymous_stats', false);
// Ensure permissions are set recursively
set('writable_recursive', true);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Tasks
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
task('deploy', [
'deploy:info',
'deploy:lock',
'deploy:release',
'deploy:update_code',
'deploy:shared',
'deploy:clear_paths',
'deploy:symlink',
'deploy:unlock',
'cleanup',
'success'
])->desc('Deploy your project');
fail('deploy', 'deploy:unlock');
/**
* Log the deployment info into a revisions file in the deployment path
*/
task('signoff', function () {
$signoff = 'Branch ({{branch}}) deployed by ({{user}}) for release ({{release_name}})';
cd('{{deploy_path}}');
run('touch revisions.log');
run('echo "' . $signoff . '" >> revisions.log');
writeln('<info>' . $signoff . '</info>');
})->setPrivate();
/**
* Helpful debug task
*/
task('debug:info', function () {
writeln('Current user: <info>' . get('user') . '</info>');
writeln('Current branch: <info>' . get('branch') . '</info>');
writeln('Supports TTY: <info>' . (get('allow_input') ? 'Yes' : 'No') . '</info>');
writeln(
'Binaries:
<comment>wp</comment>: ' .
get('bin/wp') .
'
<comment>curl</comment>: ' .
get('bin/curl') .
'
<comment>npm</comment>: ' .
get('bin/npm') .
'
<comment>sed</comment>: ' .
get('bin/sed') .
'
<comment>composer</comment>: ' .
get('bin/composer') .
'
<comment>php</comment>: ' .
get('bin/php') .
'
<comment>git</comment>: ' .
get('bin/git') .
'
<comment>symlink</comment>: ' .
get('bin/symlink')
);
});
after('cleanup', 'signoff');
$autoload = array_diff(scandir(realpath(__DIR__) . '/config/tasks/', SCANDIR_SORT_ASCENDING), ['..', '.', '.DS_Store']);
if (!empty($autoload)) {
array_map(function ($task) {
require_once realpath(__DIR__) . '/config/tasks/' . $task;
}, $autoload);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// Hide uncommon tasks from the CLI
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
task('deploy:clear_paths')->setPrivate();
task('deploy:copy_dirs')->setPrivate();
task('deploy:prepare')->setPrivate();
task('deploy:release')->setPrivate();
task('deploy:shared')->setPrivate();
task('deploy:symlink')->setPrivate();
task('deploy:update_code')->setPrivate();
task('deploy:vendors')->setPrivate();
task('deploy:writable')->setPrivate();
task('cleanup')->setPrivate();