这里我以laravel框架为例子
git使用小乌龟
服务器,域名都麻烦自行准备
直接进入正题
1.composer下载好框架
新建码云仓库(重点重点重点,仓库为公开,私有仓库有很多的问题)关系到后面webhook问题,我在这里卡了很久,这里只介绍公开仓库
具体怎么创建仓库,怎么开分支,参考这篇文章
https://blog.csdn.net/qq_31164125/article/details/107021386
2.当分支创建之后,提交到仓库之后,分配给项目组成员的应该是分支仓库代码
仓库管理员
git branch dev1 //创建分支
git checkout dev1 //切换分支
git push origin dev1 //推送分支
开发人员操作
git clone -b dev1 "仓库地址"
测试服务器拉取分支代码
git clone -b dev1 "仓库地址"
这个目录代码我们可以做为测试网站。
接下来,我们要把webhook指向这个目录
配置钩子文件,也就是创建一个文件,然后域名指向这个文件,能访问就行
比如我的
http://hook.111.com/hook.php
hook.php
<?php
$json = file_get_contents("php://input");
$data = json_decode($json,true);
// $email = "294932451@qq.com";//用户仓库邮箱
// $username = "";//仓库用户名,一般和邮箱一致即可
// $passwd = '';
if (isset($data['ref']) && $data['total_commits_count']>0) {
$res = PHP_EOL."pull start ---------------------------------------------".PHP_EOL;
if(strpos($data['ref'],'dev1')>0){
$res .= shell_exec("cd /www/wwwroot/bla725/la725 && git pull origin dev1 && cd .git && chown -R www:www objects 2<&1");
}
elseif (strpos($data['ref'],'master')>0) {
$res .= shell_exec("cd /www/wwwroot/la725 && git pull origin master && cd .git && chown -R www:www objects 2<&1 ");
}else{
}
$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;
file_put_contents("/www/wwwroot/hook.weilinit.com/".date('Y-m-d',time()).".txt", $res_log, FILE_APPEND);//写入日志到log文件中
}
//git webhook 自动部署脚本
//项目存放物理路径,第一次clone时,必须保证该目录为空
// $savePath = "/www/wwwroot/testweb/";
// $gitPath = "https://gitee.com/channel_haixia/testhooks.git";//代码仓库
// $email = "294932451@qq.com";//用户仓库邮箱
// $name = "channel_haixia";//仓库用户名,一般和邮箱一致即可
// $isClone = false;//设置是否已经Clone到本地,true:已经clone,直接pull,false:先clone.
// //如果已经clone过,则直接拉去代码
// if ($isClone) {
// $requestBody = file_get_contents("php://input");
// if (empty($requestBody)) {
// die('send fail');
// }
// //解析Git服务器通知过来的JSON信息
// $content = json_decode($requestBody, true);
// //若是主分支且提交数大于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("/www/wwwroot/hook.weilinit.com/git-webhook_log.txt", $res_log, FILE_APPEND);//写入日志到log文件中
// }
// }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("/www/wwwroot/hook.weilinit.com/git-webhook_log1.txt", $res, FILE_APPEND);//写入日志到log文件中
// }
这样我们的代码就能自动更新到测试服务网站了。
当开发到一个阶段完成之后,我们就要把分支代码合并到主干,具体操作
git branch //查看自己所在的分支是哪个
//分支合并主干
git checkout dev1 //切换分支
git pull origin dev1 //拉取分支更新
git checkout master //回到主干
git merge dev1 //合并分支
git push origin master //把代码提交到主干
//主干合并分支反之亦然
遇到问题
git error:insufficient permission for adding an object to repository database ./object 权限问题怎么解决
cd project/.git
chown -R www:www objects
chmod -R g+rwX objects
右键主干。找到合并。
如图选择,然后就会合并,有冲突解决冲突。
当合并完成之后,最好是在服务器开一个网站继续测试这个网站,数据库在更新同步之后,进行测试这个主干的代码是否正常
在测试都通过之后。对主干进行打包。
打标签:
$ git archive --format=zip --output=标签名称.zip 需要打的标签 //标签导出zip
git tag tag-lw //创建标签 -lw意思是不变动的标签
git push origin tag //推送标签
git pull origin tag //拉取标签
git push origin branch_name tag_name //把标签做成分支
操作依旧是,右键主干目录,找到创建标签(小乌龟有中文语言包,自行安装),创建之后
打包之后,我们就把这个版本包导出,然后正式发布线上