Linux-gitlab+jenkins+nginx cluster

Case: Realize script upload code automatically

surroundings

CPU name Host IP
gitlab 192.168.1.20
jenkins 192.168.1.19
nginx 192.168.

The deployment of gitlab and jenkins environment is omitted here!

gitlab server

Create a new project (web)

Insert picture description here
Insert picture description here

[root@gitlab ~]# git clone https://gitee.com/kangjie1209/monitor.git  //在gitee码云上下载项目代码
正克隆到 'monitor'...
remote: Enumerating objects: 435, done.
remote: Counting objects: 100% (435/435), done.
remote: Compressing objects: 100% (381/381), done.
remote: Total 435 (delta 53), reused 390 (delta 44), pack-reused 0
接收对象中: 100% (435/435), 8.78 MiB | 6.62 MiB/s, done.
处理 delta 中: 100% (53/53), done.

创建本地代码库
[root@gitlab ~]# mkdir /web
[root@gitlab ~]# cd /web
[root@gitlab web]# git init 
初始化空的 Git 版本库于 /web/.git/
[root@gitlab web]# cp -rp /root/monitor/* /web/

上传代码到gitlab服务器
[root@gitlab web]# git remote  add origin [email protected]:dev/web.git
[root@gitlab web]# git add .
[root@gitlab web]# git commit -m "commit"
[master(根提交) eebc34b] commit
 376 files changed, 65793 insertions(+)
......
[root@gitlab web]# git push -u origin master 
Counting objects: 414, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (407/407), done.
Writing objects: 100% (414/414), 4.31 MiB | 0 bytes/s, done.
Total 414 (delta 48), reused 0 (delta 0)
remote: Resolving deltas: 100% (48/48), done.
To [email protected]:dev/web.git
 * [new branch]      master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

Insert picture description here

jenkins server

new item >> enter the project name (web) >> Freestyle project >> ok >> configure ->> source code library >> git >> gitlab path and private key authentication >> save

Insert picture description here
Insert picture description here
Insert picture description here

Implement script upload website code (nginx)

(1) Install nginx

[root@nginx ~]# yum -y install epel-release.noarch 
[root@nginx ~]# yum -y install nginx
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable  nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

(2) Write a script to upload nginx on the jenkins server

[root@jenkins ~]# mkdir /scripts
[root@jenkins ~]# vim /scripts/web.sh
#!/bin/sh
CODE_DIR=/var/lib/jenkins/workspace/web/
WEB_DIR=/usr/share/nginx
IP=192.168.1.11  
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 transmits the public key to nginx, executes the script, and tests the nginx website

[root@jenkins ~]# ssh-copy-id [email protected]
[root@jenkins ~]# sh /scripts/web.sh 
web-2020-07-23-14-43-54.tar.gz       100% 4545KB  54.5MB/s   00:00  

Insert picture description here

(4) Manual construction, associated script, automatic upload

项目web >> configure >> build >> Execute shell >> sh /scripts/web.sh

Insert picture description here
Insert picture description here

[

Jenkins associates gitlab to realize automatic code upload

(1) Jenkins server

Insert picture description here

URL: http://192.168.1.19:8080/project/web

Token: 5492f28075b4a49b60d7c5109db4c7dd

(2) gitlab server

Insert picture description here
Insert picture description here

(3) Update the push code, test automatic construction and upload.

gitlab: modify the code or add new files

[root@gitlab web]# vim index.html 
<title>移动能效管理平台</title>
改成:
<title>欢迎使用本网站</title>
[root@gitlab web]# git add .
[root@gitlab web]# git commit  -m "modify index.html"
[master cfd388a] modify index.html
 1 file changed, 2 insertions(+), 2 deletions(-)
[root@gitlab web]# git push -u origin master 
Counting objects: 5, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 324 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To [email protected]:dev/web.git
   eebc34b..cfd388a  master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

(4) Log in to the nginx website to check the updated content.

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45191791/article/details/107539741