CICD(9)-Gitlab&Jenkins自动化部署项目案例

本文详细介绍了如何利用Jenkins和Gitlab进行自动化部署,包括Jenkins创建job、配置公钥、权限设置、构建触发器设定以及与HAProxy和SonarQube的集成。通过自定义shell脚本,实现了代码部署和回滚,确保了CICD流程的高效执行。
摘要由CSDN通过智能技术生成

使用 Jenkins 和 Gitlab 结合自定义 shell 脚本将代码自动部署到后端,
并实现代码回滚到上一个版本。

环境:

主机名 IP
gitlab-server 192.168.100.146
Jenkins-server 192.168.100.148
tomcat-server-node1 192.168.100.150
tomcat-server-node2 192.168.100.152
tomcat-server-node3 192.168.100.160
tomcat-server-node4 192.168.100.162
HAProxy-node1 192.168.100.154
HAProxy-node2 192.168.100.156
SonarQube-server 192.168.100.158

一. Jenkins 和 Gitlab 项目配置

1.1 Jenkins 创建 job

1.2 添加公钥

1.2.1 添加 KEY

将 Jenkins 服务器的 www 用户的公钥添加到 Gitlab

www@Jenkins-server:~$ cat .ssh/id_rsa.pub # 没有就ssh-keygen一下
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVOHV/qFhtkVVtpFxq0cOm9VobqwlVmaulAP6cxPUm3fv/HrKjnKWPll78RBpfTrSwCpTX4SarlEDH5c0Ot1nZFMV8LFuXnvb4oYacOdJovuj8ESdEvYjZpq3Gy2BUT9GlBwsKdxnA8NHd4/z5V+wtjKpIuGSdXnc0NtSr7fumAzqEEHNly0WNcqWoSN0kgEHUcNzT0UfllkBp/EbZEAPtwvZxMAhHW3sEgqV+X01Mm7K5RHw2UEM47cLVi2QrT21sGH7jAD8VmR+vUPPrUOeYg2hh0UlP8ua4ffABYpVJDnnd8CjJds4UTU8hg9CU5pT5k0lhiV4V1W9XPJQe8DnX www@Jenkins-server

1.2.2 测试 WWW 用户 clone 代码

# 必须非交互克隆代码
www@Jenkins-server:~$ git clone git@192.168.100.146:root/deploy-demo.git
Cloning into 'deploy-demo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
www@Jenkins-server:~$ ll deploy-demo/
total 16
drwxrwxr-x 3 www www 4096 Mar 14 21:15 ./
drwxr-xr-x 4 www www 4096 Mar 14 21:15 ../
drwxrwxr-x 8 www www 4096 Mar 14 21:15 .git/
-rw-rw-r-- 1 www www   26 Mar 14 21:15 README.md

1.2.3 编写测试页面并提交

www@Jenkins-server:~/deploy-demo$ pwd
/home/www/deploy-demo
www@Jenkins-server:~/deploy-demo$ vim deploy-demo.html
...
www@Jenkins-server:~/deploy-demo$ cat deploy-demo.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js_start</title>
    <h1 id="h" >Hello, welcome to V1.</h1>
</head>
<body>
<canvas id="v1" width="300" height="200" style="border: 8px solid #009"></canvas>
    <script>
        const canvas = document.getElementById("v1");
        const context = canvas.getContext("2d");
        let x = 50;
        let y = 50;
        let xChange = 0.6;
        let yChange = 0.3;
        function blob(xloc, yloc, size, color) {
   
            context.clearRect(0, 0, canvas.width, canvas.height);
            context.beginPath();
            context.arc(xloc, yloc, size, 0, 2*Math.PI);
            context.fillStyle = color;
            context.fill();
            context.stroke();
            if (x >= canvas.width - 40|| x <= 40) {
   
                xChange *= -1;
            }
            if (y >= canvas.height - 40|| y <= 40) {
   
                yChange *= -1;
            }
            x += xChange;
            y += yChange; }
            setInterval(function () {
   
               blob(x, y, 10, "green")
            }, 1);
        document.getElementById("mod").value = "click";
    </script>
