Jenkins是一个开源的持续集成工具,应用Jenkins搭建持续集成环境,可以进行自动构建、自动编译和部署,非常方便。

在服务器比较少的情况下,Jenkins的优势并不明显,但是随着项目发展,服务器数量的增加,Jenkins的优势就会凸显出来,可以很好的提高效率,减少很多人工操作。

现在公司的开发都是使用Git管理代码,Maven管理多模块和项目依赖,

所以今天尝试学习如何使用Jenkins搭建Github与Maven下的自动构建和部署

1.部署Jenkins

官网下载http://jenkins-ci.org/。目前的最新版本是1.629。

Jenkins的安装十分简单,下载后就是一个jenkins.war的war包,可以直接部署在Tomcat或者其他容器中。

如果不能部署,可以检查Tomcat的配置文件,可以查看server.xml里unpackWARs和autoDeploy是否设置为True。

另外官网还有相关的.deb等的安装,比较繁琐,具体哪种方式部署可以自己选择。

 

2.安装相关插件

把war文件拷贝到Tomcat的webapps目录,

启动后进入http://SERVER_PATH:8080/jenkins/,可以看到Jenkins已经在运行:

jenkins构建git maven项目 jenkins自动构建git_tomcat

配置Git仓库需要用到Git插件,Jenkins默认没有Git插件,需要手动安装。

点击Manage Jenkins,进入Manage Plugins

可用(Avaliable)插件列表下找到Source Code Management一栏,

选择GIT plugin插件,安装之后重启。

jenkins构建git maven项目 jenkins自动构建git_tomcat_02

其他的插件如Maven等,Jenkins默认安装,不需要手动下载。

 

3.配置Maven和JDK路径等

选择Configure System,可以配置Maven安装路径等。
如果没有Maven和Git环境,需要另外设置。

jenkins构建git maven项目 jenkins自动构建git_运维_03

记得配置JDK路径,第一次我就忘记配置,结果构建时系统自动安装Jdk,特别慢。

jenkins构建git maven项目 jenkins自动构建git_tomcat_04


下面是Jenkins Location选项,jenkins默认会存放在用户主目录下的.jenkins文件夹中,如果需要变动可以在这里更改。

后面的shell中也会用到Jenkins Location,默认的访问路径:

http://SERVER_PATH:8080/jenkins/job/

jenkins构建git maven项目 jenkins自动构建git_java_05

 

4.创建任务,配置项目信息

为了更好的学习Jenkins,我新建了一个非常简单的Spring MVC项目,这个项目使用Maven管理,提交到了github上,地址:

https://github.com/bingyue/easy-springmvc-maven

 

pom.xml的部分内容:

 

 

 


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


...


< groupId >springmvc-maven</ groupId >


< artifactId >easy-springmvc-maven</ artifactId >


< version >0.0.1-SNAPSHOT</ version >


< packaging >war</ packaging >


...


< build >


<!-- 生成的war文件名 避免添加版本号 -->


< finalName >easy-springmvc-maven</ finalName >


< plugins >


< plugin >


< artifactId >maven-compiler-plugin</ artifactId >


< version >3.1</ version >


< configuration >


< source >1.6</ source >


< target >1.6</ target >


</ configuration >


</ plugin >


< plugin >


< artifactId >maven-war-plugin</ artifactId >


< configuration >


< version >3.0 </ version >


</ configuration >


</ plugin >


</ plugins >


</ build >


...



 

 

 

首先按照提示创建一个任务,选择Maven Project

 

jenkins构建git maven项目 jenkins自动构建git_运维_06

进入Config页面,点击Source Code Management的git选项,

填入上面的git地址,配置用户名密码等参数。

 

jenkins构建git maven项目 jenkins自动构建git_git_07

下面的Build Triggers是一个持续集成的触发器插件,
可以根据已经完成构建的结果,触发新Job或者传递参数。
默认的选项是Build whenever a SNAPSHOT dependency is built,

意思是依赖于快照的构建意思是依赖于快照的构建,当代码有更新时就构建项目。

下面的Build periodically和Poll SCM可以设置定时自动构建,这里我暂时不设置。

jenkins构建git maven项目 jenkins自动构建git_tomcat_08

Pre Steps选项用来配置构建前的工作,这里不作更改。

因为是Maven项目,Build选项有Root POM和Goals and options的设置,
使用默认的打包应该就可以。

 

