CI/CD(Kylin)
持续集成/持续交付(ci/cd)
一、服务器准备
| IP | 主机名 | 配置 | 系统 |
|---|---|---|---|
| 10.0.0.200 | gitlab | 2核4g | ubt |
| 10.0.0.201 | Jenkins | 1核2g | kylin |
| 10.0.0.202 | nexus | 1核2g | kylin |
| 10.0.0.203 | sonar | 1核2g | kylin |
| 10.0.0.7 | web01(测试环境) | 1核1g | kylin |
| 10.0.0.8 | web02(发布环境) | 1核1g | kylin |
二、Git版本系统
1.企业常用的git版本系统
- SVN
- Git
2.页面版的代码仓库
-
gihub 全球使用最多的代码仓库
-
gitlab 全球使用最多的私有代码仓库
-
gitee 码云 中国使用最多的代码仓库
三、Git的使用
1.配置当前代码仓库的使用角色的信息
- 查看版本信息
root@dezyan:~# git --version
git version 2.34.1
- 创建目录,将该目录设置为代码仓库
root@dezyan:~# mkdir git_data
root@dezyan:~# cd git_data/
- 设置全局用户名/邮箱:在提交代码时,Git 会使用这个来标识提交的作者
root@dezyan:~# git config --global user.name "dzy"
root@dezyan:~# git config --global user.email "dzy@email.com"
#启用Git彩色输出功能
root@dezyan:~# git config --global color.ui true
#验证配置
root@dezyan:~# git config --list
user.name=dzy
user.email=dzy@email.com
color.ui=true
2.初始化仓库
- 初始化仓库
root@dezyan:~/git_data# git init
Reinitialized existing Git repository in /root/git_data/.git/
#查看是否初始化成功
root@dezyan:~/git_data# ll
. .. .git
- .git目录详解
root@dezyan:~/git_data# ll .git/
total 40
drwxr-xr-x 7 root root 4096 Feb 17 17:27 .
drwxr-xr-x 3 root root 4096 Feb 17 17:26 ..
drwxr-xr-x 2 root root 4096 Feb 17 17:26 branches #分支
-rw-r--r-- 1 root root 92 Feb 17 17:27 config #配置文件
-rw-r--r-- 1 root root 73 Feb 17 17:26 description #描述
-rw-r--r-- 1 root root 23 Feb 17 17:26 HEAD #头部
drwxr-xr-x 2 root root 4096 Feb 17 17:26 hooks #勾子
drwxr-xr-x 2 root root 4096 Feb 17 17:26 info #
drwxr-xr-x 4 root root 4096 Feb 17 17:26 objects #项目 代码放此目录但是不是明文的HASH
drwxr-xr-x 4 root root 4096 Feb 17 17:26 refs #缓存区
3.Git区域名称

