实现代码自动打包与发布(Jenkins+Maven+Svn)
前言 很多开发人员都知道,在一些互联企业初期大多数都是采用手工打包上传与发布的方式进行代码发布,常见就是利用打包工具手工打包,上传到WEB服务器,备份原代码文件,发布新的代码,重启服务和检测是否发布成功,由于现在WEB集群数量越来越多,那手工打包的方式就会使工作效率大大下降,为了提高效率项目自动打包与发布代码的功能(sit/ust/pro)就显得格外的高效.1、安装JD
安装jdk、tomcat环境
[root@centos6 ~]# tar zxf jdk-8u111-linux-x64.tar.gz -C /usr/local/
[root@centos6 ~]# tar zxf apache-tomcat-8.5.9.tar.gz -C /usr/local/
[root@centos6 local]# export JAVA_HOME=/usr/local/jdk1.8.0_111
[root@centos6 local]# export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
[root@centos6 local]# export PATH=$JAVA_HOME/bin:$PATH
[root@centos6 local]# export CATALINA_HOME=/usr/local/apache-tomcat-8.5.9
[root@centos6 local]# source /etc/profile
[root@centos6 local]# java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)
上官网下载安装包
[root@centos6 ~]# wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
[root@centos6 ~]#cp jenkins.war /usr/local/apache-tomcat-8.5.9/webapps/
[root@centos6 webapps]#../bin/startup.sh &
可以在下面的文件中找到初始密码
[root@centos6 webapps]# cat /root/.jenkins/secrets/initialAdminPassword
0d647a32992149b3b43f77e4bda93809
输入密码,跳转到下一个界面
安装完成创建用户与密码
接下来安装下后续需要插件
官网下载软件
wget http://apache.fayea.com/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.tar.gz
tar zxf apache-maven-3.3.9-bin.tar.gz –C /usr/local/
mv /usr/local/apache-maven-3.3.9 /usr/local/maven-3.3.9
配置环境变量
echo 'export MAVEN_HOME=/usr/local/maven-3.3.9' >> /etc/profile
echo 'export PATH=$PATH:$MAVEN_HOME/bin' >> /etc/profile
source /etc/profile
检测是否安装完成
[root@centos6 ~]# mvn -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/maven-3.3.9
Java version: 1.8.0_111, vendor: Oracle Corporation
Java home: /usr/local/jdk1.8.0_111/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-642.el6.x86_64", arch: "amd64", family: "unix"
在Jenkins上配置maven
完成配置后点保存,在首页就可以看到刚刚创建的JOB,点立即构建,然后在控制台看输出信息
可以看到出现构建成功的字样,同时也看代码已打包完成,目录如下:/root/.jenkins/workapace/JAVA/target/spring-mvc.war
[root@centos scripts]# cat auto_push.sh
#!/bin/sh
################################################
## create by mingongge at 2017-07-09
## this scripts is for auto push code to WEB server
## 微信公众号:友侃有笑(youkanyouxiao)
################################################
#stop server
/usr/local/tomcat/bin/shutdown.sh
TPID=`ps -ef|grep tomcat|grep -v grep|awk '{print $2}'`
TIME=`date +%F`
INSTALLDIR=/usr/local/tomcat/webapps/
if [ $TPID =="" ];then
echo "tomcat stop sucessfully"
else
kill -9 $TPID
fi
#push code to server and start server
cd $INSTALLDIR
\cp -ar /root/.jenkins/workspace/JAVA/target/*.war $INSTALLDIR
/usr/local/tomcat/bin/startup.sh
RESULT=`netstat -lntup|grep 8081|wc -l`
if [ $RESULT -eq 1 ];then
echo "tomcat start sucessfull"
else
/usr/local/tomcat/bin/startup.sh
Fi
更多推荐
所有评论(0)