Git Hook 远程发布

需求:

AA:gitolite服务器

BB: 生产服务器

代码全部提交到AA,每次都需要手工在BB上pull代码

现在想做到:提交代码到AA时,如果commit字符串中存在[deploy]那么自动登录到BB上特定目录pull代码,并清理缓存。

 

操作步骤:

一、新建用户

AA:gitolite的用户是git,生成一个不带密码的sshkey,假设文件名为id_rsa

BB: 生成一个新用户gitUpdate用来发布代码,最好将gitUpdate加入apache组,方便管理

二、配置ssh信任关系

将AA上的git用户的id_rsa添加到BB中gitUpdate用户的信任列表中。注意权限的设置

可以在AA上测试:ssh gitUpdate@BB

三、在BB的gitUpdate主目录创建一个脚本文件update.sh

四、在AA上配置git hook

#!/bin/sh
SUBJECT=$(git log -1 --pretty=format:"%s")
echo >&2 "msg1:$SUBJECT"


# 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
exec ssh hwpublish ./publishToMobileTest.sh
exit 1

 

搞定收工。

 

posted on 2012-06-18 01:04  neolf  阅读(743)  评论(0编辑  收藏  举报

导航