-
工作目录:进入git_data目录 当前的位置称为工作目录
-
暂存区域: 临时存放代码的问题
-
本地仓库: 存储代码的位置
在工作目录写代码--->提交到暂存区域---->提交到本地仓库--->代码才真正的被管理。
每次走当前这个流程,相当于虚拟机做了一个快照的动作
4.Git常用命令
①<查>初始化、查看仓库状态、查看本地仓库中的文件
root@dezyan:~/git_data# git init
root@dezyan:~/git_data# git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
②<增>将文件从工作目录保存到本地仓库的流程
1)创建文件
root@dezyan:~/git_data# touch a.txt
2)提交到暂存区
git add 文件名
#若想将目录下全部文件都提交到暂存区使用 . 即可。即:git add .
root@dezyan:~/git_data# git add a.txt
3)将暂存区的内容提交到本地仓库
#执行一次commit操作 就相当于做了一个快照
root@dezyan:~/git_data# git commit -m "newfile a.txt"
[master (root-commit) a639e2c] newfile a.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
4) 提交完成后需要查看工作状态,必须是干净(clean)的
root@dezyan:~/git_data# git status
On branch master
nothing to commit, working tree clean
③<删>删除本地仓库中的文件
root@dezyan:~/git_data# git rm -f b.txt
rm 'b.txt'
root@dezyan:~/git_data# git ls-files
a.txt
c.txt
④<恢>误删工作目录文件恢复
#查看本地仓库全部文件
root@dezyan:~/git_data# git ls-files
a.txt
c.txt
#工作目录的文件也是这些
#删除工作目录的a.txt
root@dezyan:~/git_data# rm -rf a.txt
root@dezyan:~/git_data# ls
c.txt
#恢复
root@dezyan:~/git_data# git restore a.txt
root@dezyan:~/git_data# ls
a.txt c.txt
⑤<改>修改文件(本质就是重新上传)
#增添内容
root@dezyan:~/git_data# echo aaa > a.txt
root@dezyan:~/git_data# cat a.txt
aaa
#再次提交到暂存区
root@dezyan:~/git_data# git add .
#再次提交到本地仓库
root@dezyan:~/git_data# git commit -m "modifled aaa to a.txt"
[master 55da49a] modifled aaa to a.txt
2 files changed, 1 insertion(+)
delete mode 100644 b.txt
#查看仓库状态
root@dezyan:~/git_data# git status
On branch master
nothing to commit, working tree clean
⑥<比>工作目录文件与本地仓库文件比对
- 工作目录文件与本地仓库文件比对
#增添文件内容
root@dezyan:~/git_data# echo bbb> a.txt
#进行比对
root@dezyan:~/git_data# git diff a.txt
diff --git a/a.txt b/a.txt
index 72943a1..f761ec1 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1 @@
-aaa
+bbb #可以看到,是删除了aaa行,增添了bbb
- 暂存区域文件与本地仓库文件比对
#增添文件内容
root@dezyan:~/git_data# echo 111 >> c.txt
#上传到暂存区域
root@dezyan:~/git_data# git add .
#进行比对
root@dezyan:~/git_data# git diff --cached
diff --git a/c.txt b/c.txt
index e69de29..58c9bdf 100644
--- a/c.txt
+++ b/c.txt
@@ -0,0 +1 @@
+111 #可以看到,增加了一行111
⑦<回滚>版本回滚
#查看提交记录
root@dezyan:~/git_data# git log --oneline
8454ace (HEAD -> master) new c.txt
2b77c72 new a.txt
55da49a modifled aaa to a.txt
5085b34 newfile b c.txt
a639e2c newfile a.txt #选择此版本回滚
#版本回滚 git reset --hard 提交的哈希值
root@dezyan:~/git_data# git reset --hard a639e2c
HEAD is now at a639e2c newfile a.txt
#如果打了标签的话 git reset --hard 标签名
root@dezyan:~/git_data# git reset --hard v1.0
⑧<日志>查看历史提交日志
- 列表顺序从新到旧
#简略显示(只显示"提交的哈希值"和"提交信息")
root@dezyan:~/git_data# git log --oneline
2b77c72 (HEAD -> master) new a.txt
55da49a modifled aaa to a.txt
5085b34 newfile b c.txt
a639e2c newfile a.txt
#全量显示
root@dezyan:~/git_data# git log
commit 8454acecf12f3be6d179e00658cc6db82fef606d (HEAD -> master)
Author: dzy <dzy@email.com>
Date: Mon Feb 17 19:15:32 2025 +0800
new c.txt
#查看所有的历史提交
#当进行版本回滚后,git log只会显示当前以及以前的提交记录,之后的不会显示
root@dezyan:~/git_data# git log --oneline
a639e2c (HEAD -> master) newfile a.txt
#使用git reflog 可查看全部提交记录
root@dezyan:~/git_data# git reflog
a639e2c (HEAD -> master) HEAD@{0}: reset: moving to a639e2c
8454ace HEAD@{1}: commit: new c.txt
2b77c72 HEAD@{2}: commit: new a.txt
55da49a HEAD@{3}: commit: modifled aaa to a.txt
5085b34 HEAD@{4}: commit: newfile b c.txt
a639e2c (HEAD -> master) HEAD@{5}: commit (initial): newfile a.txt
5.Git分支
一般在实际的项目开发中,我们要尽量保证master分支是非常稳定的,仅用于发布新版本,平时不要随便直接修改里面的数据文件,而工作的时候则可以新建不同的工作分支,等到工作完成后在合并到master分支上面。
①查看当前所在分支
root@dezyan:~/git_data# git branch
* master
②创建分支
root@dezyan:~/git_data# git branch dev
root@dezyan:~/git_data# git branch
dev
* master
③切换到其他分支
root@dezyan:~/git_data# git checkout dev
Switched to branch 'dev'
root@dezyan:~/git_data# git branch
* dev
master
#增添新功能(文件)
root@dezyan:~/git_data# echo devfenzhi > 1.txt
root@dezyan:~/git_data# git add .
root@dezyan:~/git_data# git commit -m "new dev 1.txt"
[dev c4c8c3e] new dev 1.txt
1 file changed, 1 insertion(+)
create mode 100644 1.txt
root@dezyan:~/git_data# git status
On branch dev
nothing to commit, working tree clean
④分支合并
- 分支合并到master分支
#切换到主分支
root@dezyan:~/git_data# git checkout master
Switched to branch 'master'
root@dezyan:~/git_data# git branch
dev
* master
#合并dev的代码到master中
root@dezyan:~/git_data# git merge dev
Updating a639e2c..c4c8c3e
Fast-forward
1.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 1.txt
root@dezyan:~/git_data# git ls-files
1.txt
a.txt
- 合并后删除dev1分支
#创建新分支dev1
root@dezyan:~/git_data# git branch dev1
#切换到dev1分支
root@dezyan:~/git_data# git checkout dev1
Switched to branch 'dev1'
#查看当前所在分支
root@dezyan:~/git_data# git branch
dev
* dev1
master
#增加新功能并上传到本地仓库
root@dezyan:~/git_data# echo 222 > 2.txt
root@dezyan:~/git_data# git add .
root@dezyan:~/git_data# git commit -m "new dev1 2.txt"
[dev1 dd45965] new dev1 2.txt
1 file changed, 1 insertion(+)
create mode 100644 2.txt
root@dezyan:~/git_data# git status
On branch dev1
nothing to commit, working tree clean
#切换到master分支
root@dezyan:~/git_data# git checkout master
#合并后删除dev分支
root@dezyan:~/git_data# git branch -d dev
⑤删除分支
#使用 -D 可强制删除
root@dezyan:~/git_data# git branch -d dev
⑥冲突合并
1)master分支下的本地仓库1.txt中只有这两行信息
root@dezyan:~/git_data# cat 1.txt
devfenzhi
test
2)切换到dev分支,新加一行
root@dezyan:~/git_data# git checkout dev
Switched to branch 'dev'
root@dezyan:~/git_data# git branch
* dev
master
root@dezyan:~/git_data# echo nononono >> 1.txt
#dev分支下的本地仓库1.txt中只有这两行信息
root@dezyan:~/git_data# cat 1.txt
devfenzhi
nononon
3)切换到master分支下合并
root@dezyan:~/git_data# git checkout master
Switched to branch 'master'
root@dezyan:~/git_data# git branch
dev
* master
4)合并代码会有冲突提示
root@dezyan:~/git_data# git merge dev
Auto-merging 1.txt
CONFLICT (content): Merge conflict in 1.txt
Automatic merge failed; fix conflicts and then commit the result.
root@dezyan:~/git_data# cat 1.txt
devfenzhi
<<<<<<< HEAD
test
=======
nononono
>>>>>>> dev
5)解决方式:使用vim手动解决留下想要的代码
root@dezyan:~/git_data# vim 1.txt
root@dezyan:~/git_data# cat 1.txt
devfenzhi
test
nononono
root@dezyan:~/git_data# git add .
root@dezyan:~/git_data# git commit -m "newnewnenw"
[master 6cfb03f] newnewnenw
6.git标签
标签也是指向了一次commit提交,是一个里程碑式的标签,
回滚打标签直接加标签号,不需要加唯一字符串不好记,标签即为版本号
#查看所有提交记录
root@dezyan:~/git_data# git log --oneline
6cfb03f (HEAD -> master) newnewnenw
5ec8674 (dev) dev new 1.txt
0f6705b new 1.txt 2
c4c8c3e new dev 1.txt
a639e2c newfile a.txt
#给最早的提交版本打标签 git tag -a 标签名 提交的哈希值 -m 说明
root@dezyan:~/git_data# git tag -a v1.0 a639e2c -m "初始版本"
#查看所有标签
root@dezyan:~/git_data# git tag
v1.0
#查看标签详细信息 git show 标签名
root@dezyan:~/git_data# git show v1.0
tag v1.0
Tagger: dzy <dzy@email.com>
Date: Mon Feb 17 19:58:33 2025 +0800
初始版本
commit a639e2c0f538977388f224f2c58e826a49016e39 (tag: v1.0)
Author: dzy <dzy@email.com>
Date: Mon Feb 17 17:48:43 2025 +0800
newfile a.txt
diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..e69de29
#通过tag版本回滚代码
root@dezyan:~/git_data# git reset --hard v1.0
#删除标签
root@dezyan:~/git_data# git tag -d v1.0
Deleted tag 'v1.0' (was 2cde558)
四、gitlab的安装使用
GitLab 是一个用于仓库管理系统的开源项目。使用Git作为代码管理工具,并在此基础上搭建起来的web服务。可通过Web界面进行访问公开的或者私人项目。它拥有与Github类似的功能,能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。团队成员可以利用内置的简单聊天程序(Wall)进行交流。它还提供一个代码片段收集功能可以轻松实现代码复用。
- 国内官网:https://gitlab.cn/
- 国外官网:https://about.gitlab.com/
- 国内镜像源:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/jammy/main/g/gitlab-ce/
1.安装gitlab
①安装环境
1、 ubuntu22.04
2、 4G内存(实验)生产(至少6G)
3、 安装包:gitlab-ce_16.5.2-ce.0_amd64
4、 禁用防火墙,关闭selinux
②安装准备
#yum仓库更新
root@dezyan:~# apt-get update
#安装仓库依赖
root@dezyan:~# apt-get install -y curl openssh-server ca-certificates tzdata perl
#运行官网给的脚本
root@dezyan:~# curl -L get.gitlab.cn | bash
③安装过程
方式1:使用apt安装
root@dezyan:~# EXTERNAL_URL="http://10.0.0.200" apt-get install -y gitlab-jh
方式2:使用deb包安装(Centos使用rpm包)
root@dezyan:~# dpkg -i gitlab-ce_16.5.2-ce.0_amd64.deb
root@dezyan:~# vim /etc/gitlab/gitlab.rb
external_url 'http://your-server-ip:port'
#修改此行为external_url 'http://10.0.0.200:80'
root@dezyan:~# gitlab-ctl reconfigure
#查看gitlab状态
root@dezyan:~# gitlab-ctl status
#停止gitlab
root@dezyan:~# gitlab-ctl stop
#启动gitlab
root@dezyan:~# gitlab-ctl start
④访问gitlab
访问设定的EXTERNAL_URL="http://10.0.0.200"
默认用户: root
临时密码存储在
cat /etc/gitlab/initial_root_password
2.修改语言及密码
1.进入偏好设置

