Git Hook 同步服务器代码

时间:2021-03-25 08:31:55

参考并转载自:

http://www.embbnux.com/2014/09/05/git_server_let_code_auto_deploy/

http://www.chenyudong.com/archives/git-sync-manage-website.html#i

1. 创建远程仓库

$ mkdir /home/www.cnblogs.com/www.cnblogs.com.git #将来网站git的记录存放位置
$ chown -R git:git www.cnblogs.com.git #改权限
$ cd www.cnblogs.com.git
$ git init --bare
Initialized empty Git repository in /home/www.cnblogs.com/www.cnblogs.com.git/

2. 在git远端仓库的hooks目录下新建post-receive文件:

#!/bin/sh

#author: embbnux
#Blog of Embbnux: http://www.embbnux.com #判断是不是远端仓库
IS_BARE=$(git rev-parse --is-bare-repository)
if [ -z "$IS_BARE" ]; then
echo >&2 "fatal: post-receive: IS_NOT_BARE"
exit 1
fi unset GIT_DIR
DeployPath="/var/web" echo "==============================================="
cd $DeployPath
echo "deploying the test web" git fetch --all
git reset --hard origin/master time=`date`
echo "web server pull at webserver at time: $time."
echo "================================================"

  保存后赋予可执行权限:

chmod +x hooks/post-receive