welcome to xlongwei.com

欢迎大家一起学习、交流、分享


QQ:9167702333 邮箱:admin@xlongwei.com

post-commit and post-update


分类 Java   关键字 分享   标签 linux   nginx   svn   git   发布 hongwei  1450964300337
注意 转载须保留原文链接,译文链接,作者译者等信息。  
svn和git仓库都提供钩子服务,可用于自动更新源码目录,本站svn.xlongwei.com和git.xlongwei.com已实现代码提交后自动更新。

svn提交后会自动执行hooks/post-commit钩子,下面#注释掉的行都不能正确运行,绕道tomcat来运行shell脚本反而可行。
vi svnrepos/hooks/post-commit
echo "svn post commit `date +'%Y-%m-%d %H:%M:%S'`" >> /var/log/repos.log
#sh /soft/shells/repos-update.sh

#cd /soft/svndata
#svn up

#/usr/bin/svn up /soft/svndata --username deployer --password 3ks4deploy

wget http://localhost:8080/dir/path.action?cmd=sh+/soft/shells/repos-update.sh

git推送之后会自动执行hooks/post-update,直接复制post-update.sample并添加可执行权限
vi gitrepos/hooks/post-update
echo "git post update `date +'%Y-%m-%d %H:%M:%S'`" >> /var/log/repos.log

#sh /soft/shells/repos-update.sh

#cd /soft/gitdata/sample
#git pull

wget http://localhost:8080/dir/path.action?cmd=sh+/soft/shells/repos-update.sh

代码更新脚本可以分开为多个脚本,这里合并以便省事
vi shells/repos-update.sh
echo "svndata update `date +'%Y-%m-%d %H:%M:%S'`" >> /var/log/repos.log
svn up /soft/svndata >> /var/log/repos.log

echo "\ngitdata update `date +'%Y-%m-%d %H:%M:%S'`" >> /var/log/repos.log
cd /soft/gitdata/mobile
git pull >> /var/log/repos.log
cd /soft/gitdata/sample
git pull >> /var/log/repos.log

Controller代码
public Object shell(HttpServletRequest request) {
	JSONObject json = new JSONObject();
	String cmd = RequestUtil.getStringParam(request, "cmd", null);
	if(!StringUtil.isBlank(cmd)) {
		try {
			Process exec = Runtime.getRuntime().exec(cmd); //有一定安全隐患,所以隐藏访问路径
			json.put("ret", FileUtil.readString(exec.getInputStream(), FileUtil.defaultCharsetName));
			json.put("error", FileUtil.readString(exec.getErrorStream(), FileUtil.defaultCharsetName));
			exec.waitFor();
		} catch (IOException | InterruptedException e) {
			json.put("error", e.getMessage());
		}
	}else json.put("error", "empty cmd");
	return json;
}

代码检出一份,通过nginx提供访问
server {
        listen 80;
        server_name git.xlongwei.com;
        access_log /var/log/nginx/access.log main;
        location ~* .*\.(svn|cvs|git).* {
                deny all;
        }
        location / {
                root /soft/gitdata;
                autoindex on;
        }
}