Jenkins+Gitlab+Shell脚本 实现手动代码上线

root
233
文章
0
评论
2020年2月15日23:47:51 评论 5300字阅读17分40秒

Jenkins+Gitlab+Shell脚本 实现手动代码上线

项目:码云上一个HTML5的大转盘小游戏https://gitee.com/xianzi/html5.git

实验准备:

web服务器IP:192.168.5.8

git服务器IP:192.168.5.7

jen服务器IP:192.168.5.31

这个方法有两种:

  • 一种是直接去gitlab上创建项目,git clone下载本地仓库,直接传,
  • 另一种先创建好本地仓库再去gitlab上创建项目,上传文件

我怎么算都是第一个更加聪明,这里我用的第二种

gitlab上的操作:

第一步:在git服务器上创建一个dzp的本地仓库,把代码从码云上下载下来

root@web01 dzp]# git clone https://gitee.com/xianzi/html5.git
Cloning into 'html5'...
remote: Enumerating objects: 40, done.
remote: Counting objects: 100% (40/40), done.
remote: Compressing objects: 100% (35/35), done.
remote: Total 40 (delta 7), reused 0 (delta 0)
Unpacking objects: 100% (40/40), done.
[root@web01 dzp]# ll
total 0
drwxr-xr-x 5 root root 101 Feb 15 17:39 html5

第二步:把代码推送到远程仓库上

先去gitlab上创建一个大转盘项目的仓库,请参考文章Gitlab

[root@web01 dzp]# git remote add origin git@192.168.5.7:dzp/dzp_data.git
[root@web01 dzp]# git push -u origin master
Counting objects: 2, done.
Writing objects: 100% (2/2), 182 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To git@192.168.5.7:dzp/dzp_data.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

jenkins上的操作:

第一步:在gitlab把下载地址拿上

第二步:jenkins上进行源码管理设置

源码管理设置中出现的问题

解决方法(首先把jenkins服务器的公钥放到gitlabweb页面上)

第三步:构建项目

执行路径:/var/lib/jenkins/workspace/(你起的项目名称)

Shell脚本

首先就是要用jenkins服务器给web服务器分发公钥,可以参考文章expect脚本,分发好公钥,编写shell脚本(这个脚本也是坑的很,坑了我好几个小时,呜呜呜)

