宝塔webhook
1.创建公钥 -3次回车
ssh-keygen -t rsa -C "your@email.com"
2.查看
cat ~/.ssh/id_rsa.pub
3.将密钥加入到对应项目部署公钥
4.宝塔下载webhook
5.填写附件脚本 -最下面
6.Git全局配置和单个仓库的用户名邮箱配置
git config --global user.name “xxx”
git config --global user.email “xxx”
7.服务器拉取
git clone git@gitee.com:xxx/xxx.git
#!/bin/bash
echo ""
输出当前时间
date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"
echo "Start"
判断宝塔WebHook参数是否存在
if [ ! -n "syoa" ];
then
echo "param参数错误"
echo "End"
exit
fi
git项目路径
gitPath="/www/wwwroot/xxx/xxx"
git 网址
gitHttp="https://gitee.com/xxx/xxx.git"
echo "Web站点路径:$gitPath"
判断项目路径是否存在
if [ -d "$gitPath" ]; then
cd $gitPath
判断是否存在git目录
if [ ! -d ".git" ]; then
echo "在该目录下克隆 git"
git clone $gitHttp gittemp
mv gittemp/.git .
rm -rf gittemp
fi
拉取最新的项目文件
git reset --hard origin/master
git pull
设置目录权限
chown -R www:www $gitPath
echo "End"
exit
else
echo "该项目路径不存在"
echo "End"
exit
fi
webhooks
webhook.php
<?php
#yum install -y git
#git config --global user.name “xxx”
#git config --global user.email “xxx@qq.com”
#sudo -u www ssh-keygen -t rsa -C "xxx@qq.com"
#cat /home/www/.ssh/id_rsa.pub
#sudo -u www ssh -T git@gitee.com
#sudo -u www git clone git@gitee.xxx/xxx.git
#cd /www/wwwroot/
<?php
$json = file_get_contents("php://input");
$data = json_decode($json,true);
$token='xxx';
if($data['password']!=$token){
exit('error request');
}
if (isset($data['ref']) && $data['total_commits_count']>0) {
$res = PHP_EOL."pull start -----------".PHP_EOL;
$res .= shell_exec("cd /www/wwwroot/xxx/xxx&& git pull 2>&1");
$res_log = '------------------------------------------------------------'.PHP_EOL;
$res_log .= $data['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $data['repository']['name'] . '项目的' . $data['ref'] . '分支push了' . $data['total_commits_count'] . '个commit:'.$data['commits']['message'];
$res_log .= $res.PHP_EOL;
$res_log .= "pull end -----------".PHP_EOL;
print_r($res_log);
}
?>