利用jenkins自动部署完整项目

1.环境规划

主机 ip
Jenkins 192.168.81.220
gitlab 192.168.81.210
lb 192.168.81.230
web01 192.168.81.240
web02 192.168.81.250

2.gitlab新建monitor项目

2.1.配置Jenkins服务器公钥

[root@jenkins ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQrLFOIJju4L/8LdQByTxevt+vDx1ffmL4hG255B8emlNQuLBki1r3HARLE4aGk0njC1lt1o35PaE0cJ7LHlOUxBvBo7cstMqxtdkPMBHh1kxyzr1hjAUTen/jha/vfo5IANZt1S3qQnGCz/g/XsMW6MvwVlvgKjAtxfY9zsskyaVFo7iJ/hoEr4VvzBBP+sa16s9SbJNIKEgPl6SIi/6FxcEsVXPXwvvP9kCGXXnJjIhnOj48vwJYlwGEmEpXlH6wTCqV31F8Rw0WlF8dn+hXo7NUE8HbtYx/TObHzLAMPwN6m77SQ2QoohvCz/l6tFxBOmR2lBqixRDCJog/miFj root@gitlab

将公钥文件添加到Gitlab中

2.2.创建monitor项目

2.3.将monitor的代码提交至远程项目仓库

[root@gitlab ~]# unzip monitor-master.zip
[root@gitlab monitor]# git init
初始化空的 Git 版本库于 /root/monitor/.git/
[root@gitlab monitor]# git remote -v
[root@gitlab monitor]# git remote add origin git@gitlab.jiangxl.com:root/monitor.git
[root@gitlab monitor]# git add .
[root@gitlab monitor]# git commit -m "首次提交"
[root@gitlab monitor]# git push -u origin master

代码已上传

3.jenkins拉取gitlab上的代码到本地

3.1.新建项目

源码管理关联私钥

3.2.立即构建查看项目代码

立即构建

查看构建日志

代码已获取

4.配置nginx web集群

4.1.负载均衡配置

[root@nginxlb ~]# yum -y install nginx
[root@nginxlb ~]# vim /etc/nginx/conf.d/proxy_monitor.conf
upstream monitor {server 192.168.81.240;server 192.168.81.250;
}server {listen 80;server_name monitor;location / {proxy_pass http://monitor;proxy_set_header HOST $http_host;}
}[root@nginxlb ~]# nginx -t
s、nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginxlb ~]# systemctl restart nginx

4.2.web节点配置

web01配置
[root@web01 ~]# yum -y install nginx
[root@web01 ~]# vim /etc/nginx/conf.d/monitor.conf
server {listen 80;server_name monitor;location / {root /web/monitor;index index.html;}
}[root@web01 ~]# mkdir -p /web/monitor/[root@web01 ~]# echo "web01..." > /web/monitor/index.html
[root@web01 ~]# nginx -t
[root@web01 ~]# systemctl restart nginxweb02配置
[root@web02 ~]# yum -y install nginx
[root@web02 ~]# vim /etc/nginx/conf.d/monitor.conf
server {listen 80;server_name monitor;location / {root /web/monitor;index index.html;}
}[root@web02 ~]# mkdir -p /web/monitor/
[root@web02 ~]# echo "web02..." > /web/monitor/index.html
[root@web02 ~]# nginx -t
[root@web02 ~]# systemctl restart nginx

5.手动实现项目部署

5.1.拉取代码

点击立即构建拉取代码

5.2.将代码上传至各个节点服务器

[root@jenkins ~]# cd /var/lib/jenkins/workspace/freestyle-monitor[root@jenkins freestyle-monitor]# for i in 240 250;
> do
> scp -r . root@192.168.81.$i:/web/monitor
> done由于是静态页面直接将代码放到web目录即可

6.利用Jenkins实现自动部署

6.1.实现思路

首先编写一个脚本
1)打包将Jenkins拉取的代码进行打包
2)scp将打包好的代码scp到远程web节点
3)部署将代码进行解压,并创建软连接

6.2.脚本内容

