当前位置:网站首页>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
这个可以接入机器人 比如企业微信的机器人 钉钉的机器人之类的 很多文档
边栏推荐
- [pytho-flask笔记5]蓝图简单使用
- 解决手动查询Oracle数据库时间格式不正确的问题(DATE类型)
- Matrix vector derivation in machine learning
- The super simple face recognition API can realize face recognition in just a few lines of code
- Transform: translate (-50%, -50%) border problem
- Analysis of flask source code (I) request entry
- 页面实现 “实时数据响应” 的注意事项
- npm init vite-app <project-name> 报错 Install for [‘[email protected]‘] failed with code 1
- Mixed view base class
- Uscd pedestrian anomaly data set user guide | quick download
猜你喜欢

再见if-else

Flex+js realizes that the height of the internal box follows the maximum height

flex+js实现内部盒子高度跟随其中最大的高度

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

Machine learning algorithm for large factory interview (6) time series analysis

Vite X Figma 打造设计师专属的 i18n 插件

MySQL增删改查&&高级查询语句

自定义公式输入框

大厂面试机器学习算法(0):特征工程 | 数据预处理

【C语言】什么是函数?函数的分类和侧重(帮你快速分类和记忆函数)
随机推荐
Keras saves the best model in the training process
【uiautomation】键指令大全(以及三种调用方式)+常用鼠标动作+SendKeys+Inspect学习
TypeScript 高级类型
[pytho-flask筆記5]藍圖簡單使用
flex+js实现内部盒子高度跟随其中最大的高度
[untitled]
First meet flask
手写Promise.resolve,Promise.reject, Promise.all
使用require.context完成本地图片批量导入
[Doris]配置和基本使用contens系统(有时间继续补充内容)
Sorting out common SQL interview questions and answers
js的事件执行机制(Event loop)
D2DEngine食用教程(2)———绘制图像
After the formula in word in WPS is copied, there is a picture
plsql创建Oracle数据库报错:使用Database Control配置数据库时,要求在当前Oracle主目录中配置监听程序 必须运行Netca以配置监听程序,然后才能继续。或者
[untitled]
C语言中的分支和循环语句归属
高阶函数的应用:手写Promise源码(一)
C语言之二分查找法或折半查找法剖析(经典例题,经典解析)
通用视图,序列化器