How do I setup a git hook to pull from Github?

空扰寡人 提交于 2020-01-13 06:42:13

问题


I develop on my Mac and push it to Github. I login to my server by SSH and I git pull the changes to the server. I want the changes to automatically be pulled to the server when I push them to Github so I a file .git/hooks/post-update with this info

#!/bin/sh

echo
echo "**** Pulling changes into Live [Hub's post-update hook]"
echo

cd /mydirector/html || exit
unset GIT_DIR
git pull

exec git-update-server-info

What else should I do to get it working? Thanks in advance for your answer. It will be very much appreciated.


回答1:


You could also declare a webhook on GitHub, in order to generate an event payload that can be listened to by a process on your server.

That process (a listener) would be in charge of triggering the git pull.

You will find various examples of such listeners, like:

  • a basic php one
  • one in go
  • or in python



回答2:


You need to realize that the hook can not work ok the server for this use case. The server has no way to know you did a push (unless you have a github webhook setup and working, in which case, look at answer by @VonC), so the hook for the update would need to be local. Hooks are really not intended for this purpose as stated here: Local executing hook after a git push?

What you really want to do is push your changes to both the github, and your server. The easiest way would just be to push to both. See pull/push from multiple remote locations

Usually git-hooks are added to the server so that after you push to the server, the server can do some extra stuff (like start the app or something). (Here is a good article: http://danbarber.me/using-git-for-deployment/)


However, as you requested, you could alternatively use the post-receive script to simply do the push. Since git does not support post-push git hooks, this would have to run on the git repository. Github however, does not allow these (they only allow webhooks, for security reasons of course). But if it did, you could do:

#!/bin/sh
git remote add live git@mydomain.com:www/mysite
git push live master

exec git update-server-info

And make sure to set the file as executable chmod a+x post-receive



来源:https://stackoverflow.com/questions/25814918/how-do-i-setup-a-git-hook-to-pull-from-github

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!