基础环境
两台CentOS6.8服务器,IP地址分别为:192.168.199.199和192.168.199.222
JDK1.8
Tomcat 8.x 部署了2个,一个服务器部署Jenkins应用,另一个服务器部署WEB应用
Maven3.5 192.168.199.199搭建maven仓库
SVN 远程仓库服务器
一、Jenkins安装
需要安装的插件:
Maven Integration
SSH
Publish Over SSH
二、基础环境配置
2.1JDK设置
系统管理 --> 全局工具配置 --> 设置JDK安装
# 查看JAVA_HOME
echo $JAVA_HOME
2.2Maven设置
2.2.1系统管理 --> 全局工具配置 --> 设置Maven
# 查看maven安装路径
mvn -v
2.2.2系统管理 --> 全局工具配置 --> Maven配置
2.2.3系统管理 --> 系统设置 --> Maven项目配置
选择Local to the executor(局部)或Local to the workspace(整个工作空间)
-Xms1024M -Xmx1024M -XX:PermSize=256M -XX:MaxPermSize=256M
2.3SSH设置
系统管理 --> 系统设置 --> Publish over SSH --> SSH Servers
点击新增SSH Servers
勾选Use password authentication, or use a different key,填写IP地址、账号密码等等。
三、新建任务自动部署
3.1新建任务
3.2输入项目名称,构建maven项目
3.3添加项目描述(非必须)、设置丢弃旧的构建
填写项目描述
勾选丢弃旧的构建,防止长时间部署产生内存问题。
按照如图配置
3.4设置源码管理
如果没有账户先添加账户
3.5设置构建触发器
通常选择轮询SCM,也可以不设置。
3.6设置构建环境
勾选Add timestamps to the Console Output
3.7设置Build
clean install package -Dmaven.test.skip=true
3.8设置Post Steps,编写自动化远程部署脚本
勾选Run only if build succeeds
选择Send files or execute commands over SSH
填写SSH Server远程部署信息
Run only if build succeeds(只有构建成功时才运行)
Run only if build succeeds or is unstable(仅在构建成功或不稳定时运行)
Run regardless of build result(不管构建结果如何运行)
脚本如下:
#!/bin/bash
################################################
## create by wyf at 2019-11-02
## this scripts is for auto push code to SSH WEB server
################################################
#防止jenkins构建完成后杀死tomcat进程
export BUILD_ID=dontkillme
#The environment variable
TOMCAT_PATH=/home/tomcat/testTomcat
WAR_NAME=jeecg
JENKINS_WAR_PATH=/home/tomcat/jenkinsWar
#Stop tomcat server && kill TID
TID=`ps -ef|grep $TOMCAT_PATH |grep -v grep|awk '{print $2}'`
#[]中必须保留前后空格,格式要求
if [ ! -n "$TID" ]; then
echo "Tomcat server not running!"
else
for id in $TID
do
kill -9 $id;
echo "Tomcat server stop sucessfully!";
sleep 3s;
done
fi
#Delete old war packages and files
rm -rf $TOMCAT_PATH/webapps/$WAR_NAME*
#Copy the file to the server webapps
cd $JENKINS_WAR_PATH
cp -ar $WAR_NAME.war $TOMCAT_PATH/webapps
sleep 5s
#Start tomcat server
sh $TOMCAT_PATH/bin/startup.sh
echo "Tomcat server start sucessfully!"
3.9保存,立即构建
3.10查看构建日志
选择控制台输出
最后出现SUCCESS代表成功
四、参考文章
参考文章脚本:
https://blog.csdn.net/yanfei_1986/article/details/83108557