Git Hook - Make server pull after I push to github

假装没事ソ 提交于 2019-12-04 06:11:28

Just saw this unanswered even after years.

Webhooks require you to setup a HTTP POST listener on your server. You can add a http route in your project and perform appropriate pull action.

Reference: Webhooks

There is an alternate implementation which I use in my projects:

Suppose ~/example is your project public/www folder. Ssh into your server:

$ cd ~/example && mkdir .git && cd .git && git init --bare
$ cat > hooks/post-receive << EOF
> #!/bin/sh
> GIT_WORK_TREE=~/example git checkout -f
> EOF
$ chmod +x hooks/post-receive

The above will create a bare git repo in ~/example/.git folder. Add a post-receive executable hook and perform checkout into the example directory.

On local repo:

$ git remote add server ssh://my_user@my_server.com:/absolute_path/example/.git/
$ git push server +master:refs/heads/master

This works well for me. I can push, revert commits whenever I need.

Reference: Server repository

If I understood you correctly, you want the repository on your server to pull, after you pushed to GitHub.

So you have to notify your server to pull, after you pushed from your local repository. The post-receive-hook on the server is not, what does that. It is called, after something is received, but the push to github does not make the server receive anything. (See https://www.kernel.org/pub/software/scm/git/docs/githooks.html)

To notify the server, I would use the "post-update" hook on the remote repository. But as your remote is on GitHub, that could be a problem. As I am no GitHub-user I do not exactly know how to do that. Try "Webhook" or "Services" in the settings-tab of your repository (if you own the repository).

If hooks on GitHup do not do the trick:

As you suggested, you have ssh-access on your server, I think, it is some kind of linux. Then you could just write a small script that updates the server-repo, place it on the server and execute it via ssh after every push. I am not aware of any hook that gets called on the local repo after a push, so perhaps, you should write another script, that does the push and invokes the ssh-command, if the push was successful. If you use linux-systems, I can help you with the scripts.

What you are doing with the post-receive-hook on the server is, that git pull is executed, after you pushed to the server, but you are pushing to GitHub.

I hope this explains why your approach is not working.

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