Git自动部署

Git自动部署文件位于repository下面的hooks里的post-receive

#!/bin/sh

set -e

git-update-server-info

gitosis-run-hook update-mirrors

# Check the remote git repository whetherit is bare

IS_BARE=$(git rev-parse--is-bare-repository)

if [ -z "$IS_BARE" ]; then

         echo>&2 "fatal: post-receive: IS_NOT_BARE"

         exit1

fi

# Get the latest commit subject

SUBJECT=$(git log -1--pretty=format:"%s")

# Deploy the HEAD sources to publish

IS_PULL=$(echo "$SUBJECT" | grep"deploy")

if [ -z "$IS_PULL" ]; then

         echo>&2 "tips: post-receive: IS_NOT_PULL"

         exit1

fi

# Check the deploy dir whether it exists

DEPLOY_DIR=/var/www/htdocs/test_com/

if [ ! -d $DEPLOY_DIR ] ; then

         echo>&2 "fatal: post-receive: DEPLOY_DIR_NOT_EXIST:"$DEPLOY_DIR""

         exit1

fi

# Check the deploy dir whether it is gitrepository

#

#IS_GIT=$(git rev-parse --git-dir2>/dev/null)

#if [ -z "$IS_GIT" ]; then

#       echo>&2 "fatal: post-receive: IS_NOT_GIT"

#       exit1

#fi

# Goto the deploy dir and pull the latestsources

cd $DEPLOY_DIR

#env -i git reset --hard

unset GIT_DIR

git pull

提示我:"fatal: Not a git repository: '.'" 
也就是说这个hook脚本执行了cd之后,继续执行git语句拉取的时候还是在hooks文件夹下,而不是cd的文件路径
如果第三句改成 "env -i git pull",也会提示说env git不是一个命令什么的.. 
问题是git的hooks里面默认有一些环境变量,会导致无论在哪个语句之后执行git命令都会有一个默认的环境路径,所以解决方法就是 
在git  pull与cd命令之间加上unsetGIT_DIR就搞定了

---------------------------------------------------

另外一篇参考文章:

http://www.360doc.com/content/14/0115/17/10058718_345507725.shtml

使用 Git Hooks 实现自动项目部署

原文地址:https://www.cnblogs.com/kenshinobiy/p/4308253.html