Jenkins 搭建企业实战案例 (发布与回滚)

让我们的代码部署变得easy,不再难,Jenkins是一个可扩展的持续集成引擎,是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。Jenkins非常易于安装和配置,简单易用。本次环境模拟一整套企业生产流程

Jenkins 搭建企业实战案例 (发布与回滚)

Jenkins

2019年04月24日

相关文章

Jenkins 前端自动发布 [git 钩子]

持续集成之Jenkins+Gitlab简介 [一]

环境说明

服务器IP 服务 备注 10.4.82.119 Jenkins 第一次配置不会的可以按照我的步骤来 10.4.82.120 gitlab、tomcat 测试环境tomcat端口8083 10.4.82.115 tomcat 测试环境tomcat端口8083 Jenkins发布回滚流程

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155353-83bd3d886dfce1d.png-189.1kB

一、Jenkins介绍及安装

安装和介绍都是老生常谈的内容了,说了N+1+2+3+4+5+…..的话题了,这里直接跳过了

Jenkins yum安装https://www.abcdocker.com/abcdocker/3174

Jenkins War安装https://www.abcdocker.com/abcdocker/2897

二、Jenkins 安装配置

安装完成后的界面如下

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155353-f4b988ddf6c90d3.png-80.8kB

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155353-1ef2c28a88e32fe.png-119.7kB

Jenkins插件安装

Jenkins主要依赖于插件化,我们进行插件的安装

Maven Integration Maven编译插件

Role-based Authorization Strategy 权限控制插件 ## 标题 ##

AnsiColor 输出Log颜色优化插件

安装插件的步骤不详细讲解了 https://i4t.com/3174.html详细介绍

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155354-ffbd8922ab52ba9.png-125.9kB

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155354-653871e60b20eac.png-139.3kB

Jenkins 全局配置

1.配置jdk环境
下载
wget http://down.i4t.com/jdk1.8.0_66.tar.gz
$ tar zxf jdk1.8.0_66.tar.gz -C /usr/local/
$ ln –s /usr/local/jdk1.8.0_66 /usr/local/jdk
$ vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
$ java -version

检查
$ java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

2.配置maven环境

$ wget  http://down.i4t.com/apache-maven-3.6.1-bin.tar.gz
$ tar xf apache-maven-3.6.0-bin.tar.gz
$ mv apache-maven-3.6.0-bin.tar.gz /usr/local/
$ ln -s /usr/local/apache-maven-3.6.0/ /usr/local/maven
$ vim /etc/profile
export M2_HOME=/usr/local/maven
export PATH=${M2_HOME}/bin:$PATH
$source /etc/profile
验证
$ mvn -v
Apache Maven 3.6.0 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T03:58:13-04:00)
Maven home: /usr/local/maven
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /usr/local/jdk1.8.0_151/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-327.el7.x86_64", arch: "amd64", family: "unix"

我们需要配置Java环境,以及maven等

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155354-6fadc8f1936d6c1.jpg-274.9kB

配置JDK环境

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155355-101b61a80ad098a.png-87.2kB

配置Maven环境

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155356-7a126b4237fbcda.png-48.4kB

下面进行gitlab安装,用于上传测试代码,Jenkins基本上已经配置完毕!

三、gitlab 安装及配置

Gitlab安装目前只需要配置一个yum源,yum install 安装即可

gitlab安装文档https://i4t.com/2041.html

我这里提供了一份hello 的Java代码用于测试

一共分为2步

1.在gitlab web界面创建test项目

2.将Java代码下载push到gitl

代码下载
wget http://down.i4t.com/hello-world-war-master.zip
[root@abcdocker hello-world-war-master]# yum install -y git && git remote add origin http://10.4.82.120/root/test.git

#这里需要在gitlab上创建项目,并且复制git地址进行push

