-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdeploy.php
More file actions
266 lines (246 loc) · 9.04 KB
/
deploy.php
File metadata and controls
266 lines (246 loc) · 9.04 KB
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
<?php
namespace Deployer;
$startTime = microtime();
require 'recipe/laravel.php';
set('application', 'codex.radic.ninja'); // Project name
set('repository', 'github.com:codex-project/codex.radic.ninja'); // Project repository
set('git_tty', true); // [Optional] Allocate tty for git clone. Default value is false.
//add('shared_files', []); // Shared files/dirs between deploys
//add('shared_dirs', []); // Shared files/dirs between deploys
//add('writable_dirs', []); // Writable dirs by web server
set('writable_dirs', array_merge(get('writable_dirs', []), [
'codex',
'codex/phpdoc',
'codex/git',
]));
set('allow_anonymous_stats', false);
set('branch', 'master');
set('default_stage', 'staging');
set('keep_releases', 30);
set('http_user', 'radic-ninja');
set('writable_mode', 'chmod');
set('bin/composer', '~/composer.phar');
//set('releases_list', function () {// return explode("\n", run('ls -dt {{deploy_path}}/releases/*'));//});
host('codex-staging.radic.ninja')
->port(60000)
->user('radic-ninja')
->identityFile('~/.ssh/id_rsa')
->stage('staging')
->set('deploy_path', '~/codex-staging.radic.ninja')// ->set('branch', 'develop')
;
host('codex.radic.ninja')
->port(60000)
->user('radic-ninja')
->identityFile('~/.ssh/id_rsa')
->stage('production')
->set('deploy_path', '~/codex.radic.ninja');
task('confirm', function () {
if ( ! askConfirmation('Are you sure you want to deploy to production?')) {
write('Ok, quitting.');
die;
}
})->onStage('production');
task('test', function () {
writeln('Hello world');
writeln("release_path: {{release_path}}");
});
task('dump', function () {
$result = run('pwd');
writeln("Current dir: $result");
writeln("writable_mode: {{writable_mode}}");
});
task('artisan:vendor:publish:public', function () {
run('{{bin/php}} {{release_path}}/artisan vendor:publish --tag public --force');
});
task('artisan:codex:git:sync', function () {
run('{{bin/php}} {{release_path}}/artisan codex:git:sync --all --force');
});
task('artisan:codex:phpdoc:clear', function () {
run('{{bin/php}} {{release_path}}/artisan codex:phpdoc:clear --all');
});
task('artisan:codex:phpdoc:generate', function () {
run('{{bin/php}} {{release_path}}/artisan codex:phpdoc:generate --all --force');
});
task('composer:clear', function () {
run('{{bin/php}} {{release_path}}/artisan cache:clear');
run('{{bin/php}} {{release_path}}/artisan view:clear');
run('{{bin/php}} {{release_path}}/artisan config:clear');
run('{{bin/php}} {{release_path}}/artisan route:clear');
});
task('composer:optimize', function () {
run('{{bin/php}} {{release_path}}/artisan route:cache');
run('{{bin/php}} {{release_path}}/artisan view:cache');
run('{{bin/php}} {{release_path}}/artisan config:cache');
});
task('artisan:checks', function () {
run('{{bin/php}} {{release_path}}/artisan lighthouse:validate-schema');
run('{{bin/php}} {{release_path}}/artisan self-diagnosis');
});
$envPresets = [
'development' => [
'APP_ENV' => 'local',
'APP_DEBUG' => 'true',
'RESPONSE_CACHE_ENABLED' => 'false',
'CODEX_CACHE_ENABLE' => 'false',
],
'production' => [
'APP_ENV' => 'production',
'APP_DEBUG' => 'false',
'RESPONSE_CACHE_ENABLED' => 'true',
'CODEX_CACHE_ENABLE' => 'true',
]
];
foreach($envPresets as $preset => $vars){
task('set-env:' . $preset, function() use ($vars){
foreach ($vars as $k => $v) {
run("{{bin/php}} {{release_path}}/artisan dotenv:set-key {$k} {$v}");
}
});
}
task('deploy', [
'deploy:info',
'deploy:prepare',
'deploy:lock',
'deploy:release',
'deploy:update_code',
'deploy:shared',
'deploy:vendors',
'deploy:writable',
'artisan:clear',
'artisan:vendor:publish:public',
'artisan:codex:git:sync',
'artisan:codex:phpdoc:clear',
'artisan:codex:phpdoc:generate',
'set-env:production',
'artisan:storage:link',
'artisan:optimize',
'artisan:checks',
'deploy:symlink',
'deploy:unlock',
'cleanup',
]);
// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');
// Migrate database before symlink new release.
//before('deploy:symlink', 'artisan:migrate');
task('laravel:logs', function () {
// $result = run('cat {{deploy_path}}/shared/storage/logs/laravel-2019-06-25.log');
$result = run('cat {{deploy_path}}/current/.env');
write($result);
});
$tasks = [
'log-new-deploy' => function () {
run('echo "[' . date('Y-m-d H:i:s') . '] Deploying a new version of the app." >> {{deploy_path}}/deploy.log');
},
'create:release' => function () {
$i = 0;
do {
$releasePath = '{{deploy_path}}/releases/' . date('m_d_H_i_') . $i++;
}
while (run("if [ -d $releasePath ]; then echo exists; fi;") == 'exists');
run("mkdir $releasePath");
set('release_path', $releasePath);
writeln("Release path: $releasePath");
},
'update:code' => function () {
run("git clone -b {{branch}} -q --depth 1 {{repository}} {{release_path}}");
},
'create:symlinks' => function () {
// Link .env.
run("ln -nfs {{deploy_path}}/static/.env {{release_path}}");
// Link storage.
run("ln -nfs {{deploy_path}}/static/storage {{release_path}}");
// Link vendor.
run("ln -nfs {{deploy_path}}/static/vendor {{release_path}}");
},
'update:vendors' => function () {
cd('{{release_path}}');
writeln('<info> Updating npm</info>');
run('npm-cache install npm --no-dev');
writeln('<info> Updating composer</info>');
run('composer install --no-dev');
},
'update:permissions' => function () {
run('chmod -R a+w {{release_path}}/bootstrap/cache');
run('chown -R {{user}}:{{user}} {{release_path}} -h');
},
'compile:assets' => function () {
cd('{{release_path}}');
run('npm run prod');
run('rm -rf {{release_path}}/node_modules');
},
'optimize' => function () {
run('php {{release_path}}/artisan cache:clear');
run('php {{release_path}}/artisan view:clear');
run('php {{release_path}}/artisan config:clear');
run('php {{release_path}}/artisan config:cache');
},
'site:down' => function () {
writeln(sprintf('<info>%s</info>', run('php {{release_path}}/artisan down')));
},
'migrate:db' => function () {
writeln(sprintf(' <info>%s</info>', run('php {{release_path}}/artisan migrate --force --no-interaction')));
},
'update:release_symlink' => function () {
run('cd {{deploy_path}} && if [ -e live ]; then rm live; fi');
run('cd {{deploy_path}} && if [ -h live ]; then rm live; fi');
run('ln -nfs {{release_path}} {{deploy_path}}/live');
},
'site:up' => function () {
writeln(sprintf(' <info>%s</info>', run('php {{deploy_path}}/live/artisan up')));
},
'clear:opcache' => function () {
run('cachetool opcache:reset --fcgi=/var/run/php/php7.2-fpm.sock');
},
'cleanup' => function () {
$releases = get('releases_list');
$keep = get('keep_releases');
while ($keep-- > 0) {
array_shift($releases);
}
foreach ($releases as $release) {
run("rm -rf $release");
}
},
'notify:done' => function () use ($startTime) {
$seconds = intval(microtime(true) - $startTime);
$minutes = substr('0' . intval($seconds / 60), -2);
$seconds %= 60;
$seconds = substr('0' . $seconds, -2);
shell_exec("osascript -e 'display notification \"It took: $minutes:$seconds\" with title \"Deploy Finished\"'");
shell_exec('say deployment finished');
},
'rollback' => function () {
$releases = get('releases_list');
if (isset($releases[ 1 ])) {
writeln(sprintf('<error>%s</error>', run('php {{deploy_path}}/live/artisan down')));
$releaseDir = $releases[ 1 ];
run("ln -nfs $releaseDir {{deploy_path}}/live");
run("rm -rf {$releases[0]}");
writeln("Rollback to `{$releases[1]}` release was successful.");
writeln(sprintf(' <error>%s</error>', run("php {{deploy_path}}/live/artisan up")));
} else {
writeln(' <comment>No more releases you can revert to.</comment>');
}
},
'deploy' => [
'confirm',
'create:release',
'update:code',
'create:symlinks',
'update:vendors',
'update:permissions',
'compile:assets',
'optimize',
'site:down',
'migrate:db',
'update:release_symlink',
'site:up',
'clear:opcache',
'cleanup',
'notify:done',
],
];
//foreach ($tasks as $name => $task) {
// task($name, $task);
//}