2.分别修改密码和语言


3.创建新项目
①第一种方式(空的代码仓库)
空的代码仓库,在gitlab创建代码仓库,然后拉取到本地服务器
1)创建群组

2)填写群组信息

3)创建仓库



4)打通系统和gitlab root账号的SSH免秘钥
ubt生成密钥对
root@dezyan:~# ssh-keygen
root@dezyan:~# cat .ssh/id_rsa.pub
在gitlab设置中添加ssh秘钥

5)命令行将空的仓库下载到本地

root@dezyan:~# git clone git@10.0.0.200:dezyan/test.git
root@dezyan:~# ls
git_data gitlab-ce_16.5.2-ce.0_amd64.deb snap test
root@dezyan:~# cd test/
root@dezyan:~/test# ll
total 12
drwxr-xr-x 3 root root 4096 Feb 17 20:51 .
drwx------ 9 root root 4096 Feb 17 20:51 ..
drwxr-xr-x 7 root root 4096 Feb 17 20:51 .git
6)配置使用人、email等
root@dezyan:~/test# git config --global user.name "dzy"
root@dezyan:~/test# git config --global user.email "dzy@email.com"
root@dezyan:~/test# git config --global color.ui true
7)创建新的文件提交到本地仓库
root@dezyan:~/test# touch test.txt
root@dezyan:~/test# git add .
root@dezyan:~/test# git commit -m "new file test.txt"
8)查看远程仓库名称及详细地址
root@dezyan:~/test# git remote
origin
root@dezyan:~/test# git remote -v
origin git@10.0.0.200:dezyan/test.git (fetch)
origin git@10.0.0.200:dezyan/test.git (push)
9)将本地仓库代码提交到远程仓库
git push -u 仓库名 分支名
root@dezyan:~/test# git push -u origin master

②第二种方式(代码已经存在本地)
代码已经存在本地
1.本地仓库已经初始化
2.本地代码已经存在
1)若无群组也应先创建群组
2)查看已上传至本地仓库的代码
root@dezyan:~# cd git_data/
root@dezyan:~/git_data# git ls-files
1.txt
a.txt
3)gitlab上创建新的仓库(项目)