[root@abcdocker hello-world-war-master]# git push -u origin master
Username for 'http://10.4.82.120': root
Password for 'http://root@10.4.82.120':
fatal: Authentication failed for 'http://10.4.82.120/root/test.git/'

[root@abcdocker hello-world-war-master]# git push -u origin master
Username for 'http://10.4.82.120': root
Password for 'http://root@10.4.82.120':
Counting objects: 16, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (16/16), 4.64 KiB | 0 bytes/s, done.
Total 16 (delta 0), reused 0 (delta 0)
To http://10.4.82.120/root/test.git
 * [new branch]      master -> master

当我们将测试代码上传完毕后

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155356-2d70908faf3c2ab.png-198.9kB

四、创建项目演示

准备工作

1.tomcat 地址相同 (多套情况)

2.tomcat 端口相同 (多套情况)

3.web 服务器添加tomcat重启脚本

4.Jenkins – web免密 (Jenkins-tomcat服务器免密)

5.修改好项目名

进入Jenkins,进行创建项目

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155356-41e724ba38500f1.png-307kB

简单说一下,我这里环境的配置

tomcat路径(多套环境只需要改后缀和端口)
/usr/local/abcdocker-test

tomcat端口 8083

tomcat服务器  10.4.82.115  10.4.82.120
我们进入82.115和82.120服务器,配置jdk和tomcat,设置端口

配置Tomcat JDK
wget http://down.i4t.com/jdk1.8.0_66.tar.gz
$ tar zxf jdk1.8.0_66.tar.gz -C /usr/local/
$ ln –s /usr/local/jdk1.8.0_66 /usr/local/jdk
$ vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH
$ java -version

下载Tomcat
wget wget http://down.i4t.com/apache-tomcat-8.5.39.tar.gz
tar xf apache-tomcat-8.5.39.tar.gz
mv apache-tomcat-8.5.39 abcdocker1-test

#修改端口

sed -i s#8080#8083#g abcdocker1-test/conf/server.xml
## 将默认端口修改为8083

我们先访问默认tomcat,检查是否会因为JDK环境问题导致默认tomcat就无法启动

访问地址ip:8083 tomcat安装配置参考

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155357-513d2b2b8222619.png-364.7kB

现在我们的tomcat测试基本上没有问题

配置Jenkins

1.配置Jenkins 参数化构建

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155358-e12ecef42032542.png-83.6kB

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155358-4ceb417a11c421c.png-133.6kB

[ 1 ] 创建选项参数

[ 2 ] 创建Git commit版本参数

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155358-ab833fc90bde43e.png-57.6kB

2.Git地址配置

我们通过https://i4t.com/2041.html安装完gitlab之后,将代码上传完毕。这里需要输入项目的git地址

温馨提示:我这里已经将测试代码上传到gitlab上,怎么上传代码看上面的步骤吧。

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155358-60bd59534ff6bc1.png-245.8kB

复制git地址之后需要配置一点,那就是gitlab的地址。有2种认证方式,一种是通过账号密码,在Jenkins上创建账号密码,另外就是通过key来认证。key认证实际上就是复制Jenkins服务器的ssh公钥到gitlab上的SSH key选项。 我这里以账号密码为主

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155362-9c7f3661a11171d.png-111.1kB

配置完成后

只要没有红色报错即可

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155359-8d8280600f2bf87.png-91.9kB

3.日志代码高亮配置

其他选项我们不配置,这里只配置一个日志输出高亮的参数。 没有的安装上面的插件

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155359-757fe9fd73bf451.png-89.3kB

4.maven编译参数配置

我这里的编译参数就是maven默认的编译参数,具体不讲解了。每个公司的环境不一样

clean package -Dmaven.test.skip=true
测试Jenkins就用我这个配置就OK

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155359-001de975b5ff6a4.png-41.8kB

5.构建完执行脚本

我们需要将构建完执行脚本点开,默认Jenkins不配置shell脚本,Jenkins只是将程序设置成war包,存储下来。这样并没有发布到web服务器上

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155359-37977d4993eeae7.png-62kB

