Issue in automatic executing post-commit in git hook to sync local and remote directory

泪湿孤枕 提交于 2020-01-16 01:07:11

问题


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

  • What does git "updating currently checked out branch" warning mean?

Push from server's working directory

server: git clone ~/git/dumb.git

Push from local working directory.

Make some changes in a local directory.

  • local: git add .; git commit -m "...";
  • local: git push --set-upstream origin master
    • This is the one time execution, then I can use git push
    • Why can't I push to this bare repository?

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?


回答1:


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     


来源:https://stackoverflow.com/questions/33810949/issue-in-automatic-executing-post-commit-in-git-hook-to-sync-local-and-remote-di

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