3

I have a website hosted on an Amazon EC2 instance (running Ubuntu 12.04). I want to be able to be able to push changes to the server using git and then use a post-receive hook to checkout in the working directory. So on the server in the hooks directory I have a file named post-receive containing:

#!/bin/sh
GIT_WORK_TREE=/home/ubuntu/beta git checkout -f

The file has the permissions: -rwxrwxr-x (i.e. it is executable).

On my local machine, when I push to the git repo on the server, the push is successful and the git repo is updated. The post-receive hook is not run, however. If I run the hook manually, it runs fine and updates the working directory.

The git push is carried out over SSH and uses the same user as if I were running the hook manually.

Any ideas why the hook will not run automatically?

Thanks.

user2080608
  • 31
  • 1
  • 2
  • It's set up pretty much exactly the same. The only difference is that his working directory is in /var/www/html whereas mine is just a directory in my home. That shouldn't make any difference, though. Is it correct that the hook just needs to be a shell script named 'post-receive' and be writable? – user2080608 Feb 17 '13 at 17:09
  • The hook must be *executable*. and the working tree must exist on the Amazon EC2 instance. – VonC Feb 17 '13 at 17:11
  • Sorry, I meant executable. It is definitely executable as I can run it manually. The working tree is in the home directory of the EC2 instance. – user2080608 Feb 17 '13 at 17:18
  • Check if it is run at all, with a simple echo in it. Try also to specify `GIT_DIR`(`=/path/to/your/bare/repo`) *in addition* of `GIT_WORK_TREE`. – VonC Feb 17 '13 at 17:25
  • I added an echo. It isn't shown when I do a git push. I also specified GIT_DIR as well which made no difference. So it definitely isn't run. I know there isn't anything wrong with the script itself or it's permissions as I can run it by './hooks/post-receive'. I'm really scratching my head over why git wouldn't run the script. – user2080608 Feb 17 '13 at 17:39
  • 1
    You did add it in the `bare repo/hooks/post-receive` on the Amazon EC2 side, right? (not just on your local repo on your local machine for tests). – VonC Feb 17 '13 at 17:52
  • Can you make sure that it is in the right folder? – manojlds Feb 17 '13 at 19:04
  • Thanks for all your help everybody. In the end I just deleted the repo on my server and started again. This time it worked. I wish I could say what went wrong the first time. – user2080608 Feb 23 '13 at 00:11

1 Answers1

3

I've set up an local identical test (ie I cloned from a local folder) and it works fine.

To see if the script is executed at all I've added a simple touch to see if it modifies any files

My post-receive looks like this

#!/bin/sh

touch /Users/raven/git_tests/live/.git/hooks/i_ve_been_run
GIT_WORK_TREE=/Users/raven/git_tests/live git checkout -f

Make sure that your hook is named exactly post-receive or it won't work.

Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78