</body>
</html>

www@Jenkins-server:~/deploy-demo$ git config --global user.email "1049103823@qq.com"
www@Jenkins-server:~/deploy-demo$ git config --global user.name "root"

# 提交
www@Jenkins-server:~/deploy-demo$ git commit -m "init"
[master 07c15f9] init
 1 file changed, 38 insertions(+)
 create mode 100644 deploy-demo.html

# 推送到gitlab
www@Jenkins-server:~/deploy-demo$ git push
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 776 bytes | 776.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To 192.168.100.146:root/deploy-demo.git
   1b58adb..07c15f9  master -> master

查看 gitlab 项目:

1.3 权限配置

1.3.1 脚本运行和 WEB 启动权限

一般使用非 root 用户启动 web 服务及完成代码发布,默认 Jenkins 运行使用的是
jenkins 用户,因此需要赋予 jenkins 用户一定的权限,另外发布的脚本可以在本机
也可以不在本机,如果不在 Jenkins 执行部署脚本,则需要使用 jenkins 用户 ssh
到发布服务器执行 shell 脚本。

因此,Jenkins 服务器的 jenkins 用户和 root 用户都必须和 tomcat web 服务器实现基于
KEY 的通讯。在 Jenkins 拷贝公钥到各个 tomcat 服务器:

root@Jenkins-server:~# ssh-copy-id 192.168.100.150
root@Jenkins-server:~# ssh-copy-id 192.168.100.152

root@Jenkins-server:~# su - www
www@Jenkins-server:~$ ssh-copy-id 192.168.100.150
www@Jenkins-server:~$ ssh-copy-id 192.168.100.152
www@Jenkins-server:~$ exit

root@Jenkins-server:~# su - jenkins
jenkins@Jenkins-server:~$ ssh-copy-id 192.168.100.150
jenkins@Jenkins-server:~$ ssh-copy-id 192.168.100.152

查看看各 tomcat 服务器的.ssh/authorized_keys:

# node1
root@tomcat-server-node1:~# su - www
www@tomcat-server-node1:~$ cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRiZyf8XOGdz6K91Z5tUZqZgOPclcHK3KGonxv0w8gkSQLgNims11bW5KLctyKV5PdCTaK0dFOarZl6bONyqh2tuwDw35XRz0Hbk7qxkrlRzHBlEhD8pUlI2zt+CwMDipTYO8TMdZ5zyECKQbk8BhV3blIXVUnHXpfzLvp48jed0DUsY6i0vfYxFPdyaTx0MZj68nrMHa7YkUP2xry1iWJd9xOA0i4ojYGV/sJkkR2DeoB9p+TvFoNl8fzTwoAlKTFS5RaCGAmjmbMzLnSbP8LvhloHOp3QCIwZ7Bdd2Ey8lk94cibJRG/mT1JM8FC+WJzc8Ni5k3m9HqVcFJ8rXo9 www@85ee75762188
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCbWnoOk92XRnX49/sucan1En+V8vAdI1Xqwb1lp9LECythPRDvt7THfdUbGCNLB6qX2S01bvTLq9B2Lm5ekSOyFFVr+wUGBZRYJ2VurA4V9psGg5UZpQl+heiqXZIFdQxwTNLC3LRuMqomgNlfPfc6vp+rZloYsXw9F6m/quz8TjznK7tb1VjghhTSJEX8fEicM0QcKn/g0HLEKzgWqBhOnjdNY3zRsTg/eZY8eeTn4wTMDX5ra9Ek/DV6kvMMyhiydpGgJI5P5zoaKbc8dZ3Kzbe8RZNR/9LbC04mKd6A6sogj6GulZTT61qUAXcpVClHrST51e/Dc+ie87mJMUw9 root@Jenkins-server  # root用户
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxWqyM1uK8w1AwOAyXkHnUR2vM6z9otNeTkFLc+sj6kVf4Ugh0qOcIE42+MCLOHEUJ0bU8MekN8tZ//FDCD3GtP1kDol9tpBR3e2I7B3stpqgE4sdDwDv0DRfi3kIyuMR9vLemqJ0CGchQpfA4tZPgXOvMUNhWiig+rxf0O4WQO3AgBnJTC4xQ9awOIagyg/IgHnw2UCW+CATfsCiMeljar8eIsi2CrDj/WGclMkONc0xy1Rjldj5RGTlqfwU0nmJTX1fMsxpH7PD5D2QCCnE5agkr9R4XTV3YKIrjxRxerr9rzkB38piXatqy74pg+s64MM/k/6fcjPdhwJ00mR8V jenkins@Jenkins-server # jenkins用户
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVOHV/qFhtkVVtpFxq0cOm9VobqwlVmaulAP6cxPUm3fv/HrKjnKWPll78RBpfTrSwCpTX4SarlEDH5c0Ot1nZFMV8LFuXnvb4oYacOdJovuj8ESdEvYjZpq3Gy2BUT9GlBwsKdxnA8NHd4/z5V+wtjKpIuGSdXnc0NtSr7fumAzqEEHNly0WNcqWoSN0kgEHUcNzT0UfllkBp/EbZEAPtwvZxMAhHW3sEgqV+X01Mm7K5RHw2UEM47cLVi2QrT21sGH7jAD8VmR+vUPPrUOeYg2hh0UlP8ua4ffABYpVJDnnd8CjJds4UTU8hg9CU5pT5k0lhiV4V1W9XPJQe8DnX www@Jenkins-server # www用户

