diff --git a/deploy/.env.example b/deploy/.env.example index e8b6758..47bdca3 100644 --- a/deploy/.env.example +++ b/deploy/.env.example @@ -1,2 +1,3 @@ WORK_DIR = "/www/wwwroot/fs.book.wandoubaba.com" -BRANCH = "master" \ No newline at end of file +BRANCH = "master" +GIT_URL = "git@git.wandoubaba.com:wandoubaba/knowledge.git" diff --git a/deploy/start.php b/deploy/start.php index 62b8098..c7149dc 100644 --- a/deploy/start.php +++ b/deploy/start.php @@ -1,4 +1,5 @@ onConnect = function ($connection) { echo "New connection\n"; }; -$worker->onMessage = function(TcpConnection $connection, Request $request) -{ +$worker->onMessage = function (TcpConnection $connection, Request $request) { $work_dir = isset($_ENV['WORK_DIR']) ? $_ENV['WORK_DIR'] : null; $branch = isset($_ENV['BRANCH']) ? $_ENV['BRANCH'] : null; - var_dump(json_encode($request->post('repository'), JSON_UNESCAPED_UNICODE)); + $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("git fetch origin $branch"); + 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"); + // if ($work_dir && $branch) { + // chdir($work_dir); + // exec("git fetch origin $branch"); + // exec("git reset --hard FETCH_HEAD"); - // exec("yarn install"); - // exec("yarn build"); + // exec("yarn install"); + // exec("yarn build"); - // $connection->send("Deployment finished\n"); - // $connection->close(); - } + // $connection->send("Deployment finished\n"); + // $connection->close(); + // } // 设置工作目录 // $WORK_DIR = "/www/wwwroot/fs.book.wandoubaba.com"; @@ -69,4 +88,4 @@ $worker->onMessage = function(TcpConnection $connection, Request $request) }; // 运行worker -Worker::runAll(); \ No newline at end of file +Worker::runAll();