所需环境:
gitlab | 192.168.1.128 |
---|---|
jenkins | 192.168.1.129 |
nginx | 192.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