# node2
www@tomcat-server-node2:~$ cat .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDRiZyf8XOGdz6K91Z5tUZqZgOPclcHK3KGonxv0w8gkSQLgNims11bW5KLctyKV5PdCTaK0dFOarZl6bONyqh2tuwDw35XRz0Hbk7qxkrlRzHBlEhD8pUlI2zt+CwMDipTYO8TMdZ5zyECKQbk8BhV3blIXVUnHXpfzLvp48jed0DUsY6i0vfYxFPdyaTx0MZj68nrMHa7YkUP2xry1iWJd9xOA0i4ojYGV/sJkkR2DeoB9p+TvFoNl8fzTwoAlKTFS5RaCGAmjmbMzLnSbP8LvhloHOp3QCIwZ7Bdd2Ey8lk94cibJRG/mT1JM8FC+WJzc8Ni5k3m9HqVcFJ8rXo9 www@85ee75762188
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCbWnoOk92XRnX49/sucan1En+V8vAdI1Xqwb1lp9LECythPRDvt7THfdUbGCNLB6qX2S01bvTLq9B2Lm5ekSOyFFVr+wUGBZRYJ2VurA4V9psGg5UZpQl+heiqXZIFdQxwTNLC3LRuMqomgNlfPfc6vp+rZloYsXw9F6m/quz8TjznK7tb1VjghhTSJEX8fEicM0QcKn/g0HLEKzgWqBhOnjdNY3zRsTg/eZY8eeTn4wTMDX5ra9Ek/DV6kvMMyhiydpGgJI5P5zoaKbc8dZ3Kzbe8RZNR/9LbC04mKd6A6sogj6GulZTT61qUAXcpVClHrST51e/Dc+ie87mJMUw9 root@Jenkins-server # root用户
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCxWqyM1uK8w1AwOAyXkHnUR2vM6z9otNeTkFLc+sj6kVf4Ugh0qOcIE42+MCLOHEUJ0bU8MekN8tZ//FDCD3GtP1kDol9tpBR3e2I7B3stpqgE4sdDwDv0DRfi3kIyuMR9vLemqJ0CGchQpfA4tZPgXOvMUNhWiig+rxf0O4WQO3AgBnJTC4xQ9awOIagyg/IgHnw2UCW+CATfsCiMeljar8eIsi2CrDj/WGclMkONc0xy1Rjldj5RGTlqfwU0nmJTX1fMsxpH7PD5D2QCCnE5agkr9R4XTV3YKIrjxRxerr9rzkB38piXatqy74pg+s64MM/k/6fcjPdhwJ00mR8V jenkins@Jenkins-server  # jenkins用户
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVOHV/qFhtkVVtpFxq0cOm9VobqwlVmaulAP6cxPUm3fv/HrKjnKWPll78RBpfTrSwCpTX4SarlEDH5c0Ot1nZFMV8LFuXnvb4oYacOdJovuj8ESdEvYjZpq3Gy2BUT9GlBwsKdxnA8NHd4/z5V+wtjKpIuGSdXnc0NtSr7fumAzqEEHNly0WNcqWoSN0kgEHUcNzT0UfllkBp/EbZEAPtwvZxMAhHW3sEgqV+X01Mm7K5RHw2UEM47cLVi2QrT21sGH7jAD8VmR+vUPPrUOeYg2hh0UlP8ua4ffABYpVJDnnd8CjJds4UTU8hg9CU5pT5k0lhiV4V1W9XPJQe8DnX www@Jenkins-server  # www用户

