问题
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?
- This is the one time execution, then I can use
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