![图片[1]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-195.png)
jenkins根据业务场景的不同,提供了多种风格的任务,默认是自由风格任务,通过安装插件,还可以支持其它风格的插件
job的风格分类
- 自由风格freestyle: 根据内部的配置属性,实现各种场景的风格
- 流水线pipeline
自由风格的任务提供了下面的组成
- 通用配置:当前任务的基本配置,历史记录、存储数据、认证、存储目录等
- 源码管理:指定当前任务依赖的代码仓库地址(仓库的分支)
- 构建触发器:在什么情况下,才会自动执行当前的任务
- 构建环境:构建过程中,依赖的环境变量等
- 构建:当前的代码后续操作
- 构建后动作:构建任务成功后,我们可以做的事情,发送邮件、提交代码标签、触发其他任务、等等
构建支持变量
http://jenkins-server:8080/env-vars.html/
构建状态
晴雨表主要是针对一个任务的整体执行成功比例来算的。80%成功表示太阳。
![图片[2]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-196.png)
1、jenkins 创建任务
![图片[3]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-197.png)
2、配置git项目地址和凭证
![图片[4]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-198.png)
3、构建的环境变量
![图片[5]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-199.png)
4、测试构建项目
![图片[6]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-200.png)
![图片[7]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-201.png)
![图片[8]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-202.png)
# 服务器验证数据
[root@jenkins01 ~]# ll -d /var/lib/jenkins/workspace/testproject-test
drwxr-xr-x 3 root root 36 Dec 19 15:04 /var/lib/jenkins/workspace/testproject-test
[root@jenkins01 ~]# ll /var/lib/jenkins/workspace/testproject-test
total 4
-rw-r--r-- 1 root root 72 Dec 19 15:04 index.html
[root@jenkins01 ~]# cat /var/lib/jenkins/workspace/testproject-test/index.html
<h1> index html v1</h1>
<h1> index html v2</h1>
<h1> index html v3</h1>
# 将代码部署至后端 Web 服务器
# 先和后端 Web服务器实现基于key验证
[root@jenkins01 ~]# ssh-copy-id 192.168.1.51
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.1.51's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.1.51'"
and check to make sure that only the key(s) you wanted were added.
[root@jenkins01 ~]# ssh 192.168.1.51
Last login: Mon Dec 19 15:07:17 2022 from 192.168.1.1
[root@web1 ~]# exit
logout
Connection to 192.168.1.51 closed.
# 并在后端服务器安装nginx服务
[root@web1 ~]# yum install -y nginx
![图片[9]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-203.png)
修改之前的任务,添加构建的shell如下内容
cd /var/lib/jenkins/workspace/$JOB_NAME
ssh 192.168.1.51 systemctl stop nginx
scp -r * 192.168.1.51:/usr/share/nginx/html/
ssh 192.168.1.51 systemctl start nginx
![图片[10]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-206.png)
构建,查看输出
![图片[11]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-207.png)
![图片[12]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-208.png)
# 针对 tomcat 后端服务器,构建执行shell脚本内容
ssh-copy-id tomcat服务器IP #实现到生产服务器的免密钥认证
cd /var/lib/jenkins/workspace/job1/
tar czvf code.tar.gz index.html
scp code.tar.gz www@192.168.1.51:/data/tomcat/tomcat_appdir/
scp code.tar.gz www@192.168.1.52:/data/tomcat/tomcat_appdir/
ssh www@192.168.1.51 "/etc/init.d/tomcat stop && rm -rf
/data/tomcat/tomcat_webdir/myapp/* && cd /data/tomcat/tomcat_appdir && tar xvf
code.tar.gz -C /data/tomcat/tomcat_webdir/myapp/"
ssh www@192.168.1.52 "/etc/init.d/tomcat stop && rm -rf
/data/tomcat/tomcat_webdir/myapp/* && cd /data/tomcat/tomcat_appdir && tar xvf code.tar.gz -C /data/tomcat/tomcat_webdir/myapp/"
ssh www@192.168.1.51 "/etc/init.d/tomcat start"
ssh www@192.168.1.52 "/etc/init.d/tomcat start"
5、更新gitlab源代码在jenkins再次构建
# 在之前曾经执行git clone的主机上执行下面操作,更新代码
[root@client data]# git clone git@192.168.1.71:testgroup/testproject.git
Cloning into 'testproject'...
remote: Enumerating objects: 10, done.
remote: Total 10 (delta 0), reused 0 (delta 0), pack-reused 10
Receiving objects: 100% (10/10), done.
[root@client data]# ll
total 0
drwxr-xr-x 3 root root 36 Dec 19 15:28 testproject
[root@client data]# cd testproject/
[root@client testproject]# ll
total 4
-rw-r--r-- 1 root root 72 Dec 19 15:28 index.html
[root@client testproject]# vim index.html
[root@client testproject]# cat index.html
<h1> index html v111111</h1>
<h1> index html v222222</h1>
<h1> index html v333333</h1>
<h1> index html v444444</h1>
[root@client testproject]# git branch
* main
[root@client testproject]# git add index.html
[root@client testproject]# git commit -m v4
[main b072659] v4
1 file changed, 4 insertions(+), 3 deletions(-)
[root@client testproject]# git push
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.1.71:testgroup/testproject.git
b0cef9d..b072659 main -> main
![图片[13]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-209.png)
![图片[14]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-210.png)
![图片[15]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-211.png)
6、版本回滚
新建任务如下,实现回滚功能
![图片[16]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-212.png)
cd /data/ && rm -rf *
git clone git@192.168.1.71:testgroup/testproject.git
cd testproject && git reset --hard HEAD^
scp -r * 192.168.1.51:/usr/share/nginx/html/
![图片[17]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-213.png)
![图片[18]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-214.png)
![图片[19]-配置 jenkins 结合 gitlab 实现自动化部署和回滚-李佳程的个人主页](http://www.lijiach.com/wp-content/uploads/2022/12/image-215.png)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END