1.3.2 测试 SSH KEY 通讯

# Jenkins服务器的root用户连tomcat服务器的www用户
root@Jenkins-server:~# ssh www@192.168.100.150
# 顺利登陆
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-55-generic x86_64)
...
Last login: Sat Mar 14 21:33:04 2020 from 192.168.100.148
www@tomcat-server-node1:~$ exit
logout
Connection to 192.168.100.150 closed.

root@Jenkins-server:~# ssh www@192.168.100.152
# 顺利登陆
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-55-generic x86_64)
...
Last login: Sat Mar 14 21:33:09 2020 from 192.168.100.148
www@tomcat-server-node2:~$ exit
logout
Connection to 192.168.100.152 closed.
root@Jenkins-server:~#

# Jenkins服务器的www用户连tomcat服务器的www用户
www@Jenkins-server:~$ ssh www@192.168.100.150
# 顺利登陆
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-55-generic x86_64)
...
Last login: Thu Mar 12 15:32:51 2020 from 192.168.100.148
www@tomcat-server-node1:~$ exit
logout
Connection to 192.168.100.150 closed.

www@Jenkins-server:~$ ssh www@192.168.100.152
# 顺利登陆
Welcome to Ubuntu 18.04.3 LTS (GNU/Linux 4.15.0-55-generic x86_64)
...
Last login: Thu Mar 12 15:32:51 2020 from 192.168.100.148
www@tomcat-server-node2:~$ exit
logout
Connection to 192.168.100.152 closed.

1.3.3 配置 jenkins 用户的 sudo 权限

Jenkins 使用普通用户 jenkins 或其他普通用户启动,需要授予 sudo 权限,root 不
需要设置。

root@Jenkins-server:~# vim /etc/sudoers
...
# 注释下面一行不需要tty
#Defaults requiretty

# 不需要使用密码即可执行ssh
jenkins ALL=(ALL) NOPASSWD: /usr/bin/ssh

1.4 配置 Jenkins 项目

1.4.1 添加 Gitlab 仓库

1.4.2 在 Jenkins 服务器的 www 用户家目录编写测试脚本

root@Jenkins-server:~# su - www

www@Jenkins-server:~$ vim deploy-demo.sh
#!/bin/bash
# test
echo $USER
cd /home/www/deploy-demo
git pull git@192.168.100.146:root/deploy-demo.git

ssh www@192.168.100.150 "rm -rf /data/tomcat/appdir/*"
ssh www@192.168.100.150 "rm -rf /data/tomcat/appdir/*"

scp -r ./* www@192.168.100.150:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值