Jenkins+gitlab持续集成

之前我们已经部署了k8s和harbor仓库,本次来部署下jenkins和gitlab实现持续集成。

概念

持续集成是开发团队通过将代码的不同部分集成到共享存储库中,并频繁地进行构建和测试,以确保代码的一致性和稳定性。

流程
在现在的开发模式中,一般的项目,协同开发是离不开的,这就涉及到多个开发人员编写处理自己负责的功能模块或者某些开发人员共同负责一个模块。于是,通过版本控制系统可以将各个开发人员的代码集成在该共享存储库里,在存储库里,每个开发人员根据需求的不同来创建对应的分支,在完成需求后,每个人都需要提交合并将开发分支代码集成在一起,这就需要解决代码冲突,并且如何除了code review之外如何确保这些更改对应用没有产生影响,一旦提交请求合并到主分支,自动化构建工具就会根据流程自动编译构建安装应用,并执行单元测试框架的自动化测试来校验提交的修改。

机器规划如下

服务器名称 IP地址 配置
jenkins211 10.0.0.211 2核,4GiB,系统盘 20GiB
gitlab212 10.0.0.212 4核,4GiB,系统盘 20GiB

一、部署jenkins

1.下载jenkins

1
https://mirrors.jenkins-ci.org/debian/

2.准备jenkins部署环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1 部署Jenkins的秘钥
[root@jenkins211 ~]# sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian/jenkins.io-2023.key

2 添加Jenkins的存储库
[root@jenkins211 ~]# echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
https://pkg.jenkins.io/debian binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null

3 安装JRE环境(在线安装很慢,一般提前下载好jdk)
[root@jenkins211 ~]# sudo apt-get update
sudo apt-get install fontconfig openjdk-17-jre
sudo apt-get install jenkins

4 创建jdk安装目录,解压到指定目录
[root@jenkins211 ~]# mkdir -pv /softwares
[root@jenkins211 ~]# tar xf jdk-17_linux-x64_bin.tar.gz -C /softwares/


5 配置jdk环境变量
[root@jenkins211 ~]# vim .bashrc
...
export JAVA_HOME=/softwares/jdk-17.0.8
export PATH=$PATH:$JAVA_HOME/bin

[root@jenkins211 ~]# source .bashrc

6 验证jdk是否生效
[root@jenkins211 ~]# java --version
openjdk 17.0.13 2024-10-15
OpenJDK Runtime Environment (build 17.0.13+11-Ubuntu-2ubuntu122.04)
OpenJDK 64-Bit Server VM (build 17.0.13+11-Ubuntu-2ubuntu122.04, mixed mode, sharing)

3.部署jenkins

1
2
3
4
5
6
7
8
9
10
11
12
13
14
安装jenkins
[root@jenkins211 ~]# dpkg -i jenkins_2.489_all.deb
# apt-get install jenkins #这种在线方式安装较慢,不建议。

修改jenkins的启动脚本并重启服务
[root@jenkins211 ~]# vim /lib/systemd/system/jenkins.service
......
User=root
Group=root

...
Environment="JENKINS_HOME=/var/lib/jenkins"
# 找到上面一行后添加如下一行
Environment="JAVA_HOME=/oldboyedu/softwares/jdk-17.0.8"

4.访问jenkins地址

1
http://10.0.0.211:8080/login?from=%2F

img
5.查看密码

