Jenkins与Gitlab与网站服务器互连实现自动上传代码

一、Gitlab上获取演示代码

环境准备
三台虚拟机Jenkins、gitlab、nginx
1.在gitee码云上下载项目代码(gitlab服务器)

[root@gitlab ~]# git clone https://gitee.com/kangjie1209/monitor.git
[root@gitlab ~]# ls
anaconda-ks.cfg  monitor

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

[root@gitlab ~]# mkdir /web
[root@gitlab ~]# cd /web
[root@gitlab web]# cp -rp /root/monitor/* .

3.登录gitlab,创建新的web项目
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
4.在gitlab本地服务器上传代码到gitlab

[root@gitlab ~]# cd /web
[root@gitlab web]# git init
[root@gitlab web]# git config --global user.name "root"
[root@gitlab web]# git config --global user.email "root@163.com"
[root@gitlab web]# git remote add origin git@192.168.229.187:test/web.git
[root@gitlab web]# git add .
[root@gitlab web]# git commit -m "Initial commit"
[root@gitlab web]# ssh-keygen -t rsa
[root@gitlab web]# cat /root/.ssh/id_rsa.pub
[root@gitlab web]# git pull git@192.168.229.187:test/web.git
[root@gitlab web]# git push -u origin master

在这里插入图片描述

二、Jenkins创建任务

1.登录jenkins,新建自由项目web
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.Jenkins服务器上验证

[root@jenkins ~]# cd /var/lib/jenkins/workspace/
[root@jenkins workspace]# ls
web  web@tmp
[root@jenkins workspace]# cd web
[root@jenkins web1]# ls

三、jenkins实现脚本上传网站代码

1.新开启一台虚拟机安装nginx

[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# ss -lnt | grep 80
LISTEN     0      128          *:80                       *:*                  
LISTEN     0      128       [::]:80                    [::]:* 

2.在jenkins上编写上传nginx的脚本

[root@jenkins web]# mkdir /scripts
[root@jenkins web]# cd /scripts/
[root@jenkins scripts]# vim web.sh
#!/bin/sh
CODE_DIR=/var/lib/jenkins/workspace/web/
WEB_DIR=/usr/share/nginx
IP=192.168.229.221
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"

3.jenkins传输公钥到nginx

[root@jenkins scripts]# ssh-copy-id root@192.168.229.221

4.执行脚本测试

[root@jenkins scripts]# chmod +x web.sh
[root@jenkins scripts]# sh web.sh

5.查看nginx网站验证
在这里插入图片描述

四、手动构建,关联脚本,自动上传

1.Jenkins网页上设置构建脚本
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
注意:如果安装jenkins后没有修改配置中的用户为root,脚本执行会有权限问题。
解决方案如下

(1)修改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
保存退出

建议在安装时,把用户修改为root可以避免不必要的麻烦。
2.jenkins关联gitlab,实现自动构建上传代码
(1)jenkins服务器:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
记住口令和URL地址

eb1c0402d28530f7a51f7c483c7c4422
http://192.168.229.209:8081/project/web

(2)gitlab服务器
在这里插入图片描述
在这里插入图片描述
(3)更新push代码,测试自动构建上传。
在gitlab服务器上,修改代码或添加新的文件,依次执行:

[root@gitlab web]# vim index.html
...
            <a class="logo pull-left" href="index.html" style="width: 233px">移
动能效管理平台1</a>
...
[root@gitlab web]# git add .
[root@gitlab web]# git commit -m "modify index.html"
[root@gitlab web]# git push -u origin master

(4)登录nginx网站,查看更新内容。
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值