4)本地仓库配置远程仓库的地址
#查看默认的远程仓库 是没有的
root@dezyan:~/git_data# git remote
root@dezyan:~/git_data# git remote -v
#配置远程仓库名称为origin 仓库名后方的连接到gitlab仓库界面中复制
root@dezyan:~/git_data# git remote add origin git@10.0.0.200:dezyan/git_data.git
5)推送代码到git_data仓库
#本地分支名与远程分支名不统一,可使用一下命令修改
#git branch -m 旧名字 新名字
root@dezyan:~/git_data# git branch -m master main
#推送
root@dezyan:~/git_data# git push -u origin main
Enumerating objects: 15, done.
Counting objects: 100% (15/15), done.
Delta compression using up to 4 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (15/15), 1.15 KiB | 1.15 MiB/s, done.
Total 15 (delta 0), reused 0 (delta 0), pack-reused 0
To 10.0.0.200:dezyan/git_data.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

4.将小霸王网页代码推送至远程的test项目中
①删除本地仓库测试文件
root@dezyan:~/test# git rm -f test.txt
rm 'test.txt'
root@dezyan:~/test# git add .
root@dezyan:~/test# git commit -m "none"
②上传小霸王游戏代码并解压
root@dezyan:~/test# ls
bgm.mp3 images js shuoming.html
css index.html roms 小霸王_FC怀旧游戏机-HTML源码.zip
③提交到本地仓库并推送至远程仓库
root@dezyan:~/test# git add .
root@dezyan:~/test# git commit -m "game v1.0"
root@dezyan:~/test# git push -u origin master

5.创建普通用户dev,邀请其加入项目
①创建


②设置密码(不能与管理员密码相同)


③登录普通账号,界面为空

④使用管理员账户将dev添加至项目组




给dev用户权限!!!!!

⑤修改dev账号的ssh秘钥(使用web01模拟开发电脑)

⑥下载游戏代码至本地
[root@web01 ~]# git clone git@10.0.0.200:dezyan/test.git
[root@web01 ~]# ls
anaconda-ks.cfg a.out initial-setup-ks.cfg test
[root@web01 ~]# cd test/
[root@web01 test]# ls
bgm.mp3 images js shuoming.html
css index.html roms 小霸王_FC怀旧游戏机-HTML源码.zip
6.开发上传代码的流程
①配置好使用人信息,修改代码提交到本地仓库
[root@web01 test]# git config --global user.email "dev@mail.com"
[root@web01 test]# git config --global user.name "dev"
[root@web01 ~]# cd test/
[root@web01 test]# vim index.html
#任意修改一行模仿开发修改代码
#上传到本地仓库
[root@web01 test]# git commit -am "森林"
②提交到远程仓库
- 默认不允许提交到master主干分支,需要先创建子分支,然后提交子分支,在发起代码合并请求master
#创建新分支dev
[root@web01 test]# git branch dev
#推送代码至远程仓库的dev分支
[root@web01 test]# git push -u origin dev

③开发账号发起代码合并请求


④管理员账号同意合并请求即可合并代码

查看源分支是否删除、代码是否更改


五、Jenkins
Jenkins官网:jenkins.io
Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。
Jenkins在持续集成中的作用:
1.安装Jenkins
①配环境
1)安装JDK运行环境
[root@jenkins ~]# yum install -y java
2)使用rpm包方式安装Jenkins
[root@jenkins ~]# rpm -ivh jenkins-2.405-1.1.noarch.rpm
3)修改Jenkins配置文件的启动用户
[root@jenkins ~]# vim /usr/lib/systemd/system/jenkins.service
34 User=root
35 Group=root
[root@jenkins ~]# vim /etc/sysconfig/jenkins
31 JENKINS_USER="root"
4)重新加载配置文件,启动Jenkins
[root@jenkins ~]# systemctl daemon-reload
[root@jenkins ~]# systemctl start jenkins.service
#启动后会在/var/lib/jenkins中生成若干文件
②配置Jenkins的插件
1)上传打包好的插件包到/var/lib/jenkins/plugins并解压
[root@jenkins plugins]# tar xf jenkins_plu.tar.gz
2)重新启动Jenkins
[root@jenkins plugins]# systemctl restart jenkins.service
③打开浏览器进行页面安装步骤
- 访问10.0.0.201:8080

