架构——21——gitlab上传代码使用jenkins实现nginx网页自动同步——4

所需环境:

gitlab192.168.1.128
jenkins192.168.1.129
nginx192.168.1.134

1、在gitee码云上下载项目代码(gitlab服务器)

在这里插入图片描述
在这里插入图片描述

[root@gitlab ~]# git clone https://gitee.com/kangjie1209/monitor.git

2、gitlab服务器创建本地代码库

[root@gitlab ~]# mkdir /dengyue
[root@gitlab ~]# cd /dengyue/
[root@gitlab dengyue]# cp -rp /root/monitor/* /dengyue/
[root@gitlab dengyue]# ls
404.html              efficiencyAnalysis.html  js                       other-components.html
alerts.html           energy_consumption.html  keyInfo.html             profile-page.html
assets                file-manager.html        labels.html              QHME.iml
buttons.html          fonts                    LICENSE                  readme.md
calendar.html         form-components.html     list-view.html           real-time.html
charts.html           form-elements.html       login.html               sa.html
components.html       form-examples.html       media                    tables.html
content-widgets.html  form-validation.html     media.html               typography.html
css                   images-icons.html        messages.html            userMng.html
deviceManager.html    img                      mstp_105_SuperAdmin.iml
dianfei.html          index.html               mstp_map.html

3、登录gitlab,创建新的web项目

admin area——new project——Project path(dianfeng),Project name(dengyue)——create project
在这里插入图片描述
在这里插入图片描述

4、在gitlab本地服务器上传代码到gitlab

git config --global user.name "Administrator"
git config --global user.email "admin@example.com"
[root@gitlab dengyue]# cd /dengyue
[root@gitlab dengyue]# git init
[root@gitlab dengyue]# git remote add origin git@192.168.1.128:dianfeng/dengyue.git
[root@gitlab dengyue]# git add .
[root@gitlab dengyue]# git commit -m "Initial commit"
[root@gitlab dengyue]# git push -u origin master

5、登录jenkins,新建自由项目web

new item——Enter an item name(dianfeng-dengyue)——freestyle project——OK——configure——git——gitlab路径及私钥认证——save——build now(手动同步测试)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6、jenjins实现脚本上传网站代码

1)开启一台虚拟机,安装nginx
[root@nginx ~]# yum -y install epel-release
[root@nginx ~]# yum -y install nginx
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx
2)在jenkins上编写上传nginx的脚本
[root@jenkins ~]# mkdir /scripts
[root@jenkins ~]# vim /scripts/dengyue.sh
#!/bin/sh
CODE_DIR=/var/lib/jenkins/workspace/dianfeng-dengyue/
WEB_DIR=/usr/share/nginx/
IP=192.168.1.134
TIME=`date +%F-%H-%M-%S`
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 && rm -rf web-${TIME}.tar.gz"
ssh root@$IP "cd $WEB_DIR && rm -rf html && ln -s web-$TIME html"


[root@jenkins ~]# chmod +x /scripts/dengyue.sh
3)jenkins传输公钥到nginx,执行脚本,测试查看nginx网站
[root@jenkins ~]# ssh-copy-id root@192.168.1.134
[root@jenkins ~]# sh /scripts/dengyue.sh

#nginx服务器上查看执行结果
[root@nginx ~]# cd /usr/share/nginx/
[root@nginx nginx]# ls
html  modules  web-2020-07-24-11-22-22
[root@nginx nginx]# ls html/
404.html              efficiencyAnalysis.html  js                       other-components.html
alerts.html           energy_consumption.html  keyInfo.html             profile-page.html
assets                file-manager.html        labels.html              QHME.iml
buttons.html          fonts                    LICENSE                  readme.md
calendar.html         form-components.html     list-view.html           real-time.html
charts.html           form-elements.html       login.html               sa.html
components.html       form-examples.html       media                    tables.html
content-widgets.html  form-validation.html     media.html               typography.html
css                   images-icons.html        messages.html            userMng.html
deviceManager.html    img                      mstp_105_SuperAdmin.iml
dianfei.html          index.html               mstp_map.html

测试查看nginx网站:
在这里插入图片描述

4)jenkins网页中,手动构建,关联脚本,自动上传

项目web——configure——build——Add build step(Execute shell)——Command(sh /scripts/dengyue.sh)——Save
在这里插入图片描述
在这里插入图片描述

注意:如果安装jenkins后没有修改配置为root,脚本执行如果有权限问题,解决方法
(1)修改jenkins登录shell为修改jenkins登录shell为/bin/bash
vim /etc/passwd
修改:
jenkins:x:990:985:Jenkins Automation Server:/var/lib/jenkins:/bin/bash
保存退出
(2)修改jenkins服务器的本地profile
vim /root/.bash_profile
添加:
export PS1=’[\u@\h \W]$’
保存退出
source /root/.bash_profile
(3)赋予jenkins用户sudo权限
isudo
添加:
jenkins ALL=(ALL) NOPASSWD: ALL
保存退出

8、jenkins关联gitlab,实现自动构建上传代码

1)jenkins:

项目dianfeng-dengyue——configure——build triggers——勾选最长选项——点击高级advanced——选择Filter branches by name——include填写master——点击generate生成令牌——复制令牌和触发器顶部url路径留用——save保存
在这里插入图片描述
在这里插入图片描述
599f716a9c8fca9148221aa9e270920a
http://192.168.1.129:8081/project/dianfeng-dengyue
复制出来后,即可save保存

2)gitlab:

项目dengyue——settings——integrations——把刚刚复制的令牌和url粘贴过来——Add webhook
在这里插入图片描述

3)更新push代码,测试自动构建上传

在gitlab上,修改代码或添加新的文件,依次执行:git add;git commit;git push

[root@gitlab dengyue]# vim /dengyue/index.html
<title>移动能效管理平台!!!</title>				#定位修改
<a class="logo pull-left" href="index.html" style="width: 233px">移动能效管理平台!!!</a>				#定位修改

[root@gitlab dengyue]# git add .
[root@gitlab dengyue]# git commit -m "modify index.html"
[root@gitlab dengyue]# git push -u origin master


#如果你的手速够快,还可以看到脚本执行的变化
[root@nginx nginx]# ls
html  modules  web-2020-07-24-11-22-22  web-2020-07-24-11-51-02.tar.gz
[root@nginx nginx]# ls
html     web-2020-07-24-11-22-22  web-2020-07-24-11-51-02
modules  web-2020-07-24-11-51-02.tar.gz
[root@nginx nginx]# ls
html  modules  web-2020-07-24-11-22-22  web-2020-07-24-11-51-02
4)登录nginx网站,查看更新内容

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值