1. webhook
1.1.1. 粗暴 master 分支
<?php
//git webhook 自动部署脚本
//项目存放物理路径
$path = "/home/wwwroot/books";
$requestBody =file_get_contents("php://input");
if (empty($requestBody)) {
die('send fail');
}
$content = json_decode($requestBody, true);
if ($content['ref']=='refs/heads/master') {
$res = shell_exec("cd {$path} && git pull 2>&1");//以nginx用户运行
$res_log = '-------------------------'.PHP_EOL;
$res_log .= $content['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name'] . '项目的' . $content['ref'] . '分支push了' . $content['total_commits_count'] . '个commit:' . PHP_EOL;
$res_log .= $res.PHP_EOL;
echo $res_log;
file_put_contents("git-webhook.txt", $res_log, FILE_APPEND);//追加写入
}
?>
1.2. 分支操作
<?php
$rowData = file_get_contents('php://input', 'r');;
$rowData = json_decode($rowData,true);
$output = '';
$sign = $_GET['sign'];
// 只拉取 dev 分支的代码到服务器
if($rowData['ref'] == 'refs/heads/dev'){
if('ims' == $sign){
// 触发拉取代码的脚本
exec('./dev.sh',$output);
}
logg($sign." output:".json_encode($output));
}
logg($sign.':'.$rowData['user_name']." commit to branch:".$rowData['ref']);
function logg($data){
$text = '['.date('Y-m-d H:i:s').'] '.$data."\n";
file_put_contents('./logs/dev_'.date('ym').'.log',$text,FILE_APPEND);
}
1.2.1. dev.sh
#!/bin/bash
cd /www/packages/
git pull origin dev
1.3. 注意事项
git pull 之前要确定你执行的权限 和 Clone的权限一致 否则会失败
php.ini 去除禁用函数 exec shell_exec
脚本权限 要一致