使用Jenkins实现自动持续集成部署springboot2项目到Tomcat。
Linux系统为centos7,并且需要有能被外网访问的IP(建议使用云服务器)和能够上网
代码管理工具为git
1.安装Tomcat
打开Tomcat的官网下载压缩包
使用远程工具拷贝到Linux系统的/opt目录下
解压压缩包,剪切到/opt/local/tomcat8目录下
[root@instance-uhumcptp opt]# tar -zxvf apache-tomcat-8.0.53.tar.gz
[root@instance-uhumcptp opt]# mv apache-tomcat-8.0.53 /usr/local/tomcat8
[root@instance-uhumcptp opt]# cd /usr/local/tomcat8/
[root@instance-uhumcptp tomcat8]# pwd
/usr/local/tomcat8
2.下载Jenkins
打开Jenkins的官网,点击Download,点击Generic Java package (.war)
拷贝jenkins.war到Linux系统下的/usr/local/tomcat8/webapps
3.下载和安装git
打开git的下载地址:https://mirrors.edge.kernel.org/pub/software/scm/git/
我下载的是:git-2.18.0.tar.gz
通过远程工具拷贝到Linux系统的/opt目录下
安装编译git 时需要的包
yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel
yum install -y gcc perl-ExtUtils-MakeMaker
删除已有的git
yum remove git
解压文件,并移动到/usr/src目录下
[root@instance-uhumcptp src]# tar -zxvf git-2.18.0.tar.gz
[root@instance-uhumcptp opt]# mv git-2.18.0 /usr/src
[root@instance-uhumcptp opt]# cd /usr/src
[root@instance-uhumcptp src]# ll
total 28
drwxr-xr-x. 2 root root 4096 Apr 11 12:59 debug
drwxrwxr-x 24 root root 20480 Jun 22 01:18 git-2.18.0
drwxr-xr-x. 2 root root 4096 Apr 11 12:59 kernels
编译安装
[root@instance-uhumcptp git-2.18.0]# pwd
/usr/src/git-2.18.0
[root@instance-uhumcptp git-2.18.0]# make prefix=/usr/local/git all
[root@instance-uhumcptp git-2.18.0]# make prefix=/usr/local/git install
配置环境变量
在profile文件添加一行:export PATH=$PATH:/usr/local/git/bin
[root@instance-uhumcptp git-2.18.0]# vim /etc/profile
使刚添加的环境配置生效
[root@instance-uhumcptp git-2.18.0]# source /etc/profile
查看版本号
[root@instance-uhumcptp ~]# git --version
4.安装jdk
打开官网地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
拷贝到Linux系统下的/opt目录
解压文件
[root@instance-uhumcptp opt]# tar -zxvf jdk-8u181-linux-x64.tar.gz
[root@instance-uhumcptp opt]# mv jdk1.8.0_181 /usr/local/jdk8
[root@instance-uhumcptp opt]# vim /etc/profile
配置环境变量:在 /etc/profile文件中添加
JAVA_HOME=/usr/local/jdk8
PATH=$PATH:/usr/local/git/bin:$JAVA_HOME/bin
export JAVA_HOME PATH
使配置的环境变量生效
[root@instance-uhumcptp opt]# source /etc/profile
测试jdk是否安装成功
[root@instance-uhumcptp opt]# java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
5.安装maven
打开maven的下载地址:http://maven.apache.org/download.cgi
解压文件
[root@instance-uhumcptp conf]# tar -zxvf apache-maven-3.5.4-bin.tar.gz
[root@instance-uhumcptp conf]# mv apache-maven-3.5.4 /usr/local/maven3
配置环境变量
[root@instance-uhumcptp conf]# vim /etc/profile
添加如下信息(包括前面已经添加的环境变量)
JAVA_HOME=/usr/local/jdk8
MAVEN_HOME=/usr/local/maven3
PATH=$PATH:/usr/local/git/bin:$JAVA_HOME/bin:$MAVEN_HOME/bin
export JAVA_HOME MAVEN_HOME PATH
使环境变量生效
[root@instance-uhumcptp conf]# source /etc/profile
测试maven是否安装成功
[root@instance-uhumcptp conf]# mvn -version
1.设置防火墙,允许8080端口被外网访问(因为我使用的是百度云,需要在百度云管理中心配置)
查看已经开放的端口
firewall-cmd --list-ports
开启8080端口
命令含义:
–zone #作用域
–add-port=80/tcp #添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效
firewall-cmd --zone=public --add-port=8080/tcp --permanent
重启防火墙
firewall-cmd --reload
2.启动Tomcat服务器
[root@instance-uhumcptp bin]# cd /usr/local/tomcat8/bin/
3.用浏览器访问服务器地址
http://你的IP:8080/jenkins
4.输入管理员密码
密码位置
[root@instance-uhumcptp bin]# cat /root/.jenkins/secrets/initialAdminPassword
86ed74608ff04be7b0ec1442d88eb106
我本地的密码为:86ed74608ff04be7b0ec1442d88eb106
6.系统初始化配置
配置maven,前面是配置maven的配置文件,这次是配置maven的安装目录
现在,我们每次git push代码后,Jenkins都会进行自动部署。
配置文件位置:/usr/local/tomcat8/conf/tomcat-users.xml,配置完后重启Tomcat服务器
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="tomcat_user" password="123456"
roles="manager-gui,manager-script,manager-jmx,manager-status" />
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.zkanegroupId>
<artifactId>jenkins-demoartifactId>
<version>1.0.0version>
<packaging>warpackaging>
<name>jenkins-demoname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.3.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(JenkinsDemoApplication.class);
}
}