[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
39f7b88c0e284992b9a1c6ffc0162d23
- 由于我们已经将插件打包放入了plugin目录中,所以不需要再安装插件


④及时修改Jenkins密码

2.手动的将gitlab中的代码运行到web服务器
- 让小霸王游戏代码运行在web01(测试环境)上
[root@web01 conf.d]# cat game.conf
server {
listen 80;
server_name _;
location / {
root /code/game;
index index.html;
}
}
[root@web01 conf.d]# cd /code/game/
[root@web01 game]# git clone git@10.0.0.200:dezyan/test.git
[root@web01 game]# ls
bgm.mp3 images js shuoming.html
css index.html roms 小霸王_FC怀旧游戏机-HTML源码.zip
3.查看Jenkins默认执行的shell路径
①新建任务


②编辑配置


③运行此Jenkins

④查看控制台输出

由此可发现,默认的路径为/var/lib/jenkins/workspace/
4.Jenkins拉取gitlab的代码
gitlab服务器需要与Jenkins服务器打通秘钥,并在设置中添加ssh秘钥
①Jenkins生成秘钥对并复制到gitlab的root账号中
[root@jenkins:~]#ssh-keygen
[root@jenkins ~]# cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDmh0qD3pLFpI7tQiPebFYYAnMeyI6jPEhP+Jk………………

②配置jenkins拉取game代码

③立即构建测试
- 查看工作目录代码是否已经被拉取
[root@jenkins ~]# ll /var/lib/jenkins/workspace/game_test/
total 7768
-rw-r--r-- 1 root root 28032 Feb 18 19:26 bgm.mp3
drwxr-xr-x 2 root root 23 Feb 18 19:26 css
drwxr-xr-x 2 root root 23 Feb 18 19:26 images
-rw-r--r-- 1 root root 8963 Feb 18 19:26 index.html
drwxr-xr-x 2 root root 213 Feb 18 19:26 js
drwxr-xr-x 2 root root 4096 Feb 18 19:26 roms
-rw-r--r-- 1 root root 811 Feb 18 19:26 shuoming.html
-rw-r--r-- 1 root root 7902976 Feb 18 19:26 小霸王_FC怀旧游戏机-HTML源码.zip
④代码推送到web服务器
- 首先要打通秘钥
[root@jenkins ~]# ssh-copy-id 10.0.0.7
- 然后在
构建步骤(build steps)中执行shell推送命令
scp -r ./* 10.0.0.7:/code/game

⑤配置自动触发webhook勾子
- 在
构建触发器中勾选Build when a change is pushed to GitLab.选项

- 在高级选项中生成token

- 保留此token
18fa15365f241a999c34913a20d9a68f
⑥gitlab页面配置勾子


⑦合并代码测试是否自动推送
- 此处节省时间,直接在gitlab中修改

- 查看Jenkins,发现成功触发

- 查看网页是否被更改

六、SonarQube代码扫描
- jenkins将代码拉取到jenkins本地,先将代码推送到sonar服务器上代码扫描检测,检测漏洞 逻辑 坏味道。
1.安装SonarQube
①安装java
[root@sonar ~]# yum -y install java
②安装mysql
[root@sonar ~]# wget dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
[root@sonar ~]# rpm -ivh mysql-community-release-el6-5.noarch.rpm
#禁止gpgcheck
[root@sonar ~]# vim /etc/yum.repos.d/mysql-community.repo
...
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
....
[root@sonar ~]# yum -y install mariadb-server
[root@sonar ~]# service mysqld start
[root@sonar ~]# mysqladmin -uroot password dzy123
[root@sonar ~]# mysql -uroot -pdzy123 -e "CREATE DATABASE sonar DEFAULT CHARACTER SET utf8;"
[root@sonar ~]# mysql -uroot -pdzy123 -e "show databases;"
③安装sonarqube
1.上传包,解压
[root@sonar ~]# unzip sonarqube-7.0.zip -d /usr/local/
2.原文件名太长创建软连接方便使用
[root@sonar ~]# ln -s /usr/local/sonarqube-7.0/ /usr/local/sonarqube
3.修改连接数据库信息
[root@sonar ~]# cd /usr/local/sonarqube/conf/
[root@sonar conf]# vim sonar.properties
16 sonar.jdbc.username=root
17 sonar.jdbc.password=dzy123
26 sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewr iteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
4.创建普通用户sonar
sonarqube服务必须由普通用户运行。
[root@sonar conf]# useradd sonar
[root@sonar conf]# chown -R sonar.sonar /usr/local/sonarqube-7.0/
5.使用sonar用户运行服务
[root@sonar conf]# su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start"
访问10.0.0.203:9000
6.安装插件
[root@sonar conf]# cd /usr/local/sonarqube/extensions/
[root@sonar extensions]# cd plugins/
[root@sonar plugins]# ll
[root@sonar plugins]# rm -rf *
上传打包好的插件
[root@sonar plugins]# ll
total 44052
-rw-r--r-- 1 root root 45106788 Sep 27 17:20 sonar_plugins.tar.gz
[root@sonar plugins]# tar xf sonar_plugins.tar.gz
[root@sonar plugins]# mv plugins/* .
7.重新启动服务
[root@sonar plugins]# su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart"
④登录
用户名密码均为admin

2.sonar创建项目
- 首次登录后会弹出欢迎界面,创建令牌

- 按项目需求填写

- 完成后会给出一套变量

sonar-scanner \
-Dsonar.projectKey=html \
-Dsonar.sources=. \
-Dsonar.host.url=http://10.0.0.203:9000 \
-Dsonar.login=962b919eda0053f08d89fb3e60232b1c03611a8b
3.客户端执行代码扫描上传到sonar服务器
- 在Jenkins中安装sonar客户端
#上传解压重命名
[root@jenkins:~]#ll sonar-scanner-cli-4.2.0.1873-linux.zip
-rw-r--r-- 1 root root 42397119 Sep 27 17:20 sonar-scanner-cli-4.2.0.1873-linux.zip
[root@jenkins:~]#unzip sonar-scanner-cli-4.2.0.1873-linux.zip -d /usr/local/
[root@jenkins:~]#mv /usr/local/sonar-scanner-4.2.0.1873-linux/ /usr/local/sonar-scanner
[root@jenkins:~]#ll /usr/local/
drwxr-xr-x 6 root root 51 Oct 1 2019 sonar-scanner
- 将客户端命令写入PATH变量
[root@jenkins ~]# tail -1 /etc/profile
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/local/sonar-scanner/bin"
[root@jenkins ~]# source /etc/profile
- 执行代码扫描
[root@jenkins ~]# sonar-scanner \
-Dsonar.projectKey=html \
-Dsonar.sources=. \
-Dsonar.host.url=http://10.0.0.203:9000 \
-Dsonar.login=962b919eda0053f08d89fb3e60232b1c03611a8b
INFO: Task total time: 4.578 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS #执行成功
INFO: ------------------------------------------------------------------------
INFO: Total time: 7.274s
INFO: Final Memory: 7M/116M
INFO: ------------------------------------------------------------------------
4.配置Jenkins集成sonarqube
①构建步骤

- 默认jenkins页面找不到服务端和客户端,需要手动配置
②配置服务器的信息



③配置客户端信息位置


④继续构建步骤
sonar.projectName=${JOB_NAME} # 项目在sonarqube上的显示名称
sonar.projectKey=html # 项目的唯一表示,不能重复
sonar.sources=. # 扫描那个项目的源码

⑤修改sonar客户端(即Jenkins)指向10.0.0.203
[root@jenkins ~]# vim /usr/local/sonar-scanner/conf/sonar-scanner.properties
#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://10.0.0.203:9000
#此处值为首次创建项目时给你的变量-Dsonar.login
sonar.login=962b919eda0053f08d89fb3e60232b1c03611a8b
#----- Default source code encoding
#sonar.sourceEncoding=UTF-8
⑥查看并测试
在gitlab上更新代码,Jenkins自动推送,网页显示正确
SonarQube平台实时更新,即配置成功

七、配置企业微信
1.配置插件
- 由于jenkins没有官网的插件来完成此功能,所以我们只能用网络上一些开源的插件
- 本文汇总已将插件集成到插件包中,无需下载
本次使用的是github上的开源代码
地址为:
https://github.com/daniel-beck/changelog-environment-plugin
使用方法为:
解压到某个目录-》进入目录执行以下操作
cd 到 changelog-environment-plugin-master 下,执行mvn verify
时间较长,会在changelog-environment-plugin-master/target/下有个changelog-environment.hpi文件,上传到jenkins即可使用
2.配置jenkins
Add Changelog Information to Environment->点击选择
Entry Format中添加 %3$s(at %4$s via %1$s),参数分别为ChangeLog内容,时间,提交人。
Date Format中添加 yyyy‐MM‐dd HH:mm:ss 就是时间格式。

3.上传py脚本,并修改信息
[root@jenkins ~]# mkdir -p /server/scripts
[root@jenkins ~]# cd /server/scripts/
[root@jenkins scripts]# ll
total 4
-rw-r--r-- 1 root root 2008 Sep 27 17:18 jenkins_notify.py
[root@jenkins scripts]# vim jenkins_notify.py
data = {
"touser" : "DingZhiYan",
"msgtype" : "text",
"agentid" : 1000002,
if __name__ == '__main__':
Corpid = "ww51daa7d6d57764aa"
Secret = "GEIa60nSiLxiREA8nPKB6wdGkr-nrkJ2GIMemXKjtEs"
4.命令行测试
[root@jenkins:~]#yum -y install python2
[root@jenkins:~]#yum -y install python2-pip
[root@jenkins:~]#pip2.7 install requests
[root@jenkins:~]#python2.7 jenkins_notify.py test /etc/hosts game

5.集成到jenkins
- 注意传了3个参数
echo "==========Start Notify=============="
echo ${SCM_CHANGELOG} > /tmp/${JOB_NAME}_change.log
python2.7 /server/scripts/jenkins_notify.py ${BUILD_URL} /tmp/${JOB_NAME}_change.log ${JOB_NAME}
rm ‐fv /tmp/${JOB_NAME}_change.log

6.更改代码测试

八、线上发布流程

1.配置jenkins获取gitlab中所有的版本号
①创建一个新项目

②配置丢弃旧的构建

③配置参数化构建过程

④添加git仓库

2.在gitlab中将当前的稳定版本打tag、提交
root@dezyan:~/game# git tag -a v1.1 -m "v1.1测试版"
root@dezyan:~/game# git push -u origin v1.1
- 查看gitlab界面发现有一个标签

3.配置web02作为发布环境
WEB02nginx配置
[root@web02:conf.d]#cat wordpress.conf
server {
listen 80;
server_name _;
root /code/game;
location / {
index index.html;
}
}
[root@web02:conf.d]#nginx -t
systemc restart nginx
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02:conf.d]#systemctl restart nginx
jenkins和web02做免秘钥
[root@jenkins:~]#ssh-copy-id 10.0.0.8
4.配置代码回滚
①在版本信息后添加选项参数

②填写选项信息


③填写构建步骤,执行shell脚本
[root@jenkins:scripts]#cat deploy.sh
#!/bin/bash
code_tar(){
tar zcvf /opt/web.tar.gz ./*
}
code_cp(){
scp /opt/web.tar.gz 10.0.0.8:/code/
}
code_un(){
ssh 10.0.0.8 "mkdir -p /code/web_code_$git_version"
ssh 10.0.0.8 "cd /code;tar xf web.tar.gz -C web_code_$git_version && rm -rf web.tar.gz"
}
code_ln(){
ssh 10.0.0.8 "rm -rf /code/game && ln -s /code/web_code_$git_version /code/game"
}
main(){
code_tar
code_cp
code_un
code_ln
}
if [ $deploy_env = "deploy" ];then
main
elif [ $deploy_env = "rollback" ];then
code_ln
fi
避免重复构建
当重复执行构建后会生成多个相同版本的文件,利用jenkins变量值解决重复性构建问题
jenkins变量
1. GIT_COMMIT 当前版本提交产生的哈希唯一值
2. GIT_PREVIOUS_SUCCESSFUL_COMMIT 已经提交过的版本的哈希唯一值
使用以上两个值做比较,如果已提交则退出,如果没有提交过则继续执行构建,更改脚本做判断
if [ "$deploy_env" == "deploy" ];then
if [ "$GIT_COMMIT" == "$GIT_PREVIOUS_SUCCESSFUL_COMMIT" ];then
echo "构建失败该 $git_version 版本号已部署"
else
main
fi
elif [ "$deploy_env" == "rollback" ];then
ln_html
fi
------------------也可以判断目标主机版本号是否存在
Te=`ssh 10.0.0.8 "ls -l /code/|grep $git_version"|wc -l`
if [ $deploy_env = "deploy" ];then
if [ $Te -eq 0 ];then
main
else
echo "该版本已经部署过,不可以重复构建"
exit
fi
elif [ $deploy_env = "rollback" ];then
code_ln
fi
④查看项目

⑤多打几个标签上传测试
九、流水线
1.创建一个流水线

2.在gtilab的game项目中创建Jenkinsfile文件

pipeline{
agent any
stages{
stage("get code"){
steps{
echo "get code"
}
}
stage("unit test"){
steps{
sh '/usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=html -Dsonar.projectName=${JOB_NAME} -Dsonar.sources=.'
}
}
stage("package"){
steps{
sh 'tar zcf /opt/web-${BUILD_ID}.tar.gz ./*'
}
}
stage("deploy"){
steps{
sh 'ssh 10.0.0.8 "cd /code/ && mkdir web-${BUILD_ID}"'
sh 'scp /opt/web-${BUILD_ID}.tar.gz 10.0.0.8:/code/web-${BUILD_ID}'
sh 'ssh 10.0.0.8 "cd /code/web-${BUILD_ID} && tar xf web-${BUILD_ID}.tar.gz && rm -rf web-${BUILD_ID}.tar.gz"'
sh 'ssh 10.0.0.8 "cd /code && rm -rf game && ln -s web-${BUILD_ID} /code/game"'
}
}
}
}
3.在Jenkins中使用远程代码仓库的流水线文件


4.立即构建查看

十、java项目发布流程
1.什么是Maven
Maven是一个项目管理和综合工具。Maven提供给开发人员构建一个完整的生命周期框架。
开发团队可以自动完成该项目的基础设施建设,Maven使用标准的目录结构和默认构建生命周期。
Apache的开源项目主要服务于JAVA平台的构建、依赖管理、项目管理。
Project Object Model,项目对象模型。
通过xml格式保存的pom.xml文件。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。
该文件是由开发维护,我们运维人员可以不用去关心。
2.安装Maven
①下载Maven3安装包
官网:http://maven.apache.org/download.cgi
清华镜像:https://mirrors.tuna.tsinghua.edu.cn/apache/maven/
②解压安装Maven
[root@jenkins ~]# tar xf apache-maven-3.3.9-bin.tar.gz -C /usr/local/
[root@jenkins local]# ln -s apache-maven-3.3.9 /usr/local/maven
#查看Maven的版本信息
[root@jenkins local]# /usr/local/maven/bin/mvn -v
③配置环境变量
[root@jenkins ~]# vim /etc/profile
export PATH="$PATH:/usr/local/maven/bin/mvn"
[root@jenkins ~]# mvn -v
④上传一个简单的java项目
[root@jenkins ~]# ll
-rw-r--r-- 1 root root 18950 Sep 27 17:18 hello-world.tar.gz
[root@jenkins ~]# tar xf hello-world.tar.gz
[root@jenkins ~]# cd hello-world-war/
[root@jenkins hello-world-war]# ll
total 8
drwxr-xr-x 2 root root 29 May 23 2014 dist
-rw-r--r-- 1 root root 931 May 14 2024 pom.xml
-rw-r--r-- 1 root root 213 May 23 2014 README.md
drwxr-xr-x 3 root root 18 May 23 2014 src
⑤执行mvn package、
mvn package 是 Maven 的一个常用命令,用于将项目编译、测试并打包成可分发的格式(如 JAR、WAR 或其他格式)。
mvn package 会触发 Maven 的生命周期,依次执行以下阶段:
validate(验证): 验证项目正确,并且所有必要信息可用。
compile(编译): 编译项目源码
test(测试): 使用合适的单元测试框架测试编译后的源码。
package(打包): 源码编译之后,使用合适的格式(例如JAR格式)对编译后的源码进行打包。
除此之外,还有以下周期操作
integration‐test(集成测试): 如果有需要,把包处理并部署到可以运行集成测试的环境中去。
verify(验证): 进行各种测试来验证包是否有效并且符合质量标准。
install(安装): 把包安装到本地仓库,使该包可以作为其他本地项目的依赖。
deploy(部署): 在集成或发布环境中完成,将最终软件包复制到远程存储库,以与其他开发人员和项目共享。
mvn clean (清除) : 清除上次编译的结果
mvn package ‐Dmaven.test.skip=true 跳过测试用例
[root@jenkins hello-world-war]# mvn package
[INFO] --------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------
[root@jenkins hello-world-war]# ls
dist pom.xml README.md src target #会出现target目录
⑥web02部署tomcat
yum install -y java
tar xf apache-tomcat-9.0.98.tar.gz -C /usr/local/
cd /usr/local/
ln -s apache-tomcat-9.0.98 /usr/local/tomcat
[root@web02 tomcat]# ./bin/startup.sh
[root@web02 tomcat]# netstat -tnulp
查看8080端口是否启动
⑦将war包拷贝tomcat的站点目录下,解压,访问
[root@jenkins hello-world-war]# scp target/hello-world-war-1.0.0.war 10.0.0.8:/usr/local/tomcat/webapps/ROOT/
⑧解压war包 启动tomcat访问
[root@web02 ROOT]# unzip hello-world-war-1.0.0.war
[root@web02 ROOT]# /usr/local/tomcat/bin/shutdown.sh && sleep 1 && /usr/local/tomcat/bin/startup.sh

3.将默认的maven仓库源修改为国内的
[root@jenkins hello-world-war]# cd /usr/local/maven/conf/
[root@jenkins conf]# vim settings.xml
#在</mirrors>前添加
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
#测试
[root@jenkins conf]# cd ~/hello-world-war/
[root@jenkins hello-world-war]# mvn clean
#发现输出提示均为阿里源
Downloaded: http://maven.aliyun.com/nexus/content/gro……
4.集成到Jenkins
①将java代码推送到gitlab
3.将gitlab创建的项目配置为本地的远程仓库
root@ubuntu:~# cd hello-world-war/
#查看默认是否存在远程仓库
root@ubuntu:~/hello-world-war# git remote
origin
#删除默认的远程仓库
root@ubuntu:~/hello-world-war# git remote remove origin
root@ubuntu:~/hello-world-war# git remote
root@ubuntu:~/hello-world-war# git remote add origin git@10.0.0.201:dezyan/java.git
root@ubuntu:~/hello-world-war# git remote -v
origin git@10.0.0.201:dezyan/java.git (fetch)
origin git@10.0.0.201:dezyan/java.git (push)
#将代码提交到本地仓库
root@ubuntu:~/hello-world-war# git add .
root@ubuntu:~/hello-world-war# git commit -m "xx"
#修改主分支的名称为main
root@ubuntu:~/hello-world-war# git branch -m master main
root@ubuntu:~/hello-world-war# git branch
* main
#然后将本地仓库的代码提交到远程仓库
root@ubuntu:~/hello-world-war# git push -u origin main
②jenkins创建maven项目





scp target/hello-world-war-1.0.0.war 10.0.0.8:/usr/local/tomcat/webapps/ROOT/
ssh 10.0.0.8 "cd /usr/local/tomcat/webapps/ROOT/ && unzip *.war && rm -rf *.war"
ssh 10.0.0.8 "/usr/local/tomcat/bin/shutdown.sh && sleep 1 && /usr/local/tomcat/bin/startup.sh"

- 完成后保存立即构建,测试
![image-20250219164422654]()
5.配置nexus私服
-
配置仓库两个选项
1、项目下的pom.xml配置、只生效当前的项目
2、在maven配置全局所有项目生效
①上传JDk安装
[root@nexus ~]# ll
-rw-r--r-- 1 root root 170023183 Sep 27 17:19 jdk-8u181-linux-x64.rpm
[root@nexus ~]# rpm -ivh jdk-8u181-linux-x64.rpm
②上传nexus、解压、启动、访问
[root@nexus ~]# ll
-rw-r--r-- 1 root root 122904706 Sep 27 17:19 nexus-3.13.0-01-unix.tar.gz
[root@nexus ~]# tar xf nexus-3.13.0-01-unix.tar.gz -C /usr/local/
[root@nexus local]# ln -s nexus-3.13.0-01 /usr/local/nexus
[root@nexus local]# /usr/local/nexus/bin/nexus start
#访问:10.0.0.202:8081
#用户名admin 密码admin123
③配置nexus仓库指向阿里云

http://maven.aliyun.com/nexus/content/groups/public

④修改jenkins编译指向nexus私服
[root@jenkins conf]# cd /usr/local/maven/conf/
[root@jenkins conf]# mv settings.xml settings.xml.back
[root@jenkins conf]# #上传配置文件settings.xml
[root@jenkins conf]# rz -E
rz waiting to receive.
[root@jenkins conf]# 修改配置文件信息 如果已经上传无需修改,上传的就是修改好的
[root@jenkins conf]# vim settings.xml
132 -->
133 <server>
134 <id>my-nexus-releases</id>
135 <username>admin</username>
136 <password>admin123</password>
137 </server>
138 <server>
139 <id>my-nexus-snapshot</id>
140 <username>admin</username>
141 <password>admin123</password>
142 </server>
143 </servers>
144
...
155 |-->
156 <mirrors>
157 <mirror>
158 <id>nexus</id>
159 <mirrorOf>*</mirrorOf>
160 <url>http://10.0.0.204:8081/repository/maven-public/</url>
161 </mirror>
162 <!-- mirror
....
261 -->
262 <profile>
263 <id>nexus</id>
264 <repositories>
265 <repository>
266 <id>central</id>
267 <url>http://10.0.0.204:8081/repository/maven-public/</url>
268 <releases><enabled>true</enabled></releases>
269 <snapshots><enabled>true</enabled></snapshots>
270 </repository>
271 </repositories>
272 <pluginRepositories>
273 <pluginRepository>
274 <id>central</id>
275 <url>http://10.0.0.204:8081/repository/maven-public/</url>
276 <releases><enabled>true</enabled></releases>
277 <snapshots><enabled>true</enabled></snapshots>
278 </pluginRepository>
279 </pluginRepositories>
280 </profile>
281 </profiles>
282
...
290 -->
291 <activeProfiles>
292 <activeProfile>nexus</activeProfile>
293 </activeProfiles>
294 </settings>
⑤编译测试
[root@jenkins hello-world-war]# mvn clean
#发现编译地址已变为本地服务器
Downloaded: http://10.0.0.202:8081/
十一、配置jenkins的分布式
-
如果项目需要定期集成,同时每次集成都需要较长时间。如果都运行在master服务器上,会消耗过多资源,导致其他项
-
目搁置无法集成,这时就需要在建立多台设备,并配置作为slave机器来为master提供负载服务。
-
使用lb02服务器作为slave测试
1.在lb02上安装java jdk git和soanr客户端
[root@lb02 ~]# yum install -y java
[root@lb02 ~]# rpm -ivh jdk-17_linux-x64_bin.rpm
#做免秘钥
[root@jenkins ~]# ssh-copy-id 10.0.0.6
[root@jenkins:~]#scp -r /usr/local/sonar-scanner 10.0.0.6:/usr/local/
[root@jenkins:~]#scp -r /usr/local/maven/ 10.0.0.6:/usr/local/
2.将所有用到的shell脚本拷贝到lb02
[root@lb02:~]#mkdir /server/scripts -p
[root@jenkins:~]#scp /server/scripts/* 10.0.0.6:/server/scripts/
3.jenkins服务端添加从节点lb02
-
把lb02的公钥上传至gitlab 拥有下载代码的权限
-
点击系统管理->节点管理->新建节点






/usr/libexec/git-core/git
/usr/local/maven/
/usr/local/sonar-scanner/

lb02和web服务器做免秘钥
[root@lb02:~]#ssh-copy-id 10.0.0.8
kylin系统需要卸载tmp
[root@jenkins ~]# umount -lf /tmp
[root@jenkins ~]# systemctl mask tmp.mount
Created symlink /etc/systemd/system/tmp.mount → /dev/null.
[root@jenkins ~]# systemctl restart jenkins.service

本文来自博客园,作者:丁志岩,转载请注明原文链接:https://www.cnblogs.com/dezyan/p/18888762


浙公网安备 33010602011771号