knowledge/deploy/start.php
2023-07-04 14:38:23 +08:00

92 lines
2.7 KiB
PHP

<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Dotenv\Dotenv;
require_once __DIR__ . '/vendor/autoload.php';
// 加载.env文件
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();
$worker = new Worker('http://0.0.0.0:8787');
$worker->onConnect = function ($connection) {
echo "New connection\n";
};
$worker->onMessage = function (TcpConnection $connection, Request $request) {
$work_dir = isset($_ENV['WORK_DIR']) ? $_ENV['WORK_DIR'] : null;
$branch = isset($_ENV['BRANCH']) ? $_ENV['BRANCH'] : null;
$git_url = isset($_ENV['GIT_URL']) ? $_ENV['GIT_URL'] : null;
// var_dump(json_encode($request->post('repository'), JSON_UNESCAPED_UNICODE));
if ($work_dir && $branch && $git_url) {
$repository = $request->post('repository');
if (is_array($repository)) {
$ssh_url = isset($repository['ssh_url']) ? $repository['ssh_url'] : null;
$clone_url = isset($repository['clone_url']) ? $repository['clone_url'] : null;
if ($git_url === $ssh_url || $git_url === $clone_url) {
chdir($work_dir);
shell_exec("sh auto_build.sh");
// shell_exec("git reset --hard FETCH_HEAD");
// shell_exec("yarn install");
// shell_exec("yarn build");
$connection->send("Deployment finished\n");
$connection->close();
}
}
}
// $connection->send($request->post());
// if ($work_dir && $branch) {
// chdir($work_dir);
// exec("git fetch origin $branch");
// exec("git reset --hard FETCH_HEAD");
// exec("yarn install");
// exec("yarn build");
// $connection->send("Deployment finished\n");
// $connection->close();
// }
// 设置工作目录
// $WORK_DIR = "/www/wwwroot/fs.book.wandoubaba.com";
// 设置远程仓库地址
// $REMOTE_REPO = "git@git.wandoubaba.com:wandoubaba/knowledge.git";
// 设置分支名称
// $BRANCH = "master";
// 验证请求是否合法
// $secret = "your-webhook-secret";
// $signature = $_SERVER['HTTP_X_HUB_SIGNATURE'] ?? '';
// $body = $data;
// if (!hash_equals('sha1=' . hash_hmac('sha1', $body, $secret), $signature)) {
// $connection->send("Invalid request\n");
// $connection->close();
// return;
// }
// // 切换到工作目录
// chdir($WORK_DIR);
// // 拉取最新代码
// exec("git fetch origin $BRANCH");
// exec("git reset --hard FETCH_HEAD");
// // 执行相关命令
// exec("yarn install");
// exec("yarn build");
// $connection->send("Deployment finished\n");
// $connection->close();
};
// 运行worker
Worker::runAll();