发布一个自动部署的 git 脚本

icyleaf 2 年之前

脚本利用的是 git hooks,使用起来真是非常方便:

#!/bin/sh
#
# git autodeploy script when it matches the string "[deploy]"
#
# @author    icyleaf <icyleaf.cn@gmail.com>
# @link      http://icyleaf.com
# @version   0.1
#
# Usage:
#       1. put this into the post-receive hook file itself below
#       2. `chmod +x post-recive` 
#       3. Done!


# Check the remote git repository whether it is bare
IS_BARE=$(git rev-parse --is-bare-repository)
if [ -z "$IS_BARE" ]; then
	echo >&2 "fatal: post-receive: IS_NOT_BARE"
	exit 1
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"
	exit 1
fi


# Check the deploy dir whether it exists
DEPLOY_DIR=/home/icyleaf/php/icyleaf/
if [ ! -d $DEPLOY_DIR ] ; then
	echo >&2 "fatal: post-receive: DEPLOY_DIR_NOT_EXIST: \"$DEPLOY_DIR\""
	exit 1
fi


# Check the deploy dir whether it is git repository
#
#IS_GIT=$(git rev-parse --git-dir 2>/dev/null)
#if [ -z "$IS_GIT" ]; then
#	echo >&2 "fatal: post-receive: IS_NOT_GIT"
#	exit 1
#fi


# Goto the deploy dir and pull the latest sources
cd $DEPLOY_DIR
#env -i git reset --hard
env -i git pull

简单说下使用方法,把此脚本放在 git bare 目录下 hooks/post-receive 文件中(没有的话自己创建),然后对此文件:

 $ chmod +x post-receive

完成,脚本需要修改的是自定义部署的项目路径,其他没有什么了。

另外,脚本只在匹配到 commit 的 subject 包含 [deploy] 的时候才会部署。

在 github 也有链接:http://gist.github.com/566767

此链接会按个人需求持续更新!

刚发布的比较详细的教程:http://icyleaf.com/2010/09/08/apps-auto-deploy-with-git/

发表讨论

在回复之前你需要先进行登录
记住我的登录状态 (忘记密码)