最近在探索jenkins这个功能强大的工具,通过使用这个工具,开发过程中可以为我们提升工作效率。

我的本地环境是 ubuntu16.04、jenkins2.89.4、maven3.5、jdk1.8。
注意:安装jenkins过程中最好在安装的过程中把jenkins默认推荐的插件安装好,这样默认会把一些常用的插件提前安装好,不用到用到时才去查找插件安装。该教程需要安装好git和maven的插件。安装好插件后进入全局工具配置里配置jdk、git、maven等
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

首先,安装好jenkins后新建一个maven项目,如下所示,任务名称随便取,但不能取中文。
最近在学习springBoot这个很火的框架,下面我就以springBoot的一个简单项目来演示jenkins+maven+git集成。
这里写图片描述

可以添加任务的描述
这里写图片描述
由于需要和git集成,所有在源码管理那里选择Git,然后Repository URL输入框那里输入你的git仓库中项目的地址。我这里是用https这种方式来连接的,所以需要配置github的用户名和密码。
这里写图片描述
配置用户名/密码
这里写图片描述
下图是我的jenkins的安装目录,我刚刚创建的任务spring-boot-thymeleaf就存放在workspace路径下。
这里写图片描述
下图Root POM是指当你构建任务时,jenkins从你的git仓库下载下来的项目中的pom.xml文件所在的位置(相对路径),参照上图pom.xml文件所在位置就可知道。
这里写图片描述
由于springBoot内嵌了tomcat容器,所以如果需要把springBoot项目打成war包发布到tomcat服务器需要对项目做一些修改,网上方法有很多,但我觉得下面的这种方法比较好,修改后既可以用其它tomcat启动,也可以使用springBoot内嵌的tomcat启动。
一、修改代码,设置启动配置
程序入口处的类需要继承SpringBootServletInitializer,然后重写configure方法,将SpringBoot入口类设置进去。
这里写图片描述
二、将spring-boot-starter-tomcat的范围设置为provided
设置为provided是在打包时会将该包排除,因为要放到独立的tomcat中运行。
这里写图片描述
下图是配置好的任务,只需要点击立即构建,即可把你git仓库上的springBoot项目下载到jenkins的工作空间,然后jenkins解析项目里的pom.xml文件生成war包,接着执行shell脚本判断tomcat是否在运行中,如果在运行中则先停止,任何把生成的war包拷贝到你指定的tomcat路径下,然后启动tomcat。
这里写图片描述

浏览器输入http://localhost:7080/spring-boot-thymeleaf-1.0/hello
这里写图片描述

下面是我写的shell脚本,基本每行都有注释,都是一些很简单的指令。
其中export BUILD_ID=dontKillMe
这句很重要,如果漏了对BUILD_ID变量的设置,不管你在shell脚本里怎么启动tomcat,都是无法启动成功的,但是可以关闭tomcat。原因是 jenkins在脚本执行结束后,就认为任务结束了,但是脚本启动的相关子程序仍然在运行。由于jenkins认为任务已经结束了,Jenkins默认会在Build结束后Kill掉所有的衍生进程。

 #!/bin/bash
 #copy file and restart tomcat 

export BUILD_ID=dontKillMe

 #我的tomcat路径
tomcat_path=/opt/software/tomcat/apache-tomcat-7.0.57

 #我的war包名称
war_name=spring-boot-thymeleaf-1.0.war  

 #jenkins默认工作空间,构建的时候jenkins会从git服务器上把项目源码下载到workspace路径下
file_path=~/.jenkins/workspace/spring-boot-thymeleaf/spring-boot-thymeleaf/target

tomcat_name='apache-tomcat-7.0.57' #tomcat 完整路劲 或 最后级路劲文件家都可以

 #获取 tomcat_path 所知tomcat 的进程ID
TomcatID=$(ps -ef |grep tomcat |grep -w $tomcat_name|grep -v 'grep'|awk '{print $2}')

echo $TomcatID $tomcat_name

 #判断进程是否存在

if [ $TomcatID ];

       then
                echo "$TomcatID tomcat is starting ................."
                #关闭tomcat
                $tomcat_path/bin/shutdown.sh 
                echo "tomcat stop..................................."
        else
                echo "tomcat not start ............................."
fi

 #休眠3秒
sleep 3s 

echo "rm -rf ${tomcat_path}/webapps/spring-boot-thymeleaf-1.0*"
 #删除tomcat目录下老的war包和解压后的项目目录
rm -rf ${tomcat_path}/webapps/spring-boot-thymeleaf-*

cd $file_path
 #复制jenkins工作空间中生成的war包到tomcat的webapps目录下
cp ${war_name} ${tomcat_path}/webapps/

sleep 5s 

 #$tomcat_path/bin/startup.sh
 #重启tomcat
$tomcat_path/bin/startup.sh

echo "tomcat server restarted"

下面是执行jenkins任务时控制台的输出的信息

In progress控制台输出
Started by user wuzhangwei
Building in workspace /home/wuzhangwei/.jenkins/workspace/spring-boot-thymeleaf
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/Garfield-Lucky/spring-boot-thymeleaf.git # timeout=10
Fetching upstream changes from https://github.com/Garfield-Lucky/spring-boot-thymeleaf.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://github.com/Garfield-Lucky/spring-boot-thymeleaf.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision e89fe70ca8c09b7c21b5bf345a4722773f4f6fee (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f e89fe70ca8c09b7c21b5bf345a4722773f4f6fee
Commit message: "2"
 > git rev-list --no-walk e89fe70ca8c09b7c21b5bf345a4722773f4f6fee # timeout=10
