使用nc命令监听端口编写git的web hooks

需求:
git 服务器可以配置 web hooks
希望git服务器能在提交代码后通知开发机更新代码

解决方案:
1.使用nc命令监听5001端口(service.sh)
2.当nc命令接收到请求后, 数据使用管道传给run.php进行处理
3.因为web hooks是http协议请求, run.php解析出path, 然后分类处理

service.sh

#/bin/bash

dir=`dirname $0`
while(( 1 ))
do
     nc -l 5001 -w 1|${dir}/run.php >> ${dir}/q.log; done

run.php


 #!/usr/local/php/bin/php
 <?php

 //配置
 $arr_conf = [
     '/tmall-static' => '/opt/web/t1.sjbly.cn/tmall-static/test_install.sh 2>&1 > /dev/null',
 ];



 //读取数据
 $fp = fopen('php://stdin', 'r');
 if (!$fp) {
     echo 'exit';
     exit;
 }


 $str = fgets($fp, 1024);
 fclose($fp);


 //处理数据
 echo date('[Y-m-d H:i:s]'), $str;
 $arr = explode(' ', $str, 3);

 if (empty($arr[1])) {
     exit;
 }

 $k = $arr[1];

 if (isset($arr_conf[$k])) {
     `{$arr_conf[$k]}`;
     echo date('[Y-m-d H:i:s]'), $k, "\n";
 }

运行

./service.sh &

猜你喜欢

转载自blog.csdn.net/tsxw24/article/details/46930899