linux - git post-receive hook 可以到多台服务器上执行 git pull 吗?

【字号: 日期:2022-08-08浏览:36作者:雯心

问题描述

环境说明

在服务器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/

相关文章: