Home  >  Q&A  >  body text

linux - Can git post-receive hook perform git pull on multiple servers?

Environment Description

  1. Created a warehouse in the /home/work/ directory of Server A /home/work/my-project.git

  2. Clone /home/work/my-project.git to the website root directory /home/www/

  3. Configuredgit post-receive hook

#!/bin/bash

cd /home/www
env -i git pull

In this way, every time git push, the code can be updated to the /home/www directory of server A

Problem Description

If I clone /home/work/my-project.git on Server B to the directory /home/www/. Is there any way to go to Server A and Server B to perform git pull operations at the same time every time git push is executed?

My current approach is to add a crontab scheduled task on server B, and execute it in the /home/www/ directory every minute git pull, although this can also achieve the effect of automatic git push, but there is also a time difference.

Do you have any better practices?

巴扎黑巴扎黑2713 days ago562

reply all(1)I'll reply

  • 曾经蜡笔没有小新

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

    If服务器A能通过ssh访问服务器B, you can add a line in git post-receive hook

    rsync -az -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" /home/www/ <服务器B的IP>:/home/www/

    After each git push, server A will clone /home/work/my-project.git to the website root directory /home/www/, and at the same time, it will synchronize /home/www/ to server B’s /home through rsync
    /www/

    reply
    0
  • Cancelreply