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

环境说明

  1. 服务器A/home/work/目录下创建了仓库/home/work/my-project.git

  2. /home/work/my-project.git克隆到网站根目录/home/www/

  3. 配置了git post-receive hook

#!/bin/bash

cd /home/www
env -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的效果,但是也存在时间差。

请问大家有没有什么比较好的做法呢?

阅读 3.1k
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/

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题