| 1 | <?php |
| 2 | /** |
| 3 | * Sync sito statico da Forgejo |
| 4 | * Eseguito periodicamente via Scheduled Task Alwaysdata |
| 5 | */ |
| 6 | |
| 7 | define('REPO_API', 'https://forgejo.it/api/v1/repos/UTENTE/REPO/commits?limit=1'); |
| 8 | define('REPO_URL', 'https://forgejo.it/UTENTE/REPO.git'); |
| 9 | define('WWW_PATH', __DIR__ . '/www'); |
| 10 | define('HASH_FILE', __DIR__ . '/last_commit.txt'); |
| 11 | define('EXCLUDE', ['.git', '.gitignore', 'README.md', 'CHANGELOG.md', |
| 12 | 'LICENSE.md', '.forgejo', 'deploy']); |
| 13 | |
| 14 | function log_msg(string $msg): void { |
| 15 | echo '[' . date('Y-m-d H:i:s') . '] ' . $msg . PHP_EOL; |
| 16 | } |
| 17 | |
| 18 | function get_remote_commit(): ?string { |
| 19 | $ch = curl_init(REPO_API); |
| 20 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 21 | curl_setopt($ch, CURLOPT_TIMEOUT, 10); |
| 22 | $response = curl_exec($ch); |
| 23 | curl_close($ch); |
| 24 | |
| 25 | if (!$response) return null; |
| 26 | |
| 27 | $data = json_decode($response, true); |
| 28 | return $data[0]['sha'] ?? null; |
| 29 | } |
| 30 | |
| 31 | function get_local_commit(): ?string { |
| 32 | if (!file_exists(HASH_FILE)) return null; |
| 33 | return trim(file_get_contents(HASH_FILE)); |
| 34 | } |
| 35 | |
| 36 | function update_site(): bool { |
| 37 | $temp = sys_get_temp_dir() . '/site-update'; |
| 38 | |
| 39 | if (is_dir($temp)) { |
| 40 | exec('rm -rf ' . escapeshellarg($temp)); |
| 41 | } |
| 42 | |
| 43 | log_msg('Clone in corso...'); |
| 44 | exec('git clone --depth 1 ' . escapeshellarg(REPO_URL) . ' ' |
| 45 | . escapeshellarg($temp) . ' 2>&1', $output, $code); |
| 46 | |
| 47 | if ($code !== 0) { |
| 48 | log_msg('Errore clone: ' . implode("\n", $output)); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | $copied = 0; |
| 53 | foreach (scandir($temp) as $item) { |
| 54 | if ($item === '.' || $item === '..' || in_array($item, EXCLUDE)) { |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | $src = $temp . '/' . $item; |
| 59 | $dst = WWW_PATH . '/' . $item; |
| 60 | |
| 61 | if (is_dir($dst)) exec('rm -rf ' . escapeshellarg($dst)); |
| 62 | elseif (file_exists($dst)) unlink($dst); |
| 63 | |
| 64 | if (is_dir($src)) exec('cp -r ' . escapeshellarg($src) . ' ' . escapeshellarg($dst)); |
| 65 | else copy($src, $dst); |
| 66 | |
| 67 | $copied++; |
| 68 | } |
| 69 | |
| 70 | exec('rm -rf ' . escapeshellarg($temp)); |
| 71 | log_msg("Completato: $copied elementi copiati"); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | // Main |
| 76 | log_msg('=== Controllo aggiornamenti ==='); |
| 77 | |
| 78 | $remote = get_remote_commit(); |
| 79 | if (!$remote) { |
| 80 | log_msg('Impossibile contattare API Forgejo'); |
| 81 | exit(1); |
| 82 | } |
| 83 | |
| 84 | $local = get_local_commit(); |
| 85 | |
| 86 | if ($remote === $local) { |
| 87 | log_msg('Nessun aggiornamento'); |
| 88 | exit(0); |
| 89 | } |
| 90 | |
| 91 | log_msg('Aggiornamento disponibile...'); |
| 92 | if (update_site()) { |
| 93 | file_put_contents(HASH_FILE, $remote); |
| 94 | log_msg("Hash salvato: $remote"); |
| 95 | } else { |
| 96 | log_msg('Aggiornamento fallito'); |
| 97 | exit(1); |
| 98 | } |
emanuelegori / update-site.php
Last active 4 months ago
Sync Update sito statico da Forgejo. Eseguito periodicamente via Scheduled Task Alwaysdata