1
2
[root@jenkins211 ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
88259fc092c34ded983b6917872****d

6.安装jenkins插件

img7.创建用户

img好啦,就这样jenkins就简简单单部署完成了

img

8.修改时区

1
2
[root@jenkins211 ~]# ln -svf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
'/etc/localtime' -> '/usr/share/zoneinfo/Asia/Shanghai'

img

9.修改jenkins软件源

1
[root@jenkins211 ~]# sed -i.bak 's#updates.jenkins.io/download#mirror.tuna.tsinghua.edu.cn/jenkins#g' /var/lib/jenkins/updates/default.json

10.修改搜索引擎

1
[root@jenkins211 ~]# sed -i 's#www.google.com#www.baidu.com#g' /var/lib/jenkins/updates/default.json

11.替换jenkins国内下载地址

img

拉到最下面替换url

将升级站点URL替换成国内镜像地址
https://mirror.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

img

12.重新登录jenkins

http://10.0.0.211:8080/restart

二、部署gitlab

参考地址:https://about.gitlab.com/install/#ubuntu

  1. 安装并配置必要的依赖项
1
2
apt-get update
apt-get install -y curl openssh-server ca-certificates tzdata perl

2.安装 Postfix

1
apt-get install -y postfix

3.添加 GitLab 包仓库并安装包

1
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash

4.安装 GitLab 包

1
2
3
4
5
#url替换成自己本机的ip地址
EXTERNAL_URL="http://10.0.0.212" apt-get install gitlab-ee

#也可以选择离线部署
https://packages.gitlab.com/gitlab/gitlab-ce

5.查看密码

1
2
3
4
[root@gitlab212 ~]# cat /etc/gitlab/initial_root_password
....
Password: 8Et747zZeK0xKnx4gJ03HP+5MQ00yv4v3rzjy1a***=
....

6.登录

1
2
账号:root
密码:查看生成的密码

img

7.设置语言

点击偏好设置

img

选择中文–>保存

img8.开启本地网络请求

img

三、gitlab集成jenkins

1.创建群组

img

2.新建项目

img

img

3.添加ssh密钥

服务器和代码仓库连接方式有两种

第一种: 基于用户名和密码的方式连接

第二种: 基于SSH免秘钥方式(和命令行的SSH免秘钥相同)

我们使用基于免秘钥方式: 方便+安全性高

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@gitlab212 ~]# ssh-keygen     #生成密钥对
Generating public/private ed25519 key pair.
Enter file in which to save the key (/root/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_ed25519
Your public key has been saved in /root/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:GMd0qYCfisI7YgnL9gaTOvhSg9VWYVOtJ/ZmClhYCPc root@gitlab212
The key's randomart image is:
+--[ED25519 256]--+
| ...o+oo... |
| .oo++ .o |
| . =Eooo |
| . + ++= . |
|.o.o +..S+ |
|+=+ o . + |
|=+=. . + |
|BB . . |
|++=. |
+----[SHA256]-----+
[root@gitlab212 ~]#
[root@gitlab212 ~]# ll .ssh/
total 16
drwx------ 2 root root 4096 Dec 17 07:57 ./
drwx------ 4 root root 4096 Dec 15 12:01 ../
-rw------- 1 root root 0 Jul 23 16:40 authorized_keys
-rw------- 1 root root 411 Dec 17 07:57 id_ed25519 #私钥
-rw-r--r-- 1 root root 96 Dec 17 07:57 id_ed25519.pub #公钥
[root@gitlab212 ~]# cat .ssh/id_rsa.pub
cat: .ssh/id_rsa.pub: No such file or directory
[root@gitlab212 ~]# cat .ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILkNNA0/prl380e574O6+cfOSOAbpeJl+zhEdS5e58 root@gitlab212

4.将公钥贴近gitlab

img

添加公钥

img4.推送代码

配置邮箱

1
2
git config --local user.name "liangjh"
git config --local user.email "[email protected]"

创建仓库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#上传代码到服务器
[root@gitlab212 project]# pwd
/root/project
[root@gitlab212 project]# ll
total 228
drwxr-xr-x 6 root root 4096 Dec 17 08:09 ./
drwx------ 5 root root 4096 Dec 17 08:06 ../
-rw-r--r-- 1 root root 16458 Jun 13 2019 about.html
-rw-r--r-- 1 root root 20149 Jun 13 2019 album.html
-rw-r--r-- 1 root root 19662 Jun 13 2019 article_detail.html
-rw-r--r-- 1 root root 18767 Jun 13 2019 article.html
-rw-r--r-- 1 root root 18913 Jun 13 2019 comment.html
-rw-r--r-- 1 root root 16465 Jun 13 2019 contact.html
drwxr-xr-x 2 root root 4096 Sep 19 2022 css/
drwxr-xr-x 7 root root 4096 Dec 17 08:07 .git/
drwxr-xr-x 5 root root 4096 Sep 19 2022 images/
-rw-r--r-- 1 root root 29627 Jun 29 2019 index.html
drwxr-xr-x 2 root root 4096 Sep 19 2022 js/
-rw-r--r-- 1 root root 24893 Jun 13 2019 product_detail.html
-rw-r--r-- 1 root root 20672 Jun 13 2019 product.html


git clone [email protected]:ops/project.git
cd project
git switch --create main

#提交全部代码到缓存区
[root@gitlab212 project]# git add .

#提交到远程仓库
[root@gitlab212 project]# git commit -m "第一次提交"

#查看分支
[root@gitlab212 project]# git branch

#推送到master分支
[root@gitlab212 project]# git push -u origin master

5.gitlab查看

img

四、配置jenkins和gitlab免密

1.jenkins生成密钥对

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@jenkins211 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:8f0e/2XSZCdAGULp+LEkvVjeFo7YVJWv7HDFQDzfi1I root@jenkins211
The key's randomart image is:
+---[RSA 3072]----+
| .o.o*o. |
| .oo = |
| .+ .. *.|
| ooB..E *|
| [email protected]=|
| o B.=o+*.|
| ..++ +|
| ..=.|
| . +|
+----[SHA256]-----+
[root@jenkins211 ~]#
[root@jenkins211 ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCrTMTf0oqtW7GktgZBdhqkjCBp2xGCcGeEo8ZcEKaN352TDMGKRI5O+0gAwUBj9HMAkPASJsejtTQIrT5jkBuW/8L5RXPDqCCyuzz1nkW/rPiyVvuAvvkQxFMBxL4w/AoDAhcsxpV73DCyYTrjQ8Tj7PfSViEUKZFpPbtroOZeCgCVa/mNPj7hGI2xUapKd0rdiGpXlDMPlD1uN9TET1WTj157QbYbgtsA6y+K2hjG97HHta0miovC4vqbaKQuohbYThbkwE5v6Ndjg8WSYjXEO6SDgemR6/c/JIyMPv1rNUitzl6dg4jykJ2pCTsemvFL7Vdjypbn9l0bPK8cq+mz0W8ZKLd2Ijqshj67SFWjFxJs0/yabg3jdMTdnYxRYdiG2vaMlAmtgGbXAUGXMqCzXNftmSpyqlSxGhfKLUC76LCqc4nBhqt2reN3L5yd/rzVZENdpok8cdV/dZag8ibzlgeqOL60ONThYTPZTACyw+U2K2HhqLmoCVDpYxU= root@jenkins211

2.将公钥加入到gitlab

img

3.拉取测试

1
2
# 随便拉一个位置测试就行
[root@jenkins211 ~]# git clone [email protected]:ops/project.git

五、 jenkins推送代码到gitlab

1.jenkins创建项目

img

2.填写git地址img

3.jenkins构建

img

构建完成之后,代码会拉取到jenkins工作目录中,在日志中查看工作目录的位置

img

服务器查看,代码已经成功拉取到jenkins工作目录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@jenkins211 test]# pwd
/var/lib/jenkins/workspace/test
[root@jenkins211 test]# ll
total 228
drwxr-xr-x 6 root root 4096 Dec 17 17:22 ./
drwxr-xr-x 3 root root 4096 Dec 17 17:22 ../
-rw-r--r-- 1 root root 16458 Dec 17 17:22 about.html
-rw-r--r-- 1 root root 20149 Dec 17 17:22 album.html
-rw-r--r-- 1 root root 19662 Dec 17 17:22 article_detail.html
-rw-r--r-- 1 root root 18767 Dec 17 17:22 article.html
-rw-r--r-- 1 root root 18913 Dec 17 17:22 comment.html
-rw-r--r-- 1 root root 16465 Dec 17 17:22 contact.html
drwxr-xr-x 2 root root 4096 Dec 17 17:22 css/
drwxr-xr-x 8 root root 4096 Dec 17 17:22 .git/
drwxr-xr-x 5 root root 4096 Dec 17 17:22 images/
-rw-r--r-- 1 root root 29627 Dec 17 17:22 index.html
drwxr-xr-x 2 root root 4096 Dec 17 17:22 js/
-rw-r--r-- 1 root root 24893 Dec 17 17:22 product_detail.html
-rw-r--r-- 1 root root 20672 Dec 17 17:22 product.html

4.配置webhook

jenkins需要安装gitlab插件

img

勾选插件

img

jenkins生成令牌

img

gitlab添加令牌,填写jenkins url

img

gitlab测试钩子状态

img

六、测试gitlab和jenkins持续集成

gitlab修改代码提交,这里我们直接加个p标签测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@gitlab212 project]# vim about.html
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<p> 第一次提交 </p>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="applicable-device" content="pc,mobile" />
<title>关于我们</title>

提交代码到gitlab

1
2
3
4
5
git add .

git commit -m "第一次提交"

git push origin master

jenkins观察是否自动拉取,这时候jenkins检测到gitlab分支有变动,自动拉取代码到了jenkins工作目录中了~

img

1
2
3
4
5
6
7
8
9
10
11
[root@jenkins211 test]# cat about.html | head -10
?<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<p> 第一次提交 </p>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />

Jenkins+gitlab持续集成
http://example.com/2025/04/18/Jenkins-gitlab持续集成/
作者
淡黄的cherry
发布于
2025年4月18日
许可协议