#!/bin/bash
CODE_DIR=/var/lib/jenkins/workspace/dap
WEB_DIR=/code
WEB_HTML=html
TIME=`date +%F-%H-%M-%S`
IP=192.168.5.8
cd $CODE_DIR && tar zcf /tmp/web-${TIME}.tar.gz ./*
scp /tmp/web-${TIME}.tar.gz $IP:$WEB_DIR
ssh root@$IP "cd $WEB_DIR && mkdir web-$TIME"

ssh root@$IP "cd $WEB_DIR && tar xf web-${TIME}.tar.gz -C web-$TIME"
ssh root@$IP "cd $WEB_DIR && rm -rf $WEB_HTML web-${TIME}.tar.gz && ln -s web-$TIME $WEB_HTML"
rm -rf /tmp/web-*

 

这里我专门要写一下软连接链接目录的注意事项:

ln -s 目标目录  连接目录

ln -sf  目标目录  连接目录  ------强制连接,连接错了可以使用这个

  • 目录创建软链接时,目录不能事先存在
  • 软连接的目标目录和连接目录必须写绝对路径,除非你是对当前目录内的文件进行操作
  • 取消软连接直接删就行了
  • 脚本里给文件加时间戳精确的秒,必须使用这种格式date +%F-%H-%M-%S

执行一下shell脚本

[root@nfs01 scripts]# sh jenkins.sh 
web-2020-02-16-21-25-07.tar.gz                                          100%   48KB  17.8MB/s   00:00    

去web服务器上查看一下

[root@web02 code]# ll
total 0
lrwxrwxrwx 1 root root 23 Feb 16 21:25 html -> web-2020-02-16-21-25-07
drwxr-xr-x 5 root root 59 Feb 16 21:25 web-2020-02-16-21-25-07

上游览器看看,图太大,我就截取抽奖的

现在把shell脚本放到jenkins上

在jenkins-->进入自己建的项目-->配置-->构建(把shell脚本启动命令放进去)

现在开始模拟测试

我将在本地仓库上修改大转盘的数据,从本地仓库把代码上传到远程仓库gitlab,登陆jenkins选择大转盘项目,进行立即构建,上游览器登陆网站查看代码是否发生变化,我想把大转盘的图截取下来,可以太大了。

这是我修改后,即将展现的

从本地仓库进代码进行上传至gitlab(我自己瞎玩,没有做master保护,工作中肯定是推分支,主管进行合并)

[root@web01 dzp]# git commit -am "modifile index.html"
[master 5d79e44] modifile index.html
 1 file changed, 2 insertions(+), 2 deletions(-)
[root@web01 dzp]# git push -u origin master 
Counting objects: 5, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 394 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@192.168.5.7:dzp/dzp_data.git
   6965f52..5d79e44  master -> master
Branch master set up to track remote branch master from origin.

在jenkinds上对项目进行立即构建

没成功。。。。额,卡住了

查看项目执行记录,发现报了一个Host key verification failed.的错误,我确定密钥是分发的

tarted by user admin
Running as SYSTEM
[EnvInject] - Loading node environment variables.
Building in workspace /var/lib/jenkins/workspace/dap
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@192.168.5.7:dzp/dzp_data.git # timeout=10
Fetching upstream changes from git@192.168.5.7:dzp/dzp_data.git
 > git --version # timeout=10
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@192.168.5.7:dzp/dzp_data.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 5d79e44c7e2031e1e9c6194b1650f247ca67c6b1 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 5d79e44c7e2031e1e9c6194b1650f247ca67c6b1
Commit message: "modifile index.html"
 > git rev-list 5d79e44c7e2031e1e9c6194b1650f247ca67c6b1 # timeout=10
[dap] $ /bin/sh -xe /tmp/jenkins2751656148645224365.sh
+ sh /server/scripts/jenkins.sh
Host key verification failed.
lost connection
Host key verification failed.
Host key verification failed.
Host key verification failed.
Finished: SUCCESS

Host key verification failed.的错误,是由于没有使用jenkins用户,给jenkins开通root权限,使用ssh命令,分发钥匙给web服务器的root用户,才能使用脚本

这里有一个坑,yum安装的时候会把jenkins用户给改变了,我们给他变回来

这里给他改成bash,该分发公钥发一下,改放权放权,这里略,请参考su命令与sudo

在构建了shell脚本要用sudo提权的方式

再次立即构建,执行成功

Started by user admin
Running as SYSTEM
[EnvInject] - Loading node environment variables.
Building in workspace /var/lib/jenkins/workspace/dap
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@192.168.5.7:dzp/dzp_data.git # timeout=10
Fetching upstream changes from git@192.168.5.7:dzp/dzp_data.git
 > git --version # timeout=10
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@192.168.5.7:dzp/dzp_data.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 5d79e44c7e2031e1e9c6194b1650f247ca67c6b1 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 5d79e44c7e2031e1e9c6194b1650f247ca67c6b1
Commit message: "modifile index.html"
 > git rev-list 5d79e44c7e2031e1e9c6194b1650f247ca67c6b1 # timeout=10
[dap] $ /bin/sh -xe /tmp/jenkins255201594043948308.sh
+ sudo sh /server/scripts/jenkins.sh
Finished: SUCCESS

去web服务器看一下,已经生效

[root@web02 code]# ll
total 0
lrwxrwxrwx 1 root root 23 Feb 18 18:27 html -> web-2020-02-18-18-27-18
drwxr-xr-x 5 root root 59 Feb 18 18:27 web-2020-02-18-18-27-18

再去检查一下网站,数据已经发生变化,终于成功啦!

 

继续阅读
weinxin
我的微信
这是我的微信扫一扫
  • 文本由 发表于 2020年2月15日23:47:51
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
Jenkins的Pipeline项目 Devops

Jenkins的Pipeline项目

Pipeline项目 CI/CD持续集成/持续部署 持续集成是一种软件开发实践,团队开发成员经常集成它们的工作,通过每个成员每天至少集成一次,也就意味着每天可能发生多次集成。每次集成都通过自动化的构建...
jeesns项目代码部署and自动上线 Devops

jeesns项目代码部署and自动上线

jeesns项目代码自动上线 经过上一次失败的教训,这一次通过学习了maven项目管理工具,肯定能部署成功的。失败案例jeesns项目上线(失败案例),这对java项目的部署是一次很好的学习 Jenk...
maven工具,搭建私服nexus Devops

maven工具,搭建私服nexus

maven项目管理工具 Maven是一个项目管理的综合工具。Maven提供给开发人员构建一个完整的生命周期框架。 开发团队可以自动完成该项目的基础设施建设,Maven使用标准的目录结构和默认构建生命周...
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: