Git自动部署

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

 

#!/bin/shgit

set -epost

git-update-server-infoorm

gitosis-run-hook update-mirrorsserver

# Check the remote git repository whetherit is barehtm

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

if [ -z "$IS_BARE" ]; thenrem

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

         exit1it

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 实现自动项目部署

相关文章
相关标签/搜索