-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-receive
83 lines (63 loc) · 2.17 KB
/
post-receive
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
#!/usr/bin/php
<?php
use Symfony\Component\Process\Process;
date_default_timezone_set('Europe/Prague');
require __DIR__ . '/inc/System.php';
require __DIR__ . '/inc/Lock.php';
require __DIR__ . '/inc/Refs.php';
require __DIR__ . '/inc/vendor/autoload.php';
Lock::registerAutorelease();
$last = 0;
foreach (glob('/srv/deploy/api.webexpo.net_*') as $dir)
{
$index = substr($dir, strlen('/srv/deploy/api.webexpo.net_'));
if ($index > $last)
{
$last = $index;
}
}
$path = '/srv/deploy/api.webexpo.net_' . ($last + 1);
mkdir($path);
echo "Checking-out to $path\n";
$ref = Refs::getRef();
run("git archive $ref | tar -xC " . escapeshellarg($path));
echo "Coping previous config.local.neon\n";
run('cp /srv/api.webexpo.net/app/config/config.local.neon ' . escapeshellarg("$path/app/config/"));
echo "Updating privileges\n";
run('chmod -R ug+rwx,o= ' . escapeshellarg($path));
run('chgrp -R webexpo ' . escapeshellarg($path));
echo "Writing deploy.txt\n";
$out = "Deployed on " . date('Y-m-d H:i:s') . "\n";
$out .= Refs::getCommit() . "\n";
run('echo ' . escapeshellarg($out) . ' > ' . escapeshellarg("$path/www/deploy.txt"));
echo "Blessing this build\n";
sleep(1);
// TODO backup database
echo "Running migrations\n";
run('php ' . escapeshellarg("$path/www/index.php") . ' migrations:migrate');
echo "Swapping symlinks\n";
run('unlink /srv/api.webexpo.net && ln -s ' . escapeshellarg($path) . ' /srv/api.webexpo.net');
echo "Reloading worker\n";
run('sudo supervisorctl restart webexpo:worker-00');
echo "Reloading php5-fpm\n";
run('sudo /etc/init.d/php5-fpm reload');
echo "Flushing redis\n";
run('redis-cli flushall');
echo "Pushing tag to github\n";
$parts = explode('/', Refs::getRef());
$tag = 'deploy-' . array_pop($parts);
run("git tag -f -a $tag -m $tag $ref");
run("git push github $tag -f");
// Flushing fs cache is not required because the freshly
// checked out directory does not have any temp files
// Flushing varnish is not required
echo "\033[1;32mDeploy successful\033[0m\n";
function run($command)
{
$process = new Process($command);
$process->run();
if (!$process->isSuccessful())
{
System::fail($process->getErrorOutput() ?: "Deploy failed on command:\n$command");
}
}