6.发布及滚回脚本

脚本内容比较多,请直接下载查看

wget http://down.i4t.com/abcdocker-jenkins.txt

脚本解释

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155359-4aaf00fd996571c.png-272.5kB

除了这几个要修改,我们还需要修改tomcat启动和重启脚本

内容如下

下载地址wget http://down.i4t.com/abcdocker-test 还需要添加权限+x

#需要修改一下tomcat启动目录

#!/bin/bash
# description: Tomcat is a Java Servlet Container
. /etc/profile
TOMCAT_HOME=/usr/local/abcdocker1-test
start () {
TOMCAT_PID=`ps -ef |grep "$TOMCAT_HOME" |grep -v "grep" |awk '{print $2}'`
if [ -z $TOMCAT_PID ];then
    /bin/bash $TOMCAT_HOME/bin/startup.sh
else
    echo "$0 is  running"
fi
}
stop () {
TOMCAT_PID=`ps -ef |grep "$TOMCAT_HOME" |grep -v "grep" |awk '{print $2}'`
if [ -z $TOMCAT_PID ];then
        echo "$0 is not running"
else
        echo "shutting down $0"
        kill -9 "$TOMCAT_PID" && echo "PID $TOMCAT_PID killed."
fi
}
status () {
TOMCAT_PID=`ps -ef |grep "$TOMCAT_HOME" |grep -v "grep" |awk '{print $2}'`
if [ -z $TOMCAT_PID ];then
        echo "$0 is not running"
else
        echo "$0 is running PID is $TOMCAT_PID"
fi
}
case $1 in
start)
start
#tail -f $TOMCAT_HOME/logs/catalina.out
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
#tail -f $TOMCAT_HOME/logs/catalina.out
;;
*)
echo "Usage:$0  {start|stop|status|restart}."
;;
esac

我们Jenkins点击保存save,现在Jenkins就配置完毕,我们进行测试

tomcat需要启动正常

启动目录和启动脚本都要写好

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155359-48baa000edb0024.png-330.3kB

访问验证结果

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155357-513d2b2b8222619.png-364.7kB

现在我们开始使用Jenkins发布

1.发布前确认Jenkins和web做免密(我这里已经做了,不介绍) https://i4t.com/1353.html

2.确保tomcat启动正常

3.获取gitlab的git commit地址

我们现在获取git commit地址

git commit版本不区分分支,不一定必须在master分支上,其他分支也会有

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155360-9a2b7c7e1ce28bf.png-225kB

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155360-ebb8d5264db0c2a.png-141.2kB

我这里只配置了test环境,我们就只演示test环境,其他环境脚本配置都差不多

点击项目---->点击构建

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155360-ef2c15657cc4d8a.png-101.2kB

点击小圆圈

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155360-d691846e2bd4471.png-228.7kB

结果说明

git拉代码

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155361-71fd798915280b9.png-217.4kB

Maven编译步骤

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155361-2081685f1c3084d.png-266.2kB

执行shell命令

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155362-09f371dc7e6091a.png-248.5kB

启动服务判断,如果有一台服务启动异常,将会暂停发布。通过nginx upstream_check_module会自动停止异常tomcat请求

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155362-5a2172e9dec398b.png-235kB

同时web服务器会自动在data目录创建备份

发版完毕,在shell脚本部分我们后期可以修改成salt,不推荐使用Publish over SSH主要是由于配置麻烦,环境不好区分。要创建多个Jenkins,使用shell或者引用批量分发脚本最为简单

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155363-d8660cbe0a10b82.png-203.3kB

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155363-9e50c798f5d7a86.png-95kB

发布回滚脚本流程

https://oss.lingkb.com/wp-content/uploads/2020/01/1578155353-83bd3d886dfce1d.png-189.1kB

欢迎评论。
lingkb » Jenkins 搭建企业实战案例 (发布与回滚)