feat: 添加自动化部署脚本、Webhook处理器与TDD测试套件
这个提交包含在:
49
public/webhook.php
普通文件
49
public/webhook.php
普通文件
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* GitCode Webhook 自动部署入口
|
||||
*/
|
||||
|
||||
// 响应头设置
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
// Webhook 密钥 Token
|
||||
$secret = 'nahaichuan_deploy_webhook_2026';
|
||||
|
||||
// 项目根目录与部署脚本
|
||||
$rootDir = dirname(__DIR__);
|
||||
$deployScript = $rootDir . '/deploy.sh';
|
||||
$logFile = $rootDir . '/runtime/webhook.log';
|
||||
|
||||
// 加载核心处理类
|
||||
require_once $rootDir . '/application/common/library/WebhookHandler.php';
|
||||
|
||||
$deployExecutor = function (array $payload) use ($deployScript, $rootDir) {
|
||||
$branch = str_replace('refs/heads/', '', $payload['ref'] ?? 'master');
|
||||
// 用 bash 执行部署脚本并捕获输出
|
||||
$cmd = "bash " . escapeshellarg($deployScript) . " " . escapeshellarg($branch) . " 2>&1";
|
||||
$output = shell_exec($cmd);
|
||||
return trim($output ?? '');
|
||||
};
|
||||
|
||||
$handler = new \app\common\library\WebhookHandler($secret, 'master', $deployExecutor);
|
||||
$rawBody = file_get_contents('php://input') ?: '';
|
||||
$result = $handler->handle($_SERVER, $_GET, $rawBody);
|
||||
|
||||
// 记录日志
|
||||
$date = date('Y-m-d H:i:s');
|
||||
$ip = $_SERVER['REMOTE_ADDR'] ?? 'UNKNOWN';
|
||||
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
|
||||
$status = $result['data']['status'] ?? 'unknown';
|
||||
$msg = $result['data']['message'] ?? '';
|
||||
$out = $result['data']['output'] ?? '';
|
||||
|
||||
$logEntry = "[{$date}] [{$ip}] {$method} => Status: {$status}, Msg: {$msg}";
|
||||
if (!empty($out)) {
|
||||
$logEntry .= "\nDeploy Output:\n" . $out;
|
||||
}
|
||||
$logEntry .= "\n" . str_repeat('-', 50) . "\n";
|
||||
|
||||
@file_put_contents($logFile, $logEntry, FILE_APPEND | LOCK_EX);
|
||||
|
||||
http_response_code($result['code']);
|
||||
echo json_encode($result['data'], JSON_UNESCAPED_UNICODE);
|
||||
在新工单中引用
屏蔽一个用户