jenkins自动部署tomcat
关于部署的3种思路:
- 远程部署(jenkins编译部署到远程服务器):
- 安装ssh插件
- ssh插件配置
- 添加远程jenkins服务器节点:
- 本地部署(与jenkins在同一服务器):
关于maven构建的两种选择:
- 直接选择maven插件
- 自带插件():
- 插件配置(或直接新建maven项目):
- 添加shell编译过程,通过shell调用服务器的maven环境
jenkins配置:
export BUILD_ID=XXXXXX # 必须得加,原因未知,否则无法启动tomcat
export project_dir=$WORKSPACE
export target_dir=/export/App/zgbcms.m.jd.com
export do_dir=/export/Domains/zgbcms.m.jd.com/server1/bin
export war_path="cms-web"
export war_file="cms-web.war"
# 接受6个参数,mvn_parama为编译什么版本,接受参数dovolopment,test
export mvn_parama="test"
/home/lyz/bushu.sh
环境部署shell:
#!/bin/bash
# 自动编译部署脚本,jenkins调用
# 编译后生成文件war,清空并解压到指定文件夹,重启服务器
export M2_HOME=/export/servers/apache-maven-2.2.1
export JAVA_HOME=/export/servers/jdk1.6.0_25
export JAVA_BIN=/export/servers/jdk1.6.0_25/bin
export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/bin
export CLASSPATH=.:/lib/dt.jar:/lib/tools.jar
export JAVA_OPTS="-Djava.library.path=/usr/local/lib -server -Xms1024m -Xmx1024m -XX:MaxPermSize=256m -Djava.awt.headless=true -Dsun.net.client.defaultConnectTimeout=60000 -Dsun.net.client.defaultReadTimeout=60000 -Djmagick.systemclassloader=no -Dnetworkaddress.cache.ttl=300 -Dsun.net.inetaddr.ttl=300"
export JAVA_HOME JAVA_BIN PATH CLASSPATH JAVA_OPTS
export PATH=$PATH:$M2_HOME/bin
# 测试用传参
#export project_dir=/root/.jenkins/workspace/zgb_server_master
#export target_dir=/export/App/zgb.m.jd.com
#export do_dir=/export/Domains/zgb.m.jd.com/server1/bin
#export war_path="jd-qqgserver-web"
#export war_file="jd-qqgserver-web.war"
project_dir=$project_dir
target_dir=$target_dir
do_dir=$do_dir
war_path=$war_path
war_file=$war_file
mvn_parama=$mvn_parama
echo $project_dir
echo $target_dir
echo $do_dir
echo $war_path
echo $war_file
echo $mvn_parama
function mvn_build()
{
if[ $# -eq 6 ] # 校验参数是否为6个
then
if[-d "$project_dir/"]&&[-d "$target_dir/"]&&[-d "$do_dir/"]# 校验文件夹是否存在
then
cd $project_dir
if["$mvn_parama"=="development"]
then
echo "编译开发版本!"
mvn clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -P artifactory,development
elif["$mvn_parama"=="test"]
then
echo "编译test版本!"
mvn clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -P artifactory,test
else
echo "未指定配置文件参数!会导致服务器无法访问!"
exit 1
fi
rm -rf $target_dir/*
cp -r $project_dir/$war_path/target/$war_file $target_dir/
echo $target_dir/$war_file
cd $target_dir
unzip -oq $war_file
rm -rf $target_dir/$war_file
$do_dir/stop.sh
if[ $?==0]
then
echo "服务器停止成功!"
else
echo "服务器停止失败!"
fi
$do_dir/start.sh
if[ $?==0]
then
echo "服务器启动成功!"
else
echo "服务器启动失败!"
exit 1# 退出状态为1,传给jenkins让任务失败
fi
else
echo "Folder $* is not exist!"
exit 1
fi
else
echo "$0 is Errors! Parama is $# not 6!"
exit 1
fi
}
# 接受6个参数,mvn_parama为编译什么版本,接受参数dovolopment,beta,production,test 或其他,其他为默认
mvn_build $project_dir $target_dir $do_dir $war_path $war_file $mvn_parama
附件列表