当前位置:网站首页>php+码云 代码钩子自动更新线上代码
php+码云 代码钩子自动更新线上代码
2022-07-23 05:40:00 【我王十一呀】
php部分
public function git_push(Request $request){
//git webhook 自动部署脚本
//项目存放物理路径,第一次clone时,必须保证该目录为空
$savePath = "C:/www";
$gitPath = "https://gitee.com/wlq/wlq";//代码仓库
$name= "[email protected]";//用户仓库邮箱
$email = "[email protected]";//仓库用户名,一般和邮箱一致即可
$password = '这里是密码';
$isCloned = true;//设置是否已经Clone到本地,true:已经clone,直接pull,false:先clone.
//如果已经clone过,则直接拉去代码
if ($isCloned) {
$requestBody = file_get_contents("php://input");
if (empty($requestBody)) {
die('send fail');
}
//解析Git服务器通知过来的JSON信息
$content = json_decode($requestBody, true);
if($content['password'] != $password){
exit('password error');
}
//若是主分支且提交数大于0
if ($content['ref'] == 'refs/heads/master' && $content['total_commits_count'] > 0) {
$res = PHP_EOL . "pull start --------" . PHP_EOL;
$res .= shell_exec("cd {
$savePath} && git pull {
$gitPath}");
$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:';
$res_log .= $res . PHP_EOL;
$res_log .= "pull end --------" . PHP_EOL;
file_put_contents("git-webhook_log.txt", $res_log, FILE_APPEND);//写入日志到log文件中
$path = 'C:\www\test_ks\gitpull.bat';//这个是脚本路径
exec($path);
exit('git pull');
}
} else {
$res = "clone start --------" . PHP_EOL;
//注:在这里需要设置用户邮箱和用户名,不然后面无法拉去代码
$res .= shell_exec("git config --global user.email {
$email}}") . PHP_EOL;
$res .= shell_exec("git config --global user.name {
$name}}") . PHP_EOL;
$res .= shell_exec("git clone {
$gitPath} {
$savePath}") . PHP_EOL;
$res .= "clone end --------" . PHP_EOL;
file_put_contents("git-webhook_log.txt", $res, FILE_APPEND);//写入日志到log文件中
exit('clone success');
}
}
windows的bat文件
gitpull.bat
rem 调用git命令,如果没有找到git命令,说明环境变量未配置
rem 将git改成git安装目录下的git.exe也可以实现
git pull
pause
码云
url 就是那个方法对应的接口地址了
webhook密码就是 上面方法的 $password
这个可以接入机器人 比如企业微信的机器人 钉钉的机器人之类的 很多文档
边栏推荐
- py程序可以运行,但打包出的exe运行提示错误:加载“cv2”二进制扩展时检测到递归。请检查OpenCV安装。
- Using pytorch to realize the flower recognition classifier based on VGg 19 pre training model, the accuracy reaches 97%
- MySQL之账号管理&&四大引擎&&建库建表
- js的继承方式
- 大厂面试机器学习算法(6)时间序列分析
- 视图的使用
- shell/sh/bash的区别和基本操作
- 【Pyautogui学习】屏幕坐标、鼠标滚动
- 构造函数,原型链,instanceOf
- Sorting out common SQL interview questions and answers
猜你喜欢

C语言调试常见错误——简答
![[部署]presto-server-0.261.tar.gz的集群部署和启动](/img/37/1185b2321b003a7793c8c37891008c.png)
[部署]presto-server-0.261.tar.gz的集群部署和启动
D2DEngine食用教程(2)———绘制图像

Clear the buffer with getchar (strongly recommended, C language is error prone, typical)

使用el-table懒加载树形表格时的注意点

Oracle创建数据库“监听程序未启动或数据库服务未注册”错误处理

机器学习零散笔记:一些概念和注意
D2dengine edible tutorial (2) -- drawing images

plsql创建Oracle数据库报错:使用Database Control配置数据库时,要求在当前Oracle主目录中配置监听程序 必须运行Netca以配置监听程序,然后才能继续。或者

The problem that GBK codec cannot decode may be caused by the file name
随机推荐
Request data acquisition and response
Web Component-自定義元素的生命周期
【Pyautogui学习】屏幕坐标、鼠标滚动
Large factory interview machine learning algorithm (0): Feature Engineering | data preprocessing
MySQL之账号管理&&四大引擎&&建库建表
plsql创建Oracle数据库报错:使用Database Control配置数据库时,要求在当前Oracle主目录中配置监听程序 必须运行Netca以配置监听程序,然后才能继续。或者
Matrix vector derivation in machine learning
3. Threads in flask
D2DEngine食用教程(1)———最简单的程序
Niuke question brushing record -- MySQL
Anti shake and throttling of JS
动态设置卡片的主题色
Five methods to prevent over fitting of neural network
Web server failed to start. Port 8080 was already in use.
systemctl-service服务添加环境变量及模板
js的call、apply、bind
[untitled]
手写Promise.resolve,Promise.reject, Promise.all
C语言中的流氓goto语句
Hyperlink de underlined code