首页  >  问答  >  正文

linux - 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的效果,但是也存在时间差。

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

巴扎黑巴扎黑2713 天前558

全部回复(1)我来回复

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-16 13:24:18

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

    回复
    0
  • 取消回复