问题描述
环境说明在服务器A的/home/work/目录下创建了仓库/home/work/my-project.git
将/home/work/my-project.git克隆到网站根目录/home/www/
配置了git post-receive hook
#!/bin/bashcd /home/wwwenv -i git pull
这样每次git push的时候,都可以把代码更新到服务器A的/home/www目录下
问题描述如果我在服务器B也克隆了/home/work/my-project.git到目录/home/www/下。有没有什么办法每次执行git push的时候,同时到服务器A和服务器B执行git pull操作?
我现在的做法是在服务器B上加一个crontab定时任务,每分钟到/home/www/目录下执行一下git pull,这样虽然也可以达到自动git push的效果,但是也存在时间差。
请问大家有没有什么比较好的做法呢?
问题解答
回答1:如果服务器A能通过ssh访问服务器B, 可以在git post-receive hook中添加一行
rsync -az -e 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' /home/www/ <服务器B的IP>:/home/www/
每次git push之后,服务器A将/home/work/my-project.git克隆到网站根目录/home/www/的同时,会通过rsync将/home/www/同步到服务器B的/home/www/