Repository navigation
Expand file tree
/
Copy pathpoll-deploy.php
More file actions
executable file
·72 lines (59 loc) · 1.91 KB
/
Copy pathpoll-deploy.php
File metadata and controls
executable file
·72 lines (59 loc) · 1.91 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
#!/usr/bin/env php
<?php
$sha = $argv[1] ?? '';
if (! $sha) {
exit(1);
}
$env = parse_ini_file('/var/www/html/tekitl/.env');
$token = $env['GH_TOKEN'] ?? '';
$repo = 'Baruch4413/tekitl';
$logFile = '/var/log/tekitl-deploy.log';
function logMsg(string $msg): void
{
global $logFile;
file_put_contents($logFile, '['.date('Y-m-d H:i:s').'] '.$msg.PHP_EOL, FILE_APPEND);
}
function checkCi(string $sha, string $token, string $repo): string
{
$url = "https://api.github.com/repos/{$repo}/actions/runs?head_sha={$sha}&per_page=20";
$context = stream_context_create([
'http' => [
'header' => implode("\r\n", [
"Authorization: Bearer {$token}",
'Accept: application/vnd.github+json',
'User-Agent: tekitl-webhook/1.0',
'X-GitHub-Api-Version: 2022-11-28',
]),
'ignore_errors' => true,
],
]);
$json = @file_get_contents($url, false, $context);
if ($json === false) {
return 'api_error';
}
$data = json_decode($json, true);
foreach ($data['workflow_runs'] ?? [] as $run) {
if ($run['name'] === 'tests' && $run['head_sha'] === $sha) {
return $run['status'].'/'.$run['conclusion'];
}
}
return 'not_found';
}
logMsg("=== Polling CI for SHA {$sha} ===");
$maxAttempts = 40; // 40 × 30s = 20 minutes max
for ($i = 1; $i <= $maxAttempts; $i++) {
$status = checkCi($sha, $token, $repo);
logMsg("Attempt {$i}/40: {$status}");
if ($status === 'completed/success') {
logMsg('CI passed — triggering deploy');
passthru('sudo /var/www/html/tekitl/deploy.sh');
exit(0);
}
if (str_starts_with($status, 'completed/')) {
logMsg("CI finished with non-success status ({$status}) — aborting deploy");
exit(1);
}
sleep(30);
}
logMsg("Timed out after 20 minutes waiting for CI on SHA {$sha}");
exit(1);