一、安装环境
主机名 | IP | 所需软件/依赖包 | 用途 | 内存/CPU |
---|---|---|---|---|
Jenkins | 192.168.200.1 | Jenkins、maven、jdk、tomcat、git | 发布服务器 | 4GB/4个 |
Gitlab | 192.168.200.2 | GItlab、Jdk、tomcat | 代码存储/测试机-1 | 4GB/4个 |
Test-2 | 192.168.200.3 | Tomcat、jdk | 测试机-2 | 1G/2个 |
二、总体流程介绍:
使用Jenkins拉取Gitlab上面代码使用Maven进行打包,在使用Jenkins里面规定好shell脚本进行发布/回滚
三、软件下载地址:
Jenkins:
http://mirrors.jenkins.io/war-stable/latest/jenkins.war
Maven:
https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
JDK:
https://www.oracle.com/webapps/redirect/signon?nexturl=https://download.oracle.com/otn/java/jdk/8u251-b08/3d5a2bb8f8d4428bbe94aed7ec7ae784/jdk-8u251-linux-x64.tar.gz
Tomcat:
wget https://downloads.apache.org/tomcat/tomcat-8/v8.5.57/bin/apache-tomcat-8.5.57.tar.gz
Gitlab:
Gitlab使用网络yum部署
四、具体实施
1、Jenkins机器操作
Jdk maven tomcat jenkins搭建
[root@Jenkins ~]# mkdir /soft/ && cd /soft/
[root@Jenkins soft]# wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
[root@Jenkins soft]# wget https://downloads.apache.org/tomcat/tomcat-8/v8.5.57/bin/apache-tomcat-8.5.57.tar.gz
[root@Jenkins soft]# wget https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
[root@Jenkins soft]# JDK手动下载拖到服务器
[root@Jenkins soft]# ls
apache-maven-3.6.3-bin.tar.gz apache-tomcat-8.5.57.tar.gz jdk-8u211-linux-64_(1).tar.gz jenkins.war
建立软件解压目录
[root@Jenkins soft]# mkdir /application/
1.1: 安装git和jdk
[root@Jenkins ~]# yum -y install git
[root@Jenkins soft]# tar xf jdk-8u211-linux-64_(1).tar.gz
[root@Jenkins soft]# mv jdk1.8.0_211/ /application/jdk
[root@Jenkins soft]# echo "export JAVA_HOME=/application/jdk" >> /etc/profile
[root@Jenkins soft]# echo "PATH=
[root@Jenkins soft]# echo "export JAVA_HOME PATH" >> /etc/profile
[root@Jenkins soft]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
1.2:安装Maven
[root@Jenkins ~]# cd /soft/
[root@Jenkins soft]# tar xf apache-maven-3.6.3-bin.tar.gz
[root@Jenkins soft]# mv apache-maven-3.6.3 /application/maven
[root@Jenkins maven]# cd /application/
[root@Jenkins application]# ls
jdk maven
[root@Jenkins application]# echo "export MVN_HOME=/application/maven" >> /etc/profile
[root@Jenkins application]# echo "export PATH=
[root@Jenkins application]# source /etc/profile
[root@Jenkins application]# mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /application/maven
Java version: 1.8.0_211, vendor: Oracle Corporation, runtime: /application/jdk/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1062.el7.x86_64", arch: "amd64", family: "unix"
#设置修改maven源地址(默认的源为国外,下载速度非常慢 会导致报错,修改为阿里源即可 )
[root@Jenkins ~]# vim /application/maven/conf/settings.xml
#在158行下写入以下内容
159 <mirror>
160 <id>aliyun</id>
161 <mirrorOf>central</mirrorOf>
162 <name>aliyun maven</name>
163 <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
164 </mirror>
1.3: 安装tomcat
[root@Jenkins application]# cd /soft/
[root@Jenkins soft]# ls
apache-maven-3.6.3-bin.tar.gz apache-tomcat-8.5.57.tar.gz jdk-8u211-linux-64_(1).tar.gz jenkins.war
[root@Jenkins soft]# tar xf apache-tomcat-8.5.57.tar.gz
[root@Jenkins soft]# mv apache-tomcat-8.5.57 /application/tomcat
#启动tomcat进行测试
[root@Jenkins soft]# /application/tomcat/bin/startup.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /application/jdk
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Tomcat started.
#查看是否启动成功
[root@Jenkins soft]# netstat -anpt | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 23459/java
tcp6 0 0 ::1:53282 ::1:8080 TIME_WAIT -
#启动成功可以去网页访问一下http://ip:8080
!!!表示启动成功
1.4: 把Jenkins.war包放到tomcat的webapps文件下进行启动jenkins,并编写tomcat脚本
[root@Jenkins soft]# cp jenkins.war /application/tomcat/webapps/
#编写tomcat启动脚本/etc/init.d/tomcat
[root@Jenkins soft]# cat /etc/init.d/tomcat
#!/bin/bash
# description: Tomcat is a Java Servlet Container
TOMCAT_HOME=/application/tomcat
start () {
TOMCAT_PID=`ps -ef |grep "
if [ -z $TOMCAT_PID ];then
source /etc/profile
/bin/bash $TOMCAT_HOME/bin/startup.sh
else
echo "$0 is running"
fi
}
stop () {
TOMCAT_PID=`ps -ef |grep "
if [ -z $TOMCAT_PID ];then
echo "$0 is not running"
else
echo "shutting down $0"
kill -9 "
fi
}
status () {
TOMCAT_PID=`ps -ef |grep "
if [ -z $TOMCAT_PID ];then
echo "$0 is not running"
else
echo "
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
1.5:Jenkins去访问网页,去下载推荐依赖包访问格式http://ip:8080/jenkins
根据图中提示去Jenkins机器获得jenkins随机密码
[root@Jenkins soft]# cat /root/.jenkins/secrets/initialAdminPassword
54f885045e774d6e9f405461cfd7c35a
输入密码完成之后在页面点击继续会出现下面这个页面点击安装推荐插件
如果有几个失败的请重试,不然到后面麻烦 ----》设置用户名
1.6:到这里jenkins搭建完成,咱们在配置上jdk和maven的绝对路径
找到jdk、maven这两个选项根据自己目录进行填写
1.7: Jenkins为两台测试机器进行分发密钥:
[root@Jenkins ~]# ssh-copy-id 192.168.200.2
[root@Jenkins ~]# ssh-copy-id 192.168.200.3
!!!!到这里Jenkins已经搭建好,搭建完Gitlab和测试机器上面的tomcat就进行测试
2.Gitlab机器操作
- 附带测试机-1安装
2.1新建 /etc/yum.repos.d/gitlab_gitlab-ce.repo,
[root@Gitlab ~]# cd /etc/yum.repos.d/
[root@Gitlab yum.repos.d]# cat gitlab_gitlab-ce.repo
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
[root@Gitlab yum.repos.d]# yum clean all && yum makecache
2.2 安装gitlab并且初始化,进行简单配置
[root@Gitlab yum.repos.d]# yum -y install gitlab-ce
#初始化gitlab,时间较长
[root@Gitlab yum.repos.d]# gitlab-ctl reconfigure
#由于gitlab克隆默认地址不对。这里需要修改成自己的地址
[root@Gitlab ~]# vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
#修改完配置之后重启Gitlab
[root@Gitlab ~]# gitlab-ctl restart
2.3 登陆Gitlab网页版,建立一个简单项目访问格式http://ip
2.4: 建立简单项目
随便找一个git客户端进行测试使用命令如下
[root@Jenkins ~]# git clone http://192.168.200.2/root/test.git
正克隆到 'test'...
warning: 您似乎克隆了一个空版本库。
[root@Jenkins ~]# cd test/
[root@Jenkins test]# touch README.md
[root@Jenkins test]# git add README.md
[root@Jenkins test]# git commit -m "add README"
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'root@Jenkins.(none)')
#设置这个git的名字和邮箱地址。
[root@Jenkins test]# git config --global user.email "you@example.com"
[root@Jenkins test]# git config --global user.name "Your Name"
[root@Jenkins test]# git commit -m "add README"
[master(根提交) 6ef6564] add README
1 file changed, 0 insertions( ), 0 deletions(-)
create mode 100644 README.md
[root@Jenkins test]# git push -u origin master
Username for 'http://192.168.200.2': root
Password for 'http://root@192.168.200.2':
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.200.2/root/test.git
* [new branch] master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
通过上面可以看出gitlab正常可以服务。去网页查看一下是否正常
以有数据,Gitlab服务正常运行
2.5建立一个正式项目到后面提供给Jenkins使用,需要给jenkins提供地址
根据下图提示,进行测试看看项目正常不正常
[root@Jenkins ~]# git clone http://192.168.200.2/root/hello-word-war.git
正克隆到 'hello-word-war'...
warning: 您似乎克隆了一个空版本库。
[root@Jenkins ~]# cd hello-word-war
[root@Jenkins hello-word-war]# touch README.md
[root@Jenkins hello-word-war]# git add README.md
[root@Jenkins hello-word-war]# git commit -m "add README"
[master(根提交) e652e10] add README
1 file changed, 0 insertions( ), 0 deletions(-)
create mode 100644 README.md
[root@Jenkins hello-word-war]# git push -u origin master
Username for 'http://192.168.200.2': root
Password for 'http://root@192.168.200.2':
Counting objects: 3, done.
Writing objects: 100% (3/3), 213 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.200.2/root/hello-word-war.git
* [new branch] master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
根据测试新建项目可用
我在码云上找了一个未打包的war包项目,感谢提供Elvin
项目地址为: https://gitee.com/almi/helloworld.git
把这个的地址克隆下来发布到自己的gitlab操作如下
[root@Jenkins ~]# git clone https://gitee.com/almi/helloworld.git
正克隆到 'helloworld'...
remote: Enumerating objects: 58, done.
remote: Counting objects: 100% (58/58), done.
remote: Compressing objects: 100% (47/47), done.
remote: Total 58 (delta 17), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (58/58), done.
[root@Jenkins ~]# ls
anaconda-ks.cfg hello-word-war helloworld jenkins.war test
[root@Jenkins ~]# ls
anaconda-ks.cfg hello-word-war helloworld jenkins.war test
[root@Jenkins ~]# cd helloworld/
[root@Jenkins helloworld]# ls
build.sh Dockerfile pom.xml README.md src
[root@Jenkins helloworld]# mv * /root/hello-word-war/
mv:是否覆盖"/root/hello-word-war/README.md"?y
[root@Jenkins helloworld]# cd
[root@Jenkins ~]# cd hello-word-war/
[root@Jenkins hello-word-war]# git add *
[root@Jenkins hello-word-war]# git commit -m "转移代码"
[master 61d1c94] 转移代码
7 files changed, 83 insertions( )
create mode 100644 Dockerfile
create mode 100644 build.sh
create mode 100644 pom.xml
create mode 100644 src/main/webapp/WEB-INF/web.xml
create mode 100644 src/main/webapp/index.html
create mode 100644 src/main/webapp/index.jsp
[root@Jenkins hello-word-war]# git push origin master
Username for 'http://192.168.200.2': root
Password for 'http://root@192.168.200.2':
Counting objects: 15, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (13/13), 2.02 KiB | 0 bytes/s, done.
Total 13 (delta 0), reused 0 (delta 0)
To http://192.168.200.2/root/hello-word-war.git
e652e10..61d1c94 master -> master
登陆Gitlab看看是否上传成功:
已经上传成功,把文中我圈出来的地方复制下来等会拿到jenkins上面
(1)测试机-1安装
[root@Gitlab ~]# mkdir /application
[root@Gitlab ~]# mkdir /soft
[root@Gitlab soft]#
wget https://downloads.apache.org/tomcat/tomcat-8/v8.5.57/bin/apache-tomcat-8.5.57.tar.gz
[root@localhost soft]# JDK手动下载拖到服务器
[root@Gitlab soft]# ls
apache-tomcat-8.5.57.tar.gz jdk-8u211-linux-64_(1).tar.gz
[root@Gitlab soft]# tar xf jdk-8u211-linux-64_(1).tar.gz
[root@Gitlab soft]# mv jdk1.8.0_211/ /application/jdk
2.6 设置JDK环境变量
[root@Gitlab soft]# tail -n 3/etc/profile
export JAVA_HOME=/application/jdk
PATH=JAVA_HOME/bin:PATH
export JAVA_HOME PATH
[root@Gitlab soft]# source /etc/profile
[root@Gitlab soft]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
2.7 搭建tomcat并且修改tomcat端口号改为8000,写启动脚本,不改会和gitlab冲突
[root@Gitlab soft]# tar xf apache-tomcat-8.5.57.tar.gz
[root@Gitlab soft]# mv apache-tomcat-8.5.57 /application/tomcat
[root@Gitlab soft]# vim /application/tomcat/conf/server.xml
#t退出保存编写tomcat启动脚本
[root@Gitlab soft]# cat /etc/init.d/tomcat
代码语言:javascript复制#!/bin/bash
# description: Tomcat is a Java Servlet Container
TOMCAT_HOME=/application/tomcat
start () {
TOMCAT_PID=`ps -ef |grep "
if [ -z $TOMCAT_PID ];then
source /etc/profile
/bin/bash $TOMCAT_HOME/bin/startup.sh
else
echo "$0 is running"
fi
}
stop () {
TOMCAT_PID=`ps -ef |grep "
if [ -z $TOMCAT_PID ];then
echo "$0 is not running"
else
echo "shutting down $0"
kill -9 "
fi
}
status () {
TOMCAT_PID=`ps -ef |grep "
if [ -z $TOMCAT_PID ];then
echo "$0 is not running"
else
echo "
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
#赋予权限
[root@Gitlab soft]# chmod x /etc/init.d/tomcat
#启动tomcat并且访问tomcat
[root@Gitlab soft]# /etc/init.d/tomcat start
3: Test-2重复测试机-1安装即可
(不再演示test-2测试机安装)
4: Jenkins新建一个maven项目
4.1: 安装maven插件
4.2: 等待安装完成,新建一个maven项目
4.3: 设置两个参数,是添加两个Choince Parameter参数 填充内容如下图
再添加一个文本内容
设置Git地址
填写maven打包命令
添加shell脚本
脚本内容如下
代码语言:javascript复制##Deploy tomcat
test_host="192.168.200.2"
stag_host="192.168.200.3"
war_bak="/data/war/bak"
tomcat_deploy="/application/tomcat/webapps"
#WAR_PATH="
WAR_PATH="
port="8000"
if [ "${git}" = "" ];then
echo "请输入git版本 #############"
exit 1
else
## 判断发布 or 回滚
if [ "${status}" = "Deploy" ];then
### 判断是否为测试环境
if [ "${deploy}" = "test" ];then
### 构建主机
for i in ${test_host}
do
ssh {i} "mkdir -p {JOB_NAME}/{git}"
scp {WAR_PATH} {i}:{war_bak}/{JOB_NAME}/
ssh {i} rm -rf {tomcat_deploy}/*
ssh {i} cp {war_bak}/{JOB_NAME}/{git}/ROOT.war
ssh ${i} /etc/init.d/tomcat restart
### 判断tomcat是否正常
for http in `seq 1 5`
do
tomcat_status=`curl -I {i}:{port} -s|awk -F "[ ]" '{print
if [[ "
echo -e "请稍等,服务启动中........"
sleep 4
else
echo -e "
break
fi
done
if [[ "
if [[ "${tomcat_status}" = "" ]];then
echo -e " 构建 ${i}服务启动异常 "
exit 1
fi
echo -e "构建
fi
done
echo -e " 本次构建
## 判断为预发布环境
代码语言:javascript复制 elif [ "${deploy}" = "stag" ];then
for i in "${stag_host}"
do
ssh {i} "mkdir -p {JOB_NAME}/{git}"
scp {WAR_PATH} {i}:{war_bak}/{JOB_NAME}/
ssh {i} rm -rf {tomcat_deploy}/*
ssh {i} cp {war_bak}/{JOB_NAME}/{git}/ROOT.war
ssh ${i} /etc/init.d/tomcat restart
### 判断tomcat是否正常
代码语言:javascript复制 for http in `seq 1 5`
do
tomcat_status=`curl -I {i}:{port} -s|awk -F "[ ]" '{print
if [[ "
echo -e "请稍等,服务启动中........"
sleep 5
else
echo -e "构建
break
fi
done
if [[ "
if [[ "${tomcat_status}" = "" ]];then
echo -e "构建 ${i}服务启动异常"
exit 1
fi
echo -e "构建
fi
done
echo -e "本次构建
fi
### 回滚操作
代码语言:javascript复制elif [[ "${status}" = "RollBack" ]];then
### 判断回滚环境及主机
代码语言:javascript复制 if [ "${deploy}" = "test" ];then
for i in ${test_host}
do
ssh {i} "[ -d {JOB_NAME}/{git} ]"
if [ $? -ne '0' ];then
echo -e "git commit 回滚目录不存在,环境
exit 3
else
echo -e "准备回滚操作 本次回滚环境
sleep 3
fi
ssh {i} "mkdir -p {JOB_NAME}/{git}_
ssh {i} "cp -r {war_bak}/{JOB_NAME}/{git}_{status}_rollback/"
ssh {i} "rm -rf
ssh {i} "cp -r {JOB_NAME}/{git}/*.war
ssh ${i} /etc/init.d/tomcat restart
### 判断tomcat是否正常
for http in `seq 1 5`
do
tomcat_status=`curl -I {i}:{port} -s|awk -F "[ ]" '{print
if [[ "
echo -e "请稍等,服务启动中........"
sleep 5
else
echo -e "构建
break
fi
done
if [[ "
if [[ "${tomcat_status}" = "" ]];then
echo -e "构建 ${i}服务启动异常"
exit 1
fi
echo -e "构建
fi
done
elif [ "${deploy}" = "stag" ];then
for i in ${stag_host}
do
ssh {i} "[ -d {JOB_NAME}/{git} ]"
if [ $? -ne '0' ];then
echo -e "git commit 回滚目录不存在,环境
exit 3
else
echo -e "准备回滚操作 本次回滚环境
sleep 3
fi
ssh {i} "mkdir -p {JOB_NAME}/{git}_
ssh {i} "cp -r {war_bak}/{JOB_NAME}/{git}_{status}_rollback/"
ssh {i} "rm -rf
ssh {i} "cp -r {JOB_NAME}/{git}/*.war
ssh ${i} /etc/init.d/tomcat restart
### 判断tomcat是否正常
代码语言:javascript复制 for http in `seq 1 5`
do
tomcat_status=`curl -I {i}:{port} -s|awk -F "[ ]" '{print
if [[ "
echo -e "请稍等,服务启动中........"
sleep 5
else
echo -e "构建
break
fi
done
if [[ "
if [[ "${tomcat_status}" = "" ]];then
echo -e "构建 ${i}服务启动异常"
exit 1
fi
echo -e "构建
fi
done
### 判断测试环境fi结束
代码语言:javascript复制 fi
fi
#### fi是判断是否有git地址的结束
fi
添加完脚本保存退出
这个git地址是需要去gitlab上面获取的
填写完git commit号之后就开始构建
蓝色表示构建成功,红色就gg了
构建完成之后去访问升级的及其这个地方test我在脚本定义的是192.168.200.1:8000
看看升级是否成功
表示升级成功!!
添加点内容再次升级
root@Jenkins ~]# cd hello-word-war/
build.sh Dockerfile .git/ pom.xml README.md src/
[root@Jenkins ~]# cd hello-word-war/src/main/webapp/
[root@Jenkins webapp]# ls
index.html index.jsp WEB-INF
#修改内容,修改完成之后提交到gitlab上面,去gitlab重新获取新的commit号再复制到jenkins,再次升级
[root@Jenkins webapp]# vim index.html
[root@Jenkins webapp]# git add index.html
[root@Jenkins webapp]# git commit -m "update index.html"
[master 8b70643] update index.html
1 file changed, 1 insertion( )
[root@Jenkins webapp]# git push origin master
Username for 'http://192.168.200.2': root
Password for 'http://root@192.168.200.2':
Counting objects: 11, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 469 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To http://192.168.200.2/root/hello-word-war.git
61d1c94..8b70643 master -> master
去gitlab获取新的commit号,复制到jenkins
复制到Jenkins,可以看到正在构建,构建成功去在去访问
再次访问已经升级成功
去gitlab找到上一次的commit号进行回滚操作,看看回滚正不正常
复制这个commit号去jenkins进行回滚操作
构建完成之后去看看是否回滚成功
已经看到回滚成功
自动化一键升级已经构成
参考文章:
https://blog.51cto.com/14227204/2492464
本文PDF和WORD版已经放星球中