Git Hook - Make server pull after I push to github

2019-04-08 13:52发布

I have a local repository, and I have set up another repository on my live server:

www.site.com/projects/ProjectA

What I want to achieve is very simple: After I PUSH to GitHub, I want the repository at www.site.com/projects/ProjectA to PULL - hence update the live version of the project, that my client can see.

I've been reading tons about hooks and I cannot find a very simple example for what I need. All tutorials are made for advanced functionality.

What I have done

  1. I have the SSH access settings done.
  2. I have a remote repository at www.site.com/projects/ProjectA that I created while logged in with Putty and doing git clone (at this point my local repo, the GitHub repo and the server repo were all in sync)

  3. I copied post-receive.sample from .git/hooks to www.site.com/projects/ProjectA/

  4. I renamed post-receive.sample to post-receive
  5. In github, under the repo settings / webhooks I created a new hook that points to www.site.com/projects/ProjectA/post-receive. All other settings I left intact.

I made a local change, commit, push. The Hub updates, the hook shows a new recent delivery. All is well, but the server repository does not update.

The "hook" code is ". /usr/share/git-core/contrib/hooks/post-receive-email" and I don't understand what that does. I looked at other hook samples and I saw some of them have familiar commands like: exec git update-server-info so I figured I could write my own command. so I wrote git pull, saved it and did the change - commit - push again from my local repo. The result was the same as before. I then tried exec git pull. Same thing.

My question is - what am I doing wrong, and the second question is, why the heck isn't there a simple tutorial for this functionality, since this seems to be one of the most usual scenarios. Thank you!

标签: git github hook
2条回答
祖国的老花朵
2楼-- · 2019-04-08 14:47

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.

查看更多
成全新的幸福
3楼-- · 2019-04-08 14:54

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

查看更多
登录 后发表回答