-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.php
110 lines (95 loc) · 2.98 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
<?php
echo "Deployment script\n";
echo "--------------------------------\n";
if (!file_exists('./.git')) {
die("Error: This deployment script should be executed only from the project root. Terminating now.\n");
}
if (!isset($argv[1]) || !in_array($argv[1], ['production', 'development'])) {
die("Error: The deployment environment is empty or invalid. Allowed values: production, development (dummy data + not minified assets)\n");
}
if (file_exists('.deployment-in-progress')) {
die("Error: There was already deployment outgoing, fix issues and remove the ./.deployment-in-progress file.\n");
}
$environment = $argv[1];
$version = $argv[2] ?? null;
touch('.deployment-in-progress');
// Fetch and pull new sources
$state = 0;
system('git fetch --prune', $state);
if ($state !== 0) {
unlink('.deployment-in-progress');
die("Error: Git fetch failed\n");
}
$state = 0;
if ($version) {
echo sprintf("Checking out %s\n", $version);
system(sprintf('git checkout %s',$version), $state);
} else {
system('git merge --ff-only `git rev-parse --abbrev-ref --symbolic-full-name @{u}`', $state);
}
if ($state !== 0) {
unlink('.deployment-in-progress');
die("Error: Git checkout failed\n");
}
// Update PHP dependencies
$state = 0;
system('php composer.phar install', $state);
if ($state !== 0) {
die("Error: Update of PHP dependencies via Composer failed.\n");
}
// Update Node.js dependencies
$state = 0;
system('npm install', $state);
if ($state !== 0) {
die("Error: Update of Node.js dependencies via node failed.\n");
}
// Build frontend
$state = 0;
system('npm run build', $state);
if ($state !== 0) {
die("Error: Building frontend has failed.\n");
}
// Remove Nette cache
$state = 0;
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { // Windows systems
system('rmdir /s/q temp\cache', $state);
system('mkdir temp\cache');
} else {
system('rm -rf temp/cache/*', $state); // Linux systems
}
if ($state !== 0) {
die("Error: Nette cache could not been deleted, please do it manually.\n");
}
// Regenerate files via Gulp
$state = 0;
if ($environment === 'production') {
system('gulp production', $state);
} else {
system('gulp default', $state);
}
if ($state !== 0) {
die("Error: Update of production files could not been finished.\n");
}
// Create robots.txt according to environment
if ($environment === 'production') {
file_put_contents(__DIR__ . '/www/robots.txt', "User-agent: *\nDisallow:\n");
} else {
file_put_contents(__DIR__ . '/www/robots.txt', "User-agent: *\nDisallow:/\n");
}
// Run database migrations
$state = 0;
if ($environment === 'production') {
system('php www/index.php migrations:continue --production', $state);
} else {
system('php www/index.php migrations:continue', $state);
}
if ($state !== 0) {
die("Error: Database migrations failed.\n");
}
if (file_exists('.deployment-in-progress')) {
unlink('.deployment-in-progress');
}
if ($environment === 'development') {
echo "Info: If you want to automatically rebuild assets please run $ gulp development\n";
}
echo "Info: Finished successfully\n";