[root@jenkins ~]# vim /script/monitor_deploy.sh #!/bin/bash
Date=`date +%F-%H-%M`
monitor_dir=/var/lib/jenkins/workspace/freestyle-monitor
web_server="192.168.81.240 192.168.81.250"web_tar() {cd $monitor_dirtar_name=/opt/monitor-${Date}.tar.gztar cfzP $tar_name ./*
}scp_web(){for host in $web_serverdoweb_dir=/web/monitor-${Date}web_root=/web/monitortar_dir=monitor-${Date}.tar.gzscp -r ${tar_name} root@$host:/webssh root@$host "mkdir -p $web_dir && \tar xf /web/${tar_dir} -C $web_dir && \rm -rf $web_root ${web_dir}.tar.gz && \ln -s $web_dir $web_root"done
}deploy(){web_tarscp_web
}deploy

6.3.修改Jenkins用户为root

[root@jenkins ~]# vim /etc/sysconfig/jenkins
JENKINS_USER="root"[root@jenkins ~]# systemctl restart jenkins

6.4.改造3中创建项目

增加执行shell命令,指向脚本名即可

6.5.立即构建项目

6.6.查看控制台输出[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

6.7.查看页面是否能访问以及节点软链接

链接的都是最新的表示成功

7.Jenkins实现tag标签自动构建

7.1.改造项目支持tag标签

配置项目----参数化构建—填写信息

点击保存后就能看到在构建时可以选择版本

将git分支也改成变量

7.2.改造脚本

#!/bin/bash
Date=`date +%F-%H-%M`
monitor_dir=/var/lib/jenkins/workspace/freestyle-monitor
web_server="192.168.81.240 192.168.81.250"
NAME=monitor-${Date}-${git_version}.tar.gz
NAME2=monitor-${Date}-${git_version}
web_tar() {cd $monitor_dirtar_name=/opt/${NAME}tar cfzP $tar_name ./*
}scp_web(){for host in $web_serverdoweb_dir=/web/monitor-${Date}-${git_version}web_root=/web/monitortar_dir=${NAME}scp -r ${tar_name} root@$host:/webssh root@$host "mkdir -p $web_dir && \tar xf /web/${tar_dir} -C $web_dir && \rm -rf $web_root ${web_dir}.tar.gz && \ln -s $web_dir $web_root"done
}deploy(){web_tarscp_web
}deploy

项目中指定新脚本

7.3.制造标签

在gitlab服务器上操作
1)修改代码
[root@gitlab monitor]# vim index.html
<a class="logo pull-left" href="index.html" style="width: 233px">jiangxl管理平台-v1.1</a>2)提交代码到仓库
[root@gitlab monitor]# git add .
[root@gitlab monitor]# git commit -m "v1.1"
[master 3d9d7c2] v1.11 file changed, 1 insertion(+), 1 deletion(-)
[root@gitlab monitor]# git push origin master
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 320 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@gitlab.jiangxl.com:root/monitor.gitf812dca..3d9d7c2  master -> master3)为当前代码打标签并将标签推送到远程仓库
[root@gitlab monitor]# git tag -a "v1.1" -m "v1.1"
[root@gitlab monitor]# git push origin v1.1
Counting objects: 1, done.
Writing objects: 100% (1/1), 155 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To git@gitlab.jiangxl.com:root/monitor.git* [new tag]         v1.1 -> v1.1多打几份标签
制造1.2标签
[root@gitlab monitor]# vim index.html
<a class="logo pull-left" href="index.html" style="width: 233px">jiangxl管理平台-v1.2</a>[root@gitlab monitor]# git add .
[root@gitlab monitor]# git commit -m "v1.2"
[root@gitlab monitor]# git push origin master[root@gitlab monitor]# git tag -a "v1.2" -m "1.2"
[root@gitlab monitor]# git push origin v1.2 制造1.3标签
[root@gitlab monitor]# vim index.html
<a class="logo pull-left" href="index.html" style="width: 233px">jiangxl管理平台-v1.3</a>[root@gitlab monitor]# git add .
[root@gitlab monitor]# git commit -m "1.3"
[root@gitlab monitor]# git push origin master[root@gitlab monitor]# git tag -a "v1.3" -m "1.3"
[root@gitlab monitor]# git push origin v1.3

7.4.根据标签去自动构建

7.4.1.构建v.1.1

点击自动构建—填写上标签—开始构建

控制台输出

构建成功

版本为1.1,服务器中的目录链接到1.1

7.4.2.构建v1.2

点击自动构建—填写上标签—开始构建

控制台输出

构建成功

版本为1.2,服务器中的目录链接到1.2

7.4.3.构建v1.3

点击自动构建—填写上标签—开始构建

控制台输出

构建成功

版本为1.3,服务器中的目录链接到1.3

7.5.列表选择标签构建

7.4.1.安装git parameter插件

7.4.2.关闭之前的文本使用安装好的git parameter

general—添加参数—git parameter—填写信息

7.4.3.点击立即构建会看到有标签列表

7.4.4.构建1.1版本

查看已经构建成功

8.jenkins实现tag标签自动回退

8.1.新增一个choise parameter选项

8.2.改造脚本增加rollback函数

思路:我们希望每次回退是直接修改一下链接文件而不是在点一次构建,因此我们在写脚本的时候可以先用find找到对应的链接文件,然后再把之前的链接删除,重新挂一个软连接

8.3.项目中指定新脚本

8.4.构建一个1.3版本并回退到1.2版本

8.4.1构建1.3版本

构建成功

8.4.2.回退到1.2版本

成功的挂到的1.2版本回退成功

9.Jenkins解决tag重复构建

可以通过Jenkins的变量来解决重复构建问题

用到的Jenkins变量:

  • GIT_COMMIT

    • 当前提交的git commit id号
  • GIT_PREVIOUS_SUCCESSFUL_COMMIT
    • 已成功构建过的git commit id号

每次成功构建后都会将git commit id号写到GIT_PREVIOUS_SUCCESSFUL_COMMIT这个变量中,我们可以利用GIT_COMMIT变量和GIT_PREVIOUS_SUCCESSFUL_COMMIT这个变量进行比对,如果值相等表示已经部署过了,无需再进行部署,如果值不同则说明是第一次构建,则立即进行构建。

9.1.改造脚本

重点修改了最后的if判断,增加了commit变量进行比对再进行构建

[root@jenkins script]# vim monitor_deploy_rollback_tag.sh
#!/bin/bash
Date=`date +%F-%H-%M`
monitor_dir=/var/lib/jenkins/workspace/freestyle-monitor
web_server="192.168.81.240 192.168.81.250"
NAME=monitor-${Date}-${git_version}.tar.gz
NAME2=monitor-${Date}-${git_version}
web_dir=/web/monitor-${Date}-${git_version}
web_root=/web/monitor
tar_dir=${NAME}
tar_name=/opt/${NAME}web_tar() {cd $monitor_dirtar cfzP $tar_name ./*
}scp_web(){for host in $web_serverdoscp -r ${tar_name} root@$host:/webssh root@$host "mkdir -p $web_dir && \tar xf /web/${tar_dir} -C $web_dir && \rm -rf $web_root ${web_dir}.tar.gz && \ln -s $web_dir $web_root"done
}rollback() {for host in $web_serverdoback_file=$(ssh root@$host "find /web/ -maxdepth 1 -type d -name "*-${git_version}*"")ssh root@$host "rm -rf $web_root && \ ln -s $back_file $web_root"done
}deploy(){web_tarscp_web
}if [ $deploy_env == "deploy" ];thenif [ ${GIT_COMMIT} == ${GIT_PREVIOUS_SUCCESSFUL_COMMIT} ];thenecho "该 ${git_version} 已经构建过了,无需再进行构建!!!"elsedeployfi
elif [ $deploy_env == "rollback" ];thenrollback
fi

修改脚本

9.2.构建一个已经构建成功的tag并查看效果

1.3版本我们已经部署过了,我们在查看查看效果,是否无需再构建

commit id号进行比对,已经存在了构建成功的commit id号,则已经成功防止了重复构建,应用也不会部署指定的版本,还会保持当前的版本

9.3.新增一个1.4tag版本

gitlab服务器操作
[root@gitlab monitor]# vim index.html
<a class="logo pull-left" href="index.html" style="width: 233px">jiangxl管理平台-v1.4</a>[root@gitlab monitor]# git add .
[root@gitlab monitor]# git commit -m "1.4"[root@gitlab monitor]# git push origin master[root@gitlab monitor]# git tag -a "v1.4" -m "1.4"
[root@gitlab monitor]# git push origin v1.4 

9.4.构建1.4版本

升级到1.4版本查看是否能成功

立即构建

构建成功

9.5.回退到1.1版本

回退到1.1版本查看是否能成功

立即构建

构建成功

10.美化git parameter标签列表

1.点击高级

2.选择排序方式

none:不排序

ASC:升序

DES:降序

一般都选择降序,把最新的版本排在上面

jenkins调用shell脚本实现自动上线完整项目---此项目中用到了git parameter、choise parameter参数化构建(五)相关推荐

  1. java无阻塞执行脚本,JAVA调用Shell脚本-及阻塞的解决方法

    JAVA调用Shell脚本--及阻塞的解决办法 用java调用shell,使用 Process p=Runtime.getRuntime().exec(String[] cmd); Runtime.e ...

  2. Jenkins执行shell脚本启动tomcat失败解决方法

    Jenkins执行shell脚本启动tomcat失败解决方法 参考文章: (1)Jenkins执行shell脚本启动tomcat失败解决方法 (2)https://www.cnblogs.com/wa ...

  3. 基于sparksql调用shell脚本运行SQL

    [Author]: kwu 基于sparksql调用shell脚本运行SQL,sparksql提供了类似hive中的 -e  , -f ,-i的选项 1.定时调用脚本 #!/bin/sh # uplo ...

  4. Python 调用shell脚本

    python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执行过程中的输出内容. 实际使用时视需求情况而选择 ...

  5. python调用Shell脚本:os.system(cmd)或os.popen(cmd),

    python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执行过程中的输出内容.实际使用时视需求情况而选择. ...

  6. Python语言学习:利用python语言实现调用内部命令(python调用Shell脚本)—命令提示符cmd的几种方法

    Python语言学习:利用python语言实现调用内部命令(python调用Shell脚本)-命令提示符cmd的几种方法 目录 利用python语言实现调用内部命令-命令提示符cmd的几种方法 T1. ...

  7. hive运行mysql脚本_用java代码调用shell脚本执行sqoop将hive表中数据导出到mysql

    1:创建shell脚本 1 touch sqoop_options.sh2 chmod 777 sqoop_options.sh 编辑文件  特地将执行map的个数设置为变量  测试 可以java代码 ...

  8. java调用shell脚本并传递参数

    最近业务上需要java调用执行shell脚本进行一些业务处理,写了个demo,记录下. 主要代码 @RequestMapping("/copy/database")@Respons ...

  9. java调用shell脚本及注意事项

    需求: get方法下载远程zip包,然后zip包解压,取出第一级目录再次进行压缩获取新的压缩zip包. 问题: 如果选择使用java代码的IO流操作,在不确定zip包大小的情况下可能会占用很大的内存, ...

  10. springboot 远程调用shell脚本,环境为windows

    springboot 远程调用shell脚本,环境为windows pom.xml配置 yml配置文件 实体类 配置服务器SSH 具体业务 测试 由于需要调用shell脚本实现一些自动化控制操作,特记 ...

最新文章

  1. Python,OpenCV中的K均值聚类——K-Means Cluster
  2. ubuntu15.10安装wireshark
  3. PHP数组的排序函数
  4. 自己动手开发调试器 01
  5. python唯一映射类型_Python基础:04映射类型
  6. node.js文件操作
  7. 认识oracle监听器配置文件
  8. 掌握了开源框架还不够,你更需要掌握源代码
  9. CSS垂直居中的七个方法
  10. 求知成瘾,却无作品 的思考 - stoneniqiu - 博客园
  11. 并发编程常见面试题总结四
  12. Win10连接上了wifi但是打开浏览器显示网络异常,诊断网络发现错误“远程计算机或者设备将不接受连接
  13. 数值分析龙贝格matlab,MATLAB数值分析实验二(复合梯形、辛普森和龙贝格求积,以及二重积分计算等)...
  14. Java网络编程并实现一对一聊天室功能
  15. 主机Ping不通虚拟机
  16. python2代码转换python3(2018新)
  17. Desolate Era Book 1, Chapter 1
  18. 英国小黄车玩法,国际版抖音tiktok小店
  19. Notepad++删除各类注释
  20. 基于BS架构考试系统的设计与分析

热门文章

  1. dubbo 的SPI机制Adaptive适配
  2. 深入浅出了解AUTOSAR Adaptive平台
  3. CF 贪心+dp(动态规划) 01背包(做与不做)
  4. tfs php,TFS的原理及应用
  5. 宇宙那么大,跟着链游“上天”看看?
  6. c语言数组文曲星猜数游戏编程,文曲星“猜数字”游戏的计算机模拟 —— 算法分析与实现...
  7. html css 浏览器 响应式 面试题持续更新!
  8. C++随机设置壁纸小软件
  9. Chelly个人训练
  10. 洛谷【P1195】口袋的天空