发出自动执行git钩子中的post-commit来同步本地和远程目录 [英] Issue in automatic executing post-commit in git hook to sync local and remote directory

查看:129
本文介绍了发出自动执行git钩子中的post-commit来同步本地和远程目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个只有git仓库的服务器,我想用hook来同步内容和工作,这样我就可以将我的本地目录同步到服务器的工作目录。

谷歌在这里和那里搜索;这是我迄今为止所做的。

ssh setup



来自 https://stackoverflow.com/a/13542644/260127 关于在amazon cloud ec2上设置git的虚拟问题


$ b local: ssh-keygen - t rsa -b 1024 并将其命名为 id_rsa_aws 。然后创建 id_rsa_asw id_rsa_aws.pub 文件。

访问服务器: ssh -i amazon-generated-key.pem ubuntu@example.com



将生成的 id_rsa_aws.pub 键添加到〜/ .ssh / authorized_keys

创建一个git仓库



服务器: mkdir -p 〜/ GIT中/ dumb.git; cd〜/ git / dumb.git; git init --bare



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





从服务器的工作目录中推送



服务器: git clone〜/ git / dumb.git



从本地工作目录推送。



在本地目录中进行一些更改。





创建挂钩



创建一个〜/ git / dumb.git / hooks / post-commit ,然后用 chmod运行它a + x提交后

 #!/ bin / bash 

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



ISSUE



当我从本地计算机推送到服务器时,没有问题。

 哑GT; git push 
计数对象:3,完成。
使用多达8个线程的增量压缩。
压缩对象:100%(2/2),完成。
写入对象:100%(3/3),278字节| 0字节/秒,完成。
总计3(delta 0),重用0(delta 0)
到ubuntu@prosseek.com:git / dumb.git
5964789..b29c160 master - > master

但是,post-commit不会自动调用。我可以登录到服务器,然后执行 post-commit 并且同步工作目录。



什么可能是错误的?

解决方案

钩子错了,钩子脚本应该是 post-receive 不是 post-commit 。来自
的提示 https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks



这是 post-receive 钩子的内容。

 #!/ bin / bash 

unset GIT_INDEX_FILE

while readre oldrev newrev ref
do
if [[$ ref =〜 。* / master $]];
然后
echoMaster ref received。将主分支部署到生产...
git --work-tree = / home / ubuntu / dumb --git-dir = / home / ubuntu / git / dumb.git checkout -f master
echoRef $ ref successfully successfully。Do not nothing:只有主分支可以部署在这台服务器上。
fi
完成


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?

解决方案

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     

这篇关于发出自动执行git钩子中的post-commit来同步本地和远程目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