-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.php
306 lines (255 loc) · 7.35 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
<?php
/**
* Standalone GitDeploy Script
* This script is largly based on https://github.com/markomarkovic/simple-php-git-deploy/tree/1.3.1 version 1.3.1 but modified for my usecase
*
* @copyright Copyright (C) 2024 Tobias Zulauf All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
*/
/**
* You have to configure the script using `deploy-config.php` file.
*
* Rename `deploy-config.example.php` to `deploy-config.php` and edit the
* configuration options there.
*/
$configFile = basename(__FILE__, '.php') . '-config.php';
// Check whether the config file exists and load it.
if (file_exists($configFile))
{
define('CONFIG_FILE', $configFile);
require_once CONFIG_FILE;
}
// When the config file has not been found -> Exit
if (!defined('CONFIG_FILE'))
{
echo 'Configuration file not found! Please create the "' . $configFile . '" file based on the deploy-config.example.php';
}
if (!isset($_SERVER['HTTP_X_HUB_SIGNATURE_256']))
{
//echo "HTTP header 'X-Hub-Signature' is missing";
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403);
header('Content-type: application/json');
echo json_encode(['status' => false]);
}
if (!extension_loaded('hash'))
{
//echo "Missing 'hash' extension to check the secret code validity.";
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403);
header('Content-type: application/json');
echo json_encode(['status' => false]);
}
// Check the hash algo used
list($algo, $hash) = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE_256']) + array('', '');
if (!in_array($algo, hash_algos(), true))
{
//echo "Hash algorithm '$algo' is not supported.";
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403);
header('Content-type: application/json');
echo json_encode(['status' => false]);
}
$rawPost = file_get_contents('php://input');
if ($hash !== hash_hmac($algo, $rawPost, SECRET_ACCESS_TOKEN))
{
//echo 'Hook secret does not match';
header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden', true, 403);
header('Content-type: application/json');
echo json_encode(['status' => false]);
}
// Check if the required programs are available
$requiredBinaries = array('git', 'rsync');
if (defined('BACKUP_DIR') && BACKUP_DIR !== false)
{
$requiredBinaries[] = 'tar';
if (!is_dir(BACKUP_DIR) || !is_writable(BACKUP_DIR))
{
//echo sprintf('BACKUP_DIR: `%s` does not exists or is not writeable.', BACKUP_DIR);
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
header('Content-type: application/json');
echo json_encode(['status' => false]);
}
}
if (defined('USE_COMPOSER') && USE_COMPOSER === true)
{
$requiredBinaries[] = 'composer --no-ansi';
}
foreach ($requiredBinaries as $command)
{
$path = trim(shell_exec('which '. $command));
if ($path === '')
{
//echo sprintf('<b>%s</b> not available. It needs to be installed on the server for this script to work.', $command);
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
header('Content-type: application/json');
echo json_encode(['status' => false]);
}
}
// The commands
$commands = [];
// ========================================[ Pre-Deployment steps ]===
if (!is_dir(TMP_DIR))
{
// Clone the repository into the TMP_DIR
$commands[] = sprintf(
'git clone --depth=1 --branch %s %s %s',
BRANCH,
REMOTE_REPOSITORY,
TMP_DIR
);
}
else
{
// TMP_DIR exists and hopefully already contains the correct remote origin
// so we'll fetch the changes and reset the contents.
$commands[] = sprintf(
'git --git-dir="%s.git" --work-tree="%s" fetch --tags origin %s',
TMP_DIR,
TMP_DIR,
BRANCH
);
$commands[] = sprintf(
'git --git-dir="%s.git" --work-tree="%s" reset --hard FETCH_HEAD',
TMP_DIR,
TMP_DIR
);
}
// Describe the deployed version
if (defined('VERSION_FILE') && VERSION_FILE !== '')
{
$commands[] = sprintf(
'git --git-dir="%s.git" --work-tree="%s" describe --always > %s',
TMP_DIR,
TMP_DIR,
VERSION_FILE
);
}
// Backup the TARGET_DIR
// without the BACKUP_DIR for the case when it's inside the TARGET_DIR
if (defined('BACKUP_DIR') && BACKUP_DIR !== false)
{
$commands[] = sprintf(
"tar --exclude='%s*' -czf %s/%s-%s-%s.tar.gz %s*",
BACKUP_DIR,
BACKUP_DIR,
basename(TARGET_DIR),
md5(TARGET_DIR),
date('YmdHis'),
TARGET_DIR // We're backing up this directory into BACKUP_DIR
);
}
// Invoke composer
if (defined('USE_COMPOSER') && USE_COMPOSER === true)
{
$commands[] = sprintf(
'composer --no-ansi --no-interaction --no-progress --working-dir=%s install %s',
TMP_DIR,
(defined('COMPOSER_OPTIONS')) ? COMPOSER_OPTIONS : ''
);
if (defined('COMPOSER_HOME') && is_dir(COMPOSER_HOME))
{
putenv('COMPOSER_HOME=' . COMPOSER_HOME);
}
}
// ==================================================[ Deployment ]===
// Compile exclude parameters
$exclude = '';
foreach (unserialize(EXCLUDE) as $exc)
{
$exclude .= ' --exclude=' . $exc;
}
// Deployment command
$commands[] = sprintf(
'rsync -rltgoDzvO %s %s %s %s',
TMP_DIR,
TARGET_DIR,
(DELETE_FILES) ? '--delete-after' : '',
$exclude
);
// =======================================[ Post-Deployment steps ]===
// Remove the TMP_DIR (depends on CLEAN_UP)
if (CLEAN_UP)
{
$commands['cleanup'] = sprintf(
'rm -rf %s',
TMP_DIR
);
}
// =======================================[ Run the command steps ]===
// By default here is no error
$error = false;
// Set the message when there is no error message
$betreff = sprintf(
'Git Deployment successful on %s',
$_SERVER['HTTP_HOST'],
);
$nachricht = '';
// Loop via the commands
foreach ($commands as $command)
{
// Reset the time limit for each command
set_time_limit(TIME_LIMIT);
if (file_exists(TMP_DIR) && is_dir(TMP_DIR))
{
// Ensure that we're in the right directory
chdir(TMP_DIR);
}
$commandResult = [];
// Execute the command
exec($command . ' 2>&1', $commandResult, $returnCode);
// Error handling and cleanup
if ($returnCode !== 0)
{
// There is an error
$error = true;
if (CLEAN_UP)
{
shell_exec($commands['cleanup']);
}
$betreff = sprintf(
'Git Deployment error on %s',
$_SERVER['HTTP_HOST'],
);
$nachricht = 'Last error message:/n/n';
foreach ($commandResult as $key => $value)
{
$nachricht .= $value . '/n/n';
}
break;
}
}
if (EMAIL_RECEIVER && $error)
{
$empfaenger = EMAIL_RECEIVER;
$header = [
'From' => EMAIL_SENDER,
'X-Mailer' => 'PHP/' . phpversion()
];
mail($empfaenger, $betreff, $nachricht, $header);
}
if (TELEGRAM_BOT_TOKEN && TELEGRAM_BOT_CHATID)
{
$nachricht = !empty($nachricht) ? PHP_EOL . PHP_EOL . $nachricht : '';
$params = [
'chat_id' => TELEGRAM_BOT_CHATID,
'text' => $betreff . $nachricht,
'disable_web_page_preview' => 'true',
'parse_mode' => 'markdown',
];
$ch = curl_init('https://api.telegram.org/bot' . TELEGRAM_BOT_TOKEN . '/sendMessage');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
}
// When there is an error set the status to false
if ($error)
{
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
header('Content-type: application/json');
echo json_encode(['status' => false]);
}
// Send status
header('Content-type: application/json');
echo json_encode(['status' => true]);