天天看點

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"

# 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\""

# 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 實作自動項目部署

如何聯系我:【萬裡虎】www.bravetiger.cn

【QQ】3396726884 (咨詢問題100元起,幫助解決問題500元起)

【部落格】http://www.cnblogs.com/kenshinobiy/