2

I have a server that has bare git repository, and I'd like to use hook to sync the contents with a working so that I can sync my local directory to the working directory in the server.

Googling here and there; this is what I have done so far.

ssh setup

From https://stackoverflow.com/a/13542644/260127 and Dummy questions about setting up git on amazon cloud ec2

local: ssh-keygen -t rsa -b 1024 and name it id_rsa_aws. Then id_rsa_asw and id_rsa_aws.pub file is created.

access to the server: ssh -i amazon-generated-key.pem ubuntu@example.com

append the generated id_rsa_aws.pub key into ~/.ssh/authorized_keys.

creating a git bare repository

server: mkdir -p ~/git/dumb.git; cd ~/git/dumb.git; git init --bare

local: git clone ubuntu@example.com:git/dumb.git

Push from server's working directory

server: git clone ~/git/dumb.git

Push from local working directory.

Make some changes in a local directory.

Create a hook

Make a ~/git/dumb.git/hooks/post-commit, then make it runnable with chmod a+x post-commit.

#!/bin/bash

unset GIT_INDEX_FILE
git --work-tree=/home/ubuntu/dumb --git-dir=/home/ubuntu/git/dumb.git checkout -f

ISSUE

When I push from my local machine to server, there is no issue.

dumb> git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 278 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ubuntu@prosseek.com:git/dumb.git
   5964789..b29c160  master -> master 

However, the post-commit is not invoked automatically. I can login to the server, and execute the post-commit and the working directory is synced.

What might be wrong?

Community
  • 1
  • 1
prosseek
  • 182,215
  • 215
  • 566
  • 871

1 Answers1

2

The hook was wrong, the hook script should have been post-receive not post-commit. Hints from https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks

This is the content of the post-receive hook.

#!/bin/bash                                                                                               

unset GIT_INDEX_FILE                                                                                      

while read oldrev newrev ref                                                                              
do                                                                                                        
    if [[ $ref =~ .*/master$ ]];                                                                          
    then                                                                                                  
      echo "Master ref received.  Deploying master branch to production..."                               
      git --work-tree=/home/ubuntu/dumb --git-dir=/home/ubuntu/git/dumb.git checkout -f master            
      echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."                                                                                                                                                                                                          
    fi                                                                                                    
done     
prosseek
  • 182,215
  • 215
  • 566
  • 871
  • There is another issue with this approach, I posted another question for that: http://stackoverflow.com/questions/33812206/how-to-sync-the-stageindex-and-working-directory-after-git-hook-post-receive – prosseek Nov 19 '15 at 18:58