File: //proc/660475/root/www/wwwroot/nasuhang.com/webhook_pull.php
<?php
/**
* Git Webhook 自动拉取脚本
* 支持Gitee/GitHub/GitLab等平台的Webhook触发自动拉取最新代码
*/
// 配置项 - 请根据实际情况修改
$config = [
// 安全秘钥:Webhook请求时需要携带此参数验证身份,防止恶意触发
'secret' => 'c8cbe9b5d41cde0cf5c9cc16f5f6d401',
// 本地仓库绝对路径(已修正为服务器实际站点路径)
'repo_path' => '/www/wwwroot/hst.aizyu.cn',
// 拉取的分支名
'branch' => 'master',
// 日志文件路径(修改为站点目录下,避免权限问题)
'log_file' => '/www/wwwroot/hst.aizyu.cn/webhook_pull.log',
// 是否开启调试模式(开启会输出详细执行信息)
'debug' => true
];
// 日志记录函数
function writeLog($message, $level = 'INFO') {
global $config;
$logDir = dirname($config['log_file']);
if (!is_dir($logDir)) {
mkdir($logDir, 0755, true);
}
$logMsg = sprintf("[%s] [%s] %s\n", date('Y-m-d H:i:s'), $level, $message);
file_put_contents($config['log_file'], $logMsg, FILE_APPEND | LOCK_EX);
if ($config['debug']) {
echo $logMsg . "<br>";
}
}
// 1. 安全验证
$requestToken = $_GET['token'] ?? ($_POST['token'] ?? '');
if (empty($requestToken) || $requestToken !== $config['secret']) {
writeLog('非法请求:Token验证失败', 'ERROR');
http_response_code(403);
exit('Access Denied: Invalid Token');
}
// 2. 验证仓库目录存在
if (!is_dir($config['repo_path']) || !is_dir($config['repo_path'] . '/.git')) {
writeLog('错误:仓库目录不存在或不是有效的Git仓库,当前路径:' . $config['repo_path'], 'ERROR');
http_response_code(500);
exit('Error: Invalid Git Repository Path');
}
// 3. 执行Git拉取命令
writeLog('开始执行自动拉取操作,分支:' . $config['branch']);
$command = sprintf('cd %s && git pull origin %s 2>&1', escapeshellarg($config['repo_path']), escapeshellarg($config['branch']));
// 执行命令并获取输出
$output = [];
$exitCode = 0;
exec($command, $output, $exitCode);
// 记录执行结果
$outputStr = implode("\n", $output);
writeLog('命令执行输出:' . $outputStr);
if ($exitCode === 0) {
writeLog('✅ 代码拉取成功', 'SUCCESS');
exit('OK: Pull completed successfully');
} else {
writeLog('❌ 代码拉取失败,退出码:' . $exitCode, 'ERROR');
http_response_code(500);
exit('Error: Pull failed, check log for details');
}
?>