Parsing POMs
Established TCP socket on 41459
[spring-boot-thymeleaf] $ java -cp /home/wuzhangwei/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven35-agent-1.12-alpha-1.jar:/opt/software/apache-maven-3.5.2/boot/plexus-classworlds-2.5.2.jar:/opt/software/apache-maven-3.5.2/conf/logging jenkins.maven3.agent.Maven35Main /opt/software/apache-maven-3.5.2 /opt/software/tomcat/apache-tomcat-7.0.42/webapps/jenkins/WEB-INF/lib/remoting-3.14.jar /home/wuzhangwei/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven35-interceptor-1.12-alpha-1.jar /home/wuzhangwei/.jenkins/plugins/maven-plugin/WEB-INF/lib/maven3-interceptor-commons-1.12-alpha-1.jar 41459
<===[JENKINS REMOTING CAPACITY]===>channel started
Executing Maven:  -B -f /home/wuzhangwei/.jenkins/workspace/spring-boot-thymeleaf/spring-boot-thymeleaf/pom.xml install
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-boot-thymeleaf 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ spring-boot-thymeleaf ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ spring-boot-thymeleaf ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/wuzhangwei/.jenkins/workspace/spring-boot-thymeleaf/spring-boot-thymeleaf/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ spring-boot-thymeleaf ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/wuzhangwei/.jenkins/workspace/spring-boot-thymeleaf/spring-boot-thymeleaf/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ spring-boot-thymeleaf ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ spring-boot-thymeleaf ---
[INFO] No tests to run.
[JENKINS] Recording test results
[INFO] 
[INFO] --- maven-war-plugin:2.6:war (default-war) @ spring-boot-thymeleaf ---
[INFO] Packaging webapp
[INFO] Assembling webapp [spring-boot-thymeleaf] in [/home/wuzhangwei/.jenkins/workspace/spring-boot-thymeleaf/spring-boot-thymeleaf/target/spring-boot-thymeleaf-1.0]
[INFO] Processing war project
[INFO] Webapp assembled in [523 msecs]
[INFO] Building war: /home/wuzhangwei/.jenkins/workspace/spring-boot-thymeleaf/spring-boot-thymeleaf/target/spring-boot-thymeleaf-1.0.war
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.6.RELEASE:repackage (default) @ spring-boot-thymeleaf ---
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ spring-boot-thymeleaf ---
[INFO] Installing /home/wuzhangwei/.jenkins/workspace/spring-boot-thymeleaf/spring-boot-thymeleaf/target/spring-boot-thymeleaf-1.0.war to /home/wuzhangwei/.m2/repository/org/springframework/boot/spring-boot-thymeleaf/1.0/spring-boot-thymeleaf-1.0.war
[INFO] Installing /home/wuzhangwei/.jenkins/workspace/spring-boot-thymeleaf/spring-boot-thymeleaf/pom.xml to /home/wuzhangwei/.m2/repository/org/springframework/boot/spring-boot-thymeleaf/1.0/spring-boot-thymeleaf-1.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 23.788 s
[INFO] Finished at: 2018-04-21T11:19:34+08:00
[INFO] Final Memory: 30M/169M
[INFO] ------------------------------------------------------------------------
[JENKINS] Archiving /home/wuzhangwei/.jenkins/workspace/spring-boot-thymeleaf/spring-boot-thymeleaf/pom.xml to org.springframework.boot/spring-boot-thymeleaf/1.0/spring-boot-thymeleaf-1.0.pom
[JENKINS] Archiving /home/wuzhangwei/.jenkins/workspace/spring-boot-thymeleaf/spring-boot-thymeleaf/target/spring-boot-thymeleaf-1.0.war to org.springframework.boot/spring-boot-thymeleaf/1.0/spring-boot-thymeleaf-1.0.war
[spring-boot-thymeleaf] $ /bin/bash /opt/software/tomcat/apache-tomcat-7.0.42/temp/jenkins8907932664114587944.sh
channel stopped
6348 apache-tomcat-7.0.57
6348 tomcat is starting .................
Using CATALINA_BASE:   /opt/software/tomcat/apache-tomcat-7.0.57
Using CATALINA_HOME:   /opt/software/tomcat/apache-tomcat-7.0.57
Using CATALINA_TMPDIR: /opt/software/tomcat/apache-tomcat-7.0.57/temp
Using JRE_HOME:        /opt/software/java/jdk/jdk1.8.0_74/jre
Using CLASSPATH:       /opt/software/tomcat/apache-tomcat-7.0.57/bin/bootstrap.jar:/opt/software/tomcat/apache-tomcat-7.0.57/bin/tomcat-juli.jar
tomcat stop...................................
rm -rf /opt/software/tomcat/apache-tomcat-7.0.57/webapps/spring-boot-thymeleaf-1.0*
Using CATALINA_BASE:   /opt/software/tomcat/apache-tomcat-7.0.57
Using CATALINA_HOME:   /opt/software/tomcat/apache-tomcat-7.0.57
Using CATALINA_TMPDIR: /opt/software/tomcat/apache-tomcat-7.0.57/temp
Using JRE_HOME:        /opt/software/java/jdk/jdk1.8.0_74/jre
Using CLASSPATH:       /opt/software/tomcat/apache-tomcat-7.0.57/bin/bootstrap.jar:/opt/software/tomcat/apache-tomcat-7.0.57/bin/tomcat-juli.jar
Tomcat started.
tomcat server restarted
Finished: SUCCESS
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