使用git hooks实现代码自动部署更新

环境:CentOS 7

先说下整体思路:在服务器中创建远程仓库A(裸仓库)和本地仓库B(一般仓库)。然后在仓库A中创建使B git pull A的脚本,并每当有用户 git push A的时候就触发该脚本。

1. 初始化两个仓库

两个仓库的目录分别是 A:/home/mycode/test.git 和 B:/home/www/test.git

创建仓库A可以参考点击打开链接。

创建仓库B则如同在本地创建仓库一样

cd /home
mkdir www
chown git www/
cd www
#注意,这里要初始化为一般仓库而非裸仓库
git init test.git
chown -R git test.git
2. 在仓库A中配置hooks

cd /home/mycode/test.git/hooks
vi post-receive
然后输入如下内容并保存

unset GIT_DIR
 
#进入仓库B目录
cd /home/www/test.git
#执行git pull命令
git pull /home/mycode/test.git master
#返回仓库A并退出
cd /home/mycode/test.git
exit 0
赋予其权限

chmod +x /home/mycode/test.git/hooks/post-receive
然后我们在本地向仓库A中 push 文件后就可以在仓库B中看到该文件。
--------------------- 


原文:https://blog.csdn.net/zhiyual/article/details/79825376 

猜你喜欢

转载自blog.csdn.net/weixin_36065510/article/details/89330087