Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Hook - Make server pull after I push to github

Tags:

git

github

hook

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!

like image 914
user3015234 Avatar asked Jun 24 '14 08:06

user3015234


1 Answers

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

like image 163
Nitin... Avatar answered Oct 09 '22 10:10

Nitin...