5.配置构建成功后的动作,添加shell

Post Steps选项设置构建完成后的动作,
这里我设置为将war包拷贝到Tomcat目录,删除项目原来的内容文件夹,并重启Tomcat

选择Run only if build succeeds or is unstable ,点击添加Execute Shell:


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


28


29


30


31


32


33


34


35


36


37


38


39


40


#!/bin/bash 


#copy file and restart tomcat


 


tomcat_path= /usr/local/tomcat2


project=easy-springmvc-maven


war_name=easy-springmvc-maven.war


war_path=http: //192 .168.106.128:8080 /jenkins/job/jeekins-test/ws/target


server_port=8082


file_path= /home/bingyue/ .jenkins /jobs/jeekins-test/workspace/target


 


now=$( date  + "%Y%m%d%H%M%S" )


echo  "the shell execute time is ${now}"


 


echo  ` lsof  -n -P -t -i :${server_port}`


tomcat_pid=` lsof  -n -P -t -i :${server_port}`


echo  "the tomcat_pid is ${tomcat_pid}"


 


if  "${tomcat_pid}"  !=  ""  ];  then 


kill  -9 $tomcat_pid


echo  "kill the server"


fi 


 


echo  "rm ${tomcat_path}/webapps/${war_name}"


rm  ${tomcat_path} /webapps/ ${war_name}


 


echo  "rm -rf ${tomcat_path}/webapps/${project}"


rm  -rf ${tomcat_path} /webapps/ ${project}


 


cd  $file_path


if  [ -f ${war_name} ];  then 


cp  ${war_name} ${tomcat_path} /webapps


else


echo  "${war_name} unexists"


fi


 


export  JAVA_HOME= /data/jdk7


export  CATALINA_HOME2= /usr/local/apache-tomcat-2


export  CATALINA_BASE2= /usr/local/apache-tomcat-2


$tomcat_path /bin/startup .sh


echo  "server restarted"



说明几点:

这次学习是使用了单台虚拟机里的两个Tomcat,所以端口号有改变,

我为了方便测试,直接访问了Jenkins的隐藏目录/.jenkins/文件夹,

实际应用中,Jenkins通常都是应用在分布式系统,同时部署多台服务器,一般都是访问Jenkins所在的机器上下载war包来部署

我们测试的时候也可以直接修改Jenkins主目录。

最下面的BuildSetting 可以设置邮件收件人列表等,在每次构建结束后可以将相关信息发送到邮箱。

jenkins构建git maven项目 jenkins自动构建git_运维_09

6.构建项目

完成相应的设置后,就可以进行项目的构建。
在任务列表页点击Play的按钮,或者进入任务然后选择Build Now

首页左下角的栏目显示正在构建的项目状态:

jenkins构建git maven项目 jenkins自动构建git_Jenkins_10

点击可以查看控制台输出和日志:

 

jenkins构建git maven项目 jenkins自动构建git_tomcat_11

构建成功,项目状态为蓝色,失败是红色。

打包成功后,可以进入workspace查看文件:
http://SERVER_PATH:8080/jenkins/job/jeekins-test/ws/target/easy-springmvc-maven.war

现在打开刚才的Tomcat路径,如果控制台没有错误输出,构建的项目应该可以正常访问:

jenkins构建git maven项目 jenkins自动构建git_git_12

构建成功!

 

7.遇到的问题

  • 部署到Tomcat时Jenkins启动报错,war包无法解压:

Invalid or unreadable WAR file : error in opening zip file
检查发现是文件下载不完全,重新下载上传后解决。jenkins.war文件有60多MB,使用前记得检查文件完整性。

  • 首次进入提示Your container doesn’t use UTF-8 to decode URLs. If you use non-ASCII characters as a job name etc, this will cause problems.

点击Jenkins提示已经给出了解决方案:
Some versions of Tomcat (such as 5.0.28) uses iso-8859-1 to decode URLs, which is in a clear violation of the relevant RFCs. To fix this problem, add the following URIEncoding attribute to the connector definition in $TOMCAT_HOME/conf/server.xml.
<Connector port="8080" URIEncoding="UTF-8"/>

  • Sending e-mails to: name@gmail.com

ERROR: Could not connect to SMTP host: localhost, port: 25
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;

 本地的SMTP25端口没有打开。