[TOC]
持续集成/持续交付-day060

一、服务器准备
服务器 虚拟机 纯干净的系统
| IP | 主机名 | 配置 |
|---|---|---|
| 10.0.0.200 | Gitlab | 2核4G 40G硬盘 Ubt |
| 10.0.0.201 | Jenkins | 1核2G 40G硬盘 kylin |
| 10.0.0.202 | Nexus | 1核2G 40G硬盘 kylin |
| 10.0.0.203 | Sonar | 1核2G 40G硬盘 kylin |
| 10.0.0.7 | Web | 1核1G 40G硬盘 kylin |
- 笔试题: 你了解DEVOPS吗?,谈谈对的理解。
二、git
01.企业常用的git版本系统
版本控制工具
1.SVN
2.git
页面版的代码仓库
1.gihub 全球使用最多的代码仓库
2.gitlab 全球使用最多的私有代码仓库
3.gitee 码云 中国使用最多的代码仓库
02.配置当前代码仓库的使用角色信息
服务器:10.0.0.200 ubt gitlab 自带git
1.版本查看
root@gitlab:~# git --version
git version 2.34.1
2.配置使用角色的信息
root@gitlab:~# git config --global user.name "liweiwei"
root@gitlab:~# git config --global user.email "liweiwei@mail.com"
root@gitlab:~# git config --global color.ui true
3.查看配置信息
root@gitlab:~# git config --list
user.name=liweiwei
user.email=liweiwei@mail.com
color.ui=true
配置文件在 .gitconfig
root@gitlab:~# cat .gitconfig
[user]
name = liweiwei
email = liweiwei@mail.com
[color]
ui = true
03.初始化仓库
1.创建一个练习仓库
root@gitlab:~# mkdir git_data/data -p
root@gitlab:~# cd git_data/data/
2.初始化仓库
root@gitlab:data# git init
root@gitlab:data# ll -a
total 12
drwxr-xr-x 3 root root 4096 Jun 10 13:25 ./
drwxr-xr-x 3 root root 4096 Jun 10 13:24 ../
drwxr-xr-x 7 root root 4096 Jun 10 13:25 .git/ # 隐藏的代码仓库
root@gitlab:data# ll .git/
total 32
drwxr-xr-x 2 root root 4096 Jun 10 13:25 branches/ # 分支
-rw-r--r-- 1 root root 92 Jun 10 13:25 config # 配置文件
-rw-r--r-- 1 root root 73 Jun 10 13:25 description # 描述
-rw-r--r-- 1 root root 23 Jun 10 13:25 HEAD # 头部
drwxr-xr-x 2 root root 4096 Jun 10 13:25 hooks/ # 勾子
drwxr-xr-x 2 root root 4096 Jun 10 13:25 info/ # 存放内部辅助配置
drwxr-xr-x 4 root root 4096 Jun 10 13:25 objects/ # 项目代码放此目录但是不是明文的HASH
drwxr-xr-x 4 root root 4096 Jun 10 13:25 refs/ # 分支、标签、远程库
-rw-r--r-- 1 root root 104 Jun 10 13:37 index # 缓存区 默认不存在
04.git区域名称

工作目录: 进入git_data目录 当前的位置称为工作目录 类
似车间工人
暂存区域: 临时存放代码的位置,类似质检车间 有问题可以返回,没有问题可以保存到仓库
本地仓库: 存储代码的位置,类似工厂仓库
开发流程:
开发在工作目录写代码--->提交到暂存区域---->提交到本地仓库--->代码才真正的被管理。每次走当前这个流程相当于虚拟机做了一个快照的动作
1.txt---->暂存区域--->本地仓库 1.txt放到仓库
05.git常用命令
1.git 初始化命令
git init
2.查看git仓库的状态
git status
root@gitlab:data# git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
# 将文件保存到本地仓库的流程
1.创建文件
root@gitlab:data# touch a.txt
2.提交到暂存区
root@gitlab:data# git add a.txt
3.将暂存区的内容提交到本地仓库
root@gitlab:data# git commit -m "newfile a.txt"
[master (root-commit) 70adb2a] newfile a.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
# 注意:提交完成后检查工作状态必须是完全干净的。
root@gitlab:data# git status
On branch master
nothing to commit, working tree clean
# 注意: 只要执行一次commit操作 就相当于做了一个快照
git删除文件使用git rm -f
root@gitlab:data# git rm -f a.txt
rm 'a.txt'
root@gitlab:data# git add .
root@gitlab:data# git commit -m "delete a.txt file"
[master 7fa257e] delete a.txt file
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 a.txt
如果不小心误删除了工作目录的文件可以恢复
git恢复工作目录的文件
root@gitlab:data# touch c.txt
root@gitlab:data# git add c.txt
root@gitlab:data# ll
total 0
-rw-r--r-- 1 root root 0 Jun 10 14:29 c.txt
root@gitlab:data# rm -f c.txt
root@gitlab:data# ll
total 0
root@gitlab:data# git restore c.txt #git restore 只对Git已追踪的文件生效
root@gitlab:data# ll
total 0
-rw-r--r-- 1 root root 0 Jun 10 14:29 c.txt
git修改文件
root@gitlab:data# echo aaaa >> b.txt
root@gitlab:data# git add .
root@gitlab:data# git commit -m "modifiled b.txt >>aaaa"
[master c3343b4] modifiled b.txt >>aaaa
2 files changed, 1 insertion(+)
create mode 100644 b.txt
create mode 100644 c.txt
查看历史提交日志
root@gitlab:data# git log --oneline
c3343b4 (HEAD -> master) modifiled b.txt >>aaaa
7fa257e delete a.txt file
70adb2a newfile a.txt
git比对
工作区和暂存区对比
root@gitlab:data# echo bbbb >> b.txt
root@gitlab:data# git diff b.txt
diff --git a/b.txt b/b.txt
index 5d308e1..64e09ee 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +1,2 @@
aaaa
+bbbb
暂存区和仓库对比
root@gitlab:data# git add .
root@gitlab:data# git diff
root@gitlab:data# git diff --cached
diff --git a/b.txt b/b.txt
index 5d308e1..64e09ee 100644
--- a/b.txt
+++ b/b.txt
@@ -1 +1,2 @@
aaaa
+bbbb
# git diff 比对的工作目录和暂存区的不同
# git diff --cached 比对的是暂存区和本地仓库的不同

版本回滚
root@gitlab:data# git log --oneline #查看版本
c3343b4 (HEAD -> master) modifiled b.txt >>aaaa
7fa257e delete a.txt file
70adb2a newfile a.txt
root@gitlab:data# cat b.txt
aaaa
bbbb
root@gitlab:data# git reset --hard 70adb2a
HEAD is now at 70adb2a newfile a.txt
root@gitlab:data# ll
total 0
-rw-r--r-- 1 root root 0 Jun 10 14:37 a.txt
版本返回到最新的位置
#reflog 查看所有的历史提交
root@gitlab:data# git reflog
70adb2a (HEAD -> master) HEAD@{0}: reset: moving to 70adb2a
c3343b4 HEAD@{1}: commit: modifiled b.txt >>aaaa
7fa257e HEAD@{2}: commit: delete a.txt file
70adb2a (HEAD -> master) HEAD@{3}: commit (initial): newfile a.txt
回到最新位置
root@gitlab:data# git reset --hard c3343b4
HEAD is now at c3343b4 modifiled b.txt >>aaaa
root@gitlab:data# ll
total 4
-rw-r--r-- 1 root root 5 Jun 10 14:41 b.txt
-rw-r--r-- 1 root root 0 Jun 10 14:41 c.txt
06.git分支


查看分支
root@gitlab:data# git branch
* master
创建分支
root@gitlab:data# git branch dev
查看当前所在
root@gitlab:data# git branch
dev
* master # *表示当前所在分支
切换到dev分支
root@gitlab:data# git checkout dev
Switched to branch 'dev'
root@gitlab:data# git branch
* dev
master
---------------
#测试分支合并
创建新文件c.txt
root@gitlab:data# touch c.txt
root@gitlab:data# git add .
root@gitlab:data# git commit -m "newfile c.txt"
[dev afd2430] newfile c.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 c.txt
切换到master分支
root@gitlab:data# git checkout master
Switched to branch 'master'
root@gitlab:data# ll
total 4
-rw-r--r-- 1 root root 5 Jun 10 14:41 b.txt
-rw-r--r-- 1 root root 0 Jun 10 14:54 c.txt
root@gitlab:data# rm -rf c.txt
root@gitlab:data# ll
total 4
-rw-r--r-- 1 root root 5 Jun 10 14:41 b.txt
root@gitlab:data# git branch
dev
* master
将dev中的代码合并到master分支
root@gitlab:data# git merge dev
Updating c3343b4..afd2430
Fast-forward
合并后删除dev分支
root@gitlab:data# git branch
dev
* master
root@gitlab:data# git branch -d dev
Deleted branch dev (was afd2430).
root@gitlab:data# git branch
* master
案例:冲突合并

`冲突合并流程测试`
1.修改master分支中的c.txt第二行为cccc
2.创建并切换dev分支,修改c.txt第二行为dddd
3.切换到master分支 merge 合并分支
4.手动编辑c.txt留下想要的代码,然后git add . git commit
环境准备
root@gitlab:data# touch c.txt
root@gitlab:data# rm -rf c.txt
root@gitlab:data# echo aaaa >> c.txt
root@gitlab:data# git add .
root@gitlab:data# git commit -m "newfile c.txt"
[master 6af1be7] newfile c.txt
2 files changed, 2 insertions(+)
root@gitlab:data# cat c.txt
aaaa
创建dev分支
root@gitlab:data# git branch dev
root@gitlab:data# git branch
dev
* master
修改c.txt
root@gitlab:data# echo bbbb >> c.txt
root@gitlab:data# git add .
root@gitlab:data# git commit -m "modifile c.txt bbbb"
[master fb97319] modifile c.txt bbbb
1 file changed, 1 insertion(+)
root@gitlab:data# cat c.txt
aaaa
bbbb
当前为止 master c.txt里面有aaaa和bbbb两行
当前为止 dev c.txt里面有aaaa
切换到dev分支操作
root@gitlab:data# git branch
* dev
master
root@gitlab:data# cat c.txt
aaaa
修改第二行为cccc
root@gitlab:data# cat c.txt
aaaa
root@gitlab:data# echo cccc >> c.txt
root@gitlab:data# git add .
root@gitlab:data# git commit -m "modifile c.txt cccc"
[dev b0f26e2] modifile c.txt cccc
1 file changed, 1 insertion(+)
当前为止 master c.txt 里面有 aaaa和bbbb两行
当前为止 dev c.txt 里面只有 aaaa和cccc两行
切换到master分支进行合并
root@gitlab:data# git checkout master
Switched to branch 'master'
root@gitlab:data# git branch
dev
* master
root@gitlab:data# cat c.txt
aaaa
bbbb
合并代码dev 提示代码冲突
root@gitlab:data# git merge dev
Auto-merging c.txt
CONFLICT (content): Merge conflict in c.txt
Automatic merge failed; fix conflicts and then commit the result.
root@gitlab:data# cat c.txt
aaaa
<<<<<<< HEAD
bbbb
=======
cccc
>>>>>>> dev
手动vim解决冲突合并 留下想要的代码
root@gitlab:data# cat c.txt
aaaa
bbbb
cccc
然后再进行提交动作
root@gitlab:data# git add .
root@gitlab:data# git commit -m "merge dev code"
[master f1b7419] merge dev code
07.git 标签
标签也是指向了一次commit提交,是一个里程碑式的标签,回滚打标签直接加标签号,不需要加唯一字符串不好记,标签即为版本号
root@gitlab:data# git log --oneline #一行显示commit历史记录
f1b7419 (HEAD -> master) merge dev code
b0f26e2 (dev) modifile c.txt cccc
fb97319 modifile c.txt bbbb
6af1be7 newfile c.txt
afd2430 newfile c.txt
9f597f5 del c.txt
c3343b4 modifiled b.txt >>aaaa
7fa257e delete a.txt file
70adb2a newfile a.txt
给最早的一个hahs值打tag
root@gitlab:data# git tag -a v1.0 70adb2a -m "v1.0稳定版本"
root@gitlab:data# git tag
v1.0
root@gitlab:data# git tag -a v1.1 7fa257e -m "v1.1"
root@gitlab:data# git tag -a v1.2 f1b7419 -m "v1.2"
root@gitlab:data# git tag
v1.0
v1.1
v1.2
查看tag的详细信息
root@gitlab:data# git show v1.0
tag v1.0
Tagger: liweiwei <liweiwei@mail.com>
Date: Thu Jun 11 00:25:15 2026 +0000
v1.0稳定版本
commit 70adb2a6c3e79685bc800bf88d76b2357aae8d54 (tag: v1.0)
Author: liweiwei <liweiwei@mail.com>
Date: Wed Jun 10 13:57:28 2026 +0000
newfile a.txt
diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..e69de29
通过tag版本回滚代码
root@gitlab:data# git reset --hard v1.0
HEAD is now at 70adb2a newfile a.txt
root@gitlab:data# ll
total 0
-rw-r--r-- 1 root root 0 Jun 11 01:37 a.txt
root@gitlab:data# git reset --hard v1.2
HEAD is now at f1b7419 merge dev code
root@gitlab:data# ll
total 8
-rw-r--r-- 1 root root 10 Jun 11 01:38 b.txt
-rw-r--r-- 1 root root 15 Jun 11 01:38 c.txt
删除标签
root@gitlab:data# git tag -d v1.1
Deleted tag 'v1.1' (was 2c50cab)
root@gitlab:data# git tag
v1.0
v1.2
08.git常用命令总结
| 命令 | 作用 |
|---|---|
git --version |
查看git版本 |
git config --global [配置参数] user.name/email |
配置用户/邮箱/ui |
git config --list |
查看配置列表 |
git init |
初始化仓库 |
git add . |
代码上传暂存区 |
git commit -m "描述" |
代码上传本地仓库 |
git rm |
删除文件 |
git restore |
撤销工作区 / 暂存区的修改 |
git diff |
比对文件和暂存区比 |
git diff --cached |
暂存区和本地仓库比 |
git log |
查看本地仓库 |
git log --online |
查看本地仓库-简洁 |
git reflog |
查看本地所有操作记录 |
git reset --hrad xxx |
代码回滚,修改指针指向 |
git tag -a 标签 hahs -m "描述" |
打版本标签 |
git show 标签 |
查看标签详情 |
git branch |
查看和创建/删除分支 |
git checkout |
分支切换 |
git merge |
分支合并 |
git remote |
查看远程仓库别名名称 -v 详情 |
git clone |
克隆远程仓库到本地 |
git push |
把本地已提交的代码,推送到远程仓库 |
git pull |
拉取远程最新代码,并自动合并到当前本地分支 |
三、gitlab
GitLab 是一个用于仓库管理系统的开源项目。使用Git作为代码管理工具,并在此基础上搭建起来的web服务。可通过Web界面进行访问公开的或者私人项目。它拥有与Github类似的功能,能够浏览源代码,管理缺陷和注释。
可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。团队成员可以利用内置的简单聊天程序(Wall)进行交流。它还提供一个代码片段收集功能可以轻松实现代码复用。
常用的网站:
官网:https://about.gitlab.com/
国内镜像:
https://mirrors.tuna.tsinghua.edu.cn/gitlab
‐ce/yum/
01.安装部署gitlab
安装环境:
1、 ubuntu22.04
2、 2c4G内存(实验)生产(至少6G)
3、 安装包:gitlab-ce_16.5.2-ce.0_amd64
4、 禁用防火墙,关闭selinux
安装方式:
在线安装:
本地deb包安装
1.在线安装
# yum仓库更新
sudo apt-get update
#安装仓库依赖
sudo apt-get install -y curl opensshserver ca-certificates tzdata perl
#通过curl到的bash脚本然后交给bash执行
curl -L get.gitlab.cn | bash
安装gitlab
root@ubuntu:~#
EXTERNAL_URL="http://10.0.0.200" apt-get
install -y gitlab-jh
2.deb包安装
1.上传deb包
2.安装
root@gitlab:~# dpkg -i gitlab-ce_16.5.2-
ce.0_amd64.deb
3.配置URL
root@gitlab:~# vim /etc/gitlab/gitlab.rb
...
external_url 'http://10.0.0.200'
....
4.执行配置命令
root@gitlab:~# gitlab-ctl reconfigure
5.密码位置
root@gitlab:~# cat
/etc/gitlab/initial_root_password
root@ubuntu:~# gitlab-ctl status # 查看gitlab状态
root@ubuntu:~# gitlab-ctl stop # 停止gitlab服务
root@ubuntu:~# gitlab-ctl start # 启动gitlab服务
安装完成后访问10.0.0.200
默认用户: root
临时密码的问题
cat /etc/gitlab/initial_root_password
02.环境优化
1.修改语言为中文
设置-》偏好设置(preferences)->改为中文
2.修改密码
设置-》密码
03.gitlab使用-创建新的项目

第一种方式: 空的代码仓库,在gitlab创建代码仓库,然后
拉取到本地服务器
第二种方式: 已经存在的代码仓库,需要配置远程仓库,然
后将本地仓库中的代码推送到远程服务器
1.第一种方式
1.新建群组
2.创建仓库
3.打通系统和gitlabroot账号的SSH免秘钥
1)ssh-keygen
2)将公钥复制到gitlab的root账号下
4.命令行将空的仓库下载到本地
git clone 克隆地址
root@gitlab:git_data# git clone git@10.0.0.200:oldboy/game.git
root@gitlab:git_data# ll
total 8
drwxr-xr-x 3 root root 4096 Jun 11 01:38 data/
drwxr-xr-x 3 root root 4096 Jun 11 06:34 game/
5.创建新的文件提交到本地仓库
root@gitlab:git_data# cd game/
root@gitlab:game# touch game.txt
root@gitlab:game# git add .
root@gitlab:game# git commit -m "newfile game.txt"
[master (root-commit) a4a1c16] newfile game.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 game.txt
5.查看远程仓库的别名名称
root@gitlab:game# git remote
origin
root@gitlab:game# git remote -v
origin git@10.0.0.200:oldboy/game.git (fetch)
origin git@10.0.0.200:oldboy/game.git (push)
6.将本地仓库的代码提交到远程仓库
root@gitlab:game# git branch
* master
root@gitlab:game# git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 208 bytes | 208.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To 10.0.0.200:oldboy/game.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

2.第二种方式
代码已经存在本地
1.本地仓库已经初始化
2.本地代码已经存在
# 环境准备
root@gitlab:git_data# mkdir test
root@gitlab:git_data# cd test/
root@gitlab:test# git init
root@gitlab:test# touch {a..c}.txt
root@gitlab:test# ll
total 0
-rw-r--r-- 1 root root 0 Jun 11 06:40 a.txt
-rw-r--r-- 1 root root 0 Jun 11 06:40 b.txt
-rw-r--r-- 1 root root 0 Jun 11 06:40 c.txt
root@gitlab:test# git add .
root@gitlab:test# git commit -m "newifile *.txt"
[master (root-commit) c0aaf08] newifile *.txt
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
create mode 100644 b.txt
create mode 100644 c.txt
#将本地仓库的代码提交到远程仓库
1.在gitlab页面长创建仓库
2.本地仓库配置远程仓库的地址
查看默认的远程仓库
root@gitlab:test# git remote
root@gitlab:test# git remote -v
配置远程仓库名称为origin
root@gitlab:test# git remote add origin git@10.0.0.200:oldboy/test.git
root@gitlab:test# git remote -v
origin git@10.0.0.200:oldboy/test.git (fetch)
origin git@10.0.0.200:oldboy/test.git (push)
3.推送代码到远程test仓库
root@gitlab:test# git branch -m master main # 重命名分支
root@gitlab:test# git branch
* main
root@gitlab:test# git push -u origin main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 214 bytes | 214.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To 10.0.0.200:oldboy/test.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

案例:推送game游戏代码到远程game项目中
#环境清理
root@gitlab:game# git rm -f game.txt
rm 'game.txt'
root@gitlab:game# git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: game.txt
root@gitlab:game# git commit -m "delete game.txt"
[master d0476b4] delete game.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 game.txt
--------------------------------
#上传代码解压代码提交本地仓库
root@gitlab:game# ll
total 7720
-rw-r--r-- 1 root root 7902976 Sep 27 2024 xbw.zip
root@gitlab:game# unzip xbw.zip
root@gitlab:game# ll
total 7780
-rw-r--r-- 1 root root 28032 May 24 2021 bgm.mp3
drwxr-xr-x 2 root root 4096 May 24 2021 css/
drwxr-xr-x 2 root root 4096 May 24 2021 images/
-rw-r--r-- 1 root root 8956 May 24 2021 index.html
drwxr-xr-x 2 root root 4096 May 24 2021 js/
drwxr-xr-x 2 root root 4096 May 24 2021 roms/
-rw-r--r-- 1 root root 811 May 24 2021 shuoming.html
-rw-r--r-- 1 root root 7902976 Sep 27 2024 xbw.zip
root@gitlab:game# rm -rf xbw.zip
root@gitlab:game# git add .
root@gitlab:game# git commit -m "xbw code"
将本地仓库的游戏代码提交到远程仓库
root@gitlab:game# git push -u origin master
创建普通用户dev
1.管理中心-》仪表盘-》新建用户-》创建成功-》编辑-》设置密码
2.打开浏览器无痕浏览窗口-》使用普通账号登录gitlab 默认里面空的
3.回到root账号页面-》将dev账号添加到oldboy组中,dev用户拥有game项目的权限。
开发下载代码到本地服务器
dev开发使用的服务器10.0.0.6
1.开发需要再自己的Linux系统中生成秘钥对
[root@dev ~]# ssh-keygen
2.将公钥复制到gitlab dev账号中
[root@dev ~]# cat .ssh/id_rsa.pub #复制秘钥到gitlab中
3.下载游戏代码到本地
[root@dev ~]# git clone git@10.0.0.200:oldboy/game.git
[root@dev game]# ll
总用量 48
-rw-r--r-- 1 root root 28032 6月 11 15:09 bgm.mp3
drwxr-xr-x 2 root root 23 6月 11 15:09 css
drwxr-xr-x 2 root root 23 6月 11 15:09 images
-rw-r--r-- 1 root root 8956 6月 11 15:09 index.html
drwxr-xr-x 2 root root 213 6月 11 15:09 js
drwxr-xr-x 2 root root 4096 6月 11 15:09 roms
-rw-r--r-- 1 root root 811 6月 11 15:09 shuoming.html
开发上传代码的流程
开发使用的主机10.0.0.6
配置使用人的信息
[root@dev game]# git config --global user.name "dev"
[root@dev game]# git config --global user.email "dev@mail.com"
[root@dev game]# git config --list
1.写代码
[root@dev game]# touch dev.txt
[root@dev game]# git add .
[root@dev game]# git commit -m "newfile dev.txxt"
[master d45a389] newfile dev.txxt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 dev.txt
2.提交到远程仓库
[root@dev game]# git push -u origin master
枚举对象: 4, 完成.
对象计数中: 100% (4/4), 完成.
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 267 字节 | 267.00 KiB/s, 完成.
总共 3(差异 1),复用 1(差异 0),包复用 0
remote: GitLab: You are not allowed to push code to protected branches on this project.
To 10.0.0.200:oldboy/game.git
! [remote rejected] master -> master (pre-receive hook declined)
error: 推送一些引用到 '10.0.0.200:oldboy/game.git' 失败
原因:非管理员默认不允许提交到master主干分支,需要先创建子分支,然后提交子分支,在发起代码合并请求master
[root@dev game]# git branch dev
[root@dev game]# git push -u origin dev
枚举对象: 4, 完成.
对象计数中: 100% (4/4), 完成.
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (3/3), 267 字节 | 267.00 KiB/s, 完成.
总共 3(差异 1),复用 1(差异 0),包复用 0
remote:
remote: To create a merge request for dev, visit:
remote: http://10.0.0.200/oldboy/game/-/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
To 10.0.0.200:oldboy/game.git
* [new branch] dev -> dev
分支 'dev' 设置为跟踪来自 'origin' 的远程分支 'dev'。
3.发起代码合并请求
1)进入dev账号gitlab项目页面-》点击创建合并请求
2)到root账号合并代码
04.gitlab备份
1.配置备份目录
对gitlab进行备份将会创建一个包含所有库和附件的归档文件。对备份的恢复只能恢复到与备份时的gitlab相同的版本。将gitlab迁移到另一台服务器上的最佳方法就是通过备份和还原。
gitlab提供了一个简单的命令行来备份整个gitlab,并且能灵活的满足需求。
备份文件将保存在配置文件中定义的backup_path中,文件名为TIMESTAMP_gitlab_backup.tar,TIMESTAMP为备份时的时间戳。TIMESTAMP的格式为:EPOCH_YYYY_MM_DD_Gitlab-version。
默认备份文件存放路径:/var/opt/gitlab/backups
---
`自定义备份目录`
如果自定义备份目录需要赋予git权限
1、修改配置文件
在配置文件/etc/gitlab/gitlab.rb中加入
gitlab_rails['backup_path'] = "/data/backup/gitlab" #备份文件存放目录
gitlab_rails['backup_keep_time'] = 604800 #备份保留时间(以秒为单位,这个是七天默认值)
2、创建备份目录,并赋予git权限
root@gitlab:~# mkdir -p /data/backup/gitlab/
root@gitlab:~# chown -R git.git /data/backup/gitlab/
3、让配置生效
root@gitlab:~# gitlab-ctl reconfigure
2.备份
`手动备份`
1、执行gitlab-rake gitlab:backup:create生成一次备份。
root@gitlab:~# gitlab-rake gitlab:backup:create
`定时备份`
在定时任务添加:
root@gitlab:~# vim /etc/crontab
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
环境变量CRON=1的作用是如果没有任何错误发生时, 抑制备份脚本的所有进度输出。
3.恢复
简单测试:备份好后删除一个项目在执行恢复过程
验证结果:删除的项目恢复 恢复成功
----------------------------------
只能还原到与备份文件相同的gitlab版本。
执行恢复操作时,需要gitlab处于运行状态,备份文件位于gitlab_rails['backup_path']。
root@gitlab:~# ll /data/backup/gitlab/
total 8092
-rw------- 1 git git 8284160 Jun 14 04:24 1781411046_2026_06_14_16.5.2_gitlab_backup.tar
1、停止连接到数据库的进程(也就是停止数据写入服务),但是保持GitLab是运行的。
root@gitlab:~# gitlab-ctl stop unicorn
root@gitlab:~# gitlab-ctl stop sidekiq
2、接下我们进行恢复,指定时间戳你要从那个备份恢复:
root@gitlab:~# gitlab-rake gitlab:backup:restore BACKUP=1781411046_2026_06_14_16.5.2
过程中有交互回答yes就行
将移除我们自建的表。回答yes
将移除所有的认证Key。回答yes
3、完成后重启GitLab服务
root@gitlab:~# gitlab-ctl restart
4、检查GitLab的服务
root@gitlab:~# gitlab-rake gitlab:check SANITIZE=true
4、注意事项
GitLab 自带备份脚本不会自动备份 gitlab.rb(配置文件)和 gitlab-secrets.json(密钥文件),恢复集群 / 实例时需要手动拷贝这两个文件
1.找到这两个文件路径
默认路径(Omnibus 安装版):
/etc/gitlab/gitlab.rb
/etc/gitlab/gitlab-secrets.json
2.手动备份(推荐每次数据库备份后执行)
# 打包备份到当前目录,可自行修改存放路径
cd /etc/gitlab/ && tar zcvf ~/gitlab-config-$(date +%Y%m%d).tar.gz gitlab.rb gitlab-secrets.json
3.恢复步骤(新服务器 / 故障恢复时)
1)停止 GitLab
gitlab-ctl stop
2)解压拷贝配置文件到对应目录
tar zxvf gitlab-config-xxx.tar.gz -C /etc/gitlab/
3)重载配置并启动
gitlab-ctl reconfigure
gitlab-ctl start
补充注意
- 两个文件绝对不能泄露,包含数据库密码、会话密钥、加密密钥等敏感信息。
- 日常备份流程:GitLab 内置备份 + 单独备份这两个配置文件,缺一不可。
四、Jenkins-day061
官网 Jenkins.io
Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。
01.安装Jenkins
服务配置
Kylin系统 10.0.0.201 1核2G内存
1.安装JDK运行环境
[root@jenkins ~]# yum -y install java
2.使用rpm安装Jenkins
1)上传rpm包
[root@jenkins ~]# ll jenkins-2.405-1.1.noarch.rpm
-rw-r--r-- 1 root root 93405530 9月 27 2024 jenkins-2.405-1.1.noarch.rpm
2)安装
[root@jenkins ~]# rpm -ivh jenkins-2.405-1.1.noarch.rpm
3.启动Jenkins
[root@jenkins ~]# systemctl start jenkins
4.修改启动用户为root默认使用Jenkins运行
[root@jenkins ~]# grep "root" /usr/lib/systemd/system/jenkins.service
User=root
Group=root
5.重新加载配置文件
[root@jenkins ~]# systemctl daemon-reload
6.修改配置文件中的启动用户
[root@jenkins ~]# grep root /etc/sysconfig/jenkins
JENKINS_USER="root"
7.配置Jenkins的插件
1)将jenkins_plu.tar.gz插件压缩包上传
2)移动到/var/lib/jenkins/plugins/
mv jenkins_plu.tar.gz /var/lib/jenkins/plugins/
[root@jenkins plugins]# ll
总用量 306796
-rw-r--r-- 1 root root 314156543 9月 27 2024 jenkins_plu.tar.gz
3)解压
tar xf jenkins_plu.tar.gz
8.重启Jenkins生效
[root@jenkins ~]# systemctl restart jenkins
页面安装
打开浏览器访问10.0.0.201:8080

查看密码
[root@jenkins ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
1802af9b5e684b4abc70f90e5fc87fa9
插件已经导入,这里选择跳过

点击开始使用

修改Jenkins的登录密码
oldboy123.com

02.jenkins使用
1.手动将gitlab的代码运行到web服务器
服务器:kylin web02 10.0.0.8 nginx game小霸王游戏
1.配置server
[root@web02 conf.d]# cat game.conf
server {
listen 80;
server_name www.xbw.com;
location / {
root /code/game;
index index.html;
}
}
[root@web02 conf.d]# nginx -t
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
[root@web02 conf.d]#
2.克隆代码到code下
[root@web02 conf.d]# mkdir -p /code/
[root@web02 conf.d]# cd /code/
克隆前做好免密钥
[root@web02 code]# ssh-keygen
[root@web02 code]# cat ~/.ssh/id_rsa.pub
[root@web02 code]# git clone git@10.0.0.200:oldboy/game.git
[root@web02 code]# ll game/
总用量 48
-rw-r--r-- 1 root root 28032 6月 11 16:14 bgm.mp3
drwxr-xr-x 2 root root 23 6月 11 16:14 css
-rw-r--r-- 1 root root 0 6月 11 16:14 dev.txt
drwxr-xr-x 2 root root 23 6月 11 16:14 images
-rw-r--r-- 1 root root 8956 6月 11 16:14 index.html
drwxr-xr-x 2 root root 213 6月 11 16:14 js
drwxr-xr-x 2 root root 4096 6月 11 16:14 roms
-rw-r--r-- 1 root root 811 6月 11 16:14 shuoming.html
3.配置hosts解析
10.0.0.8 www.xbw.com
4.访问测试
www.xbw.com
2.Jenkins默认执行的shell路径
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json #国内插件源
构建一个自由风格的项目

执行pwd命令查看默认的路径

执行查看

3.Jenkins拉取gitlab的代码
1.jenkins生成秘钥对
[root@jenkins ~]# ssh-keygen
[root@jenkins ~]# cat .ssh/id_rsa.pub
2.将公钥复制到gitlab的root账号中
配置Jenkins拉取game

手动拉取解决yes问题
[root@jenkins ~]# cd /var/lib/jenkins/workspace/game/
[root@jenkins game]# git clone git@10.0.0.200:oldboy/game.git
[root@jenkins game]# rm -rf game/

完成后执行构建
查看拉取后的代码
[root@jenkins ~]# cd /var/lib/jenkins/workspace/game/
[root@jenkins game]# ll
总用量 48
-rw-r--r-- 1 root root 28032 6月 11 16:35 bgm.mp3
drwxr-xr-x 2 root root 23 6月 11 16:35 css
-rw-r--r-- 1 root root 0 6月 11 16:35 dev.txt
drwxr-xr-x 2 root root 23 6月 11 16:35 images
-rw-r--r-- 1 root root 8956 6月 11 16:35 index.html
drwxr-xr-x 2 root root 213 6月 11 16:35 js
drwxr-xr-x 2 root root 4096 6月 11 16:35 roms
-rw-r--r-- 1 root root 811 6月 11 16:35 shuoming.html
4.将代码推送到web服务器
做免秘钥
jenkins推送公钥到web服务器
[root@jenkins game]# ssh-copy-id 10.0.0.8
Jenkins配置推送

构建测试
1.删除web01game下的代码
[root@web02 code]# rm -rf game/*
[root@web02 code]# ll game/
总用量 0
2.点击构建
3.看代码是否推送
[root@web02 code]# ll game/
总用量 48
-rw-r--r-- 1 root root 28032 6月 11 16:50 bgm.mp3
drwxr-xr-x 2 root root 23 6月 11 16:50 css
-rw-r--r-- 1 root root 0 6月 11 16:50 dev.txt
drwxr-xr-x 2 root root 23 6月 11 16:50 images
-rw-r--r-- 1 root root 8956 6月 11 16:50 index.html
drwxr-xr-x 2 root root 213 6月 11 16:50 js
drwxr-xr-x 2 root root 4096 6月 11 16:50 roms
-rw-r--r-- 1 root root 811 6月 11 16:50 shuoming.html
5.配置自动触发webhook勾子
触发方式

token
高级里面

gitlab页面配置勾子
1.网络

2.配置webhook

6.测试
1.gitlab服务器上修改代码并推送到远程仓库
root@gitlab:game# vim index.html
魂斗罗改为魂斗斗
root@gitlab:game# git commit -am "魂斗斗"
[master 53d6721] 魂斗斗
1 file changed, 2 insertions(+), 2 deletions(-)
root@gitlab:game# git pull --rebase
root@gitlab:game# git pull -u origin master
2.查看验证
7.代码回滚软链接方式
1.配置Jenkins
ssh 10.0.0.8 "mkdir -p /code/web_bak/game_${BUILD_ID}"
scp -r ./* 10.0.0.8:/code/web_bak/game_${BUILD_ID}
ssh 10.0.0.8 "rm -rf /code/game && ln -s /code/web_bak/game_${BUILD_ID} /code/game"
注意:加/和不加/的区别
ssh 10.0.0.8 "rm -rf /code/game && ln -s /code/web_bak/game_${BUILD_ID} /code/game" #正确写法
ssh 10.0.0.8 "rm -rf /code/game/ && ln -s /code/web_bak/game_${BUILD_ID} /code/game" #错误写法会在/code/game/里面创建软连接
ssh 10.0.0.8 "rm -rf /code/game && ln -s /code/web_bak/game_${BUILD_ID} /code/game/" #错误写法,无法找到目录没有这个目录
注意:软链接使用绝对路径,相对路径可能会断链

2.测试
1.修改代码上传触发勾子
root@gitlab:game# vim index.html
魂斗斗改为魂斗罗罗
root@gitlab:game# git commit -am "魂斗罗罗"
root@gitlab:game# git push -u origin master
五、SonarQube代码扫描
jenkins将代码拉取到jenkins本地,先将代码推送到sonar服务器上代码扫描检测,检测漏洞 逻辑 坏味道。

01.安装部署-服务端
服务器:kylin 10.0.0.203
1.安装java
yum -y install java
2.安装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
...
`安装数据库`
1.下载安装
[root@sonar ~]# yum -y install mariadb-server
2.启动
[root@sonar ~]# service mysqld start
Starting mysqld (via systemctl): [ OK ]
3.设置密码
[root@sonar ~]# mysqladmin -uroot password lizhenya123
4.创建sonar库
[root@sonar ~]# mysql -uroot -plizhenya123 -e "CREATE DATABASE sonar DEFAULT CHARACTER SET utf8;"
[root@sonar ~]# mysql -uroot -plizhenya123 -e "show databases;"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sonar |
+--------------------+
3.安装sonarqube
1.上传代码解压
[root@sonar ~]# ll sonarqube-7.0.zip
-rw-r--r-- 1 root root 155709573 9月 27 2024 sonarqube-7.0.zip
[root@sonar ~]# unzip sonarqube-7.0.zip -d /usr/local/
2.做软连接
[root@sonar ~]# ln -s /usr/local/sonarqube-7.0/ /usr/local/sonarqube
3.修改连接数据库信息
cd /usr/local/sonarqube/conf
修改配置文件的16 17和26行
[root@sonar conf]# grep -E 'root|lizhenya|3306' sonar.properties
sonar.jdbc.username=root
sonar.jdbc.password=lizhenya123
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
# The default value is root context (empty value).
4.创建普通用户sonar
sonarqube服务必须由普通用户运行
[root@sonar ~]# useradd sonar
[root@sonar ~]# chown -R sonar.sonar /usr/local/sonarqube-7.0/
5.使用sonar用户运行服务
[root@sonar ~]# su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start"
Starting SonarQube...
Started SonarQube.
6.访问测试
10.0.0.203:9000

7.插件安装
# 进入插件目录删除默认插件
[root@sonar ~]# cd /usr/local/sonarqube/extensions/plugins/
[root@sonar plugins]# rm -rf *
[root@sonar plugins]# ll
总用量 0
# 上传插件
[root@sonar plugins]# ll
总用量 44052
-rw-r--r-- 1 root root 45106788 9月 27 2024 sonar_plugins.tar.gz
# 解压插件
[root@sonar plugins]# tar xf sonar_plugins.tar.gz
[root@sonar plugins]# mv plugins/* .
8.重启sonarqube服务
[root@sonar plugins]# su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart"
Stopping SonarQube...
Waiting for SonarQube to exit...
Stopped SonarQube.
Starting SonarQube...
Started SonarQube.
9.访问登录
10.0.0.203:9000
用户名和密码admin

02.sonarqube使用
1.页面创建项目
sonar-scanner
-Dsonar.projectKey=html
-Dsonar.sources=.
-Dsonar.host.url=http://10.0.0.203:9000
-Dsonar.login=7a36363b865026680bbd2130b12e074c35a35a2a

2.客户端执行代码扫描上传到sonar服务器
服务器:kylin10.0.0.201 Jenkins
安装客户端
1.上传客户端代码
[root@jenkins ~]# ll sonar-scanner-cli-4.2.0.1873-linux.zip
-rw-r--r-- 1 root root 42397119 9月 27 2024 sonar-scanner-cli-4.2.0.1873-linux.zip
2.解压代码
[root@jenkins ~]# unzip sonar-scanner-cli-4.2.0.1873-linux.zip -d /usr/local/
[root@jenkins usr]# ln -s /usr/local/sonar-scanner-4.2.0.1873-linux/ /usr/local/sonar-scanner
3.将客户端命令写入PATH变量
[root@jenkins usr]# tail -1 /etc/profile
export PATH="$PATH:/usr/local/sonar-scanner/bin/"
[root@jenkins ~]# source /etc/profile
4.执行代码扫描
[root@jenkins jenkins]# cd /var/lib/jenkins/workspace/game/
sonar-scanner
-Dsonar.projectKey=html
-Dsonar.sources=.
-Dsonar.host.url=http://10.0.0.203:9000
-Dsonar.login=7a36363b865026680bbd2130b12e074c35a35a2a
执行成功
INFO: Task total time: 15.411 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 19.850s
INFO: Final Memory: 14M/139M
INFO: ------------------------------------------------------------------------

03.配置Jenkins集成soanrqube-day062
1.服务端配置

默认Jenkins页面找不到服务端和客户端,需要手动配置
1.配置服务器信息 系统管理—>系统配置

配置token

2.配置客户端信息位置
系统配置—->全局工具

修改sonar客户端指向10.0.0.203
[root@jenkins conf]# pwd
/usr/local/sonar-scanner/conf
[root@jenkins conf]# grep 'sonar' sonar-scanner.properties
sonar.host.url=http://10.0.0.203:9000
sonar.login=7a36363b865026680bbd2130b12e074c35a35a2a
sonar.sourceEncoding=UTF-8
3.配置Build Steps
sonar.projectName=${JOB_NAME} # 项目在sonarqube上的显示名称
sonar.projectKey=html # 项目的唯一表示,不能重复
sonar.sources=. # 扫描那个项目的源码

4.测试使用
1.修改代码push触发jenkins
root@gitlab:game# git commit -am "魂斗斗"
root@gitlab:game# git push -u origin master
2.查看结果
[root@jenkins conf]# date
2026年 06月 12日 星期五 15:12:13 CST

六、配置企业微信
01.Jenkins配置
1.由于jenkins没有官网的插件来完成此功能,所以我们只能用网络上一些开源的插件(线下班不需要以下步骤)
github下载代码
https://github.com/daniel-beck/changelog-environment-plugin
解压到某个目录-》进入目录执行以下操作
cd 到 changelog-environment-plugin-master下,执行
mvn verify
时间较长,会在changelog-environment-plugin-master/target/下有个changelog-master/target/下有个changelog-environment.hpi文件,上传到jenkins即可使用
-----------------------------
`从这一步开始`
2.配置Jenkins
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 就是时间格式。

02.企业微信和py脚本
1.注册企业微信
# 企业微信在学zabbix时已经注册过
2.py脚本
1.上传py脚本
[root@jenkins ~]# mkdir /server/scripts/ -p
[root@jenkins scripts]# ll
总用量 4
-rw-r--r-- 1 root root 2008 9月 27 2024 jenkins_notify.py
2.修改信息
data = {
"touser" : "LiWeiWei", # 账号名称
"msgtype" : "text",
"agentid" : 1000002, # 账号id
if __name__ == '__main__':
Corpid = "ww21ebcca6dea1ed10" # 企业id
Secret = "HQ44hRDQX70B1WaoFj-jVxn3zccHt0mLE8xmHAmCz3E" # secret
3.命令行测试
[root@jenkins scripts]# yum -y install python2
[root@jenkins scripts]# yum -y install python2-pip
[root@jenkins scripts]# pip2.7 install requests
命令测试
[root@jenkins scripts]# python2.7 jenkins_notify.py test /etc/hosts game
[变更日志] : 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
[变更日志] : 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{"errcode":0,"errmsg":"ok","msgid":"SvpFVDN5j3cfoDnUMZjZA15aV795T4Zb9gBAXa70l71Ikuo6sIijMYDKa_BB69vaOSvSFXGu6DGxMurynYoayPVP78KxjCo3nKijGI4ziaBJzYuPKyVPEdRm4N9pmg3-"}

03.集成到jenkins
# 注意传了三个参数
echo "==========StartNotify=============="
echo ${SCM_CHANGELOG} > /tmp/${JOB_NAME}_change.log
python /server/scripts/jenkins_notify.py ${BUILD_URL} /tmp/${JOB_NAME}_change.log ${JOB_NAME}
rm ‐f /tmp/${JOB_NAME}_change.log

04.测试
1.push代码触发jenkins
root@gitlab:game# git commit -am "将魂斗斗修改为魂斗罗"
[master 42be1fc] 将魂斗斗修改为魂斗罗
1 file changed, 1 insertion(+), 1 deletion(-)
root@gitlab:game# git push -u origin master
2.查看结果

七、线上发布流程

01.配置Jenkins获取gitlab中所有的版本号
创建一个新项目game_deploy作为线上发布流程
服务器用web01 10.0.0.7来做线上服务器
1.配置参数化
git_version

2.将当前的稳定版本打tag
root@gitlab:game# git tag -a v1.1 -m "v1.1稳定版本"
root@gitlab:game# git push -u origin v1.1
root@gitlab:game# vim index.html
['魂斗罗v1.2', 'roms/Contra1(U)30.nes'],
root@gitlab:game# git commit -am "v1.2"
root@gitlab:game# git tag -a v1.2 -m "v1.2稳定版本"
root@gitlab:game# git push -u origin v1.2

02.web01配置
1.配置nginx server
[root@shell conf.d]# cat game.conf
server {
listen 80;
server_name www.xbw.com;
location / {
root /code/game;
index index.html;
}
}
[root@shell conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@shell conf.d]# systemctl restart nginx
2.jenkins和web01做免秘钥
[root@jenkins ~]# ssh-copy-id 10.0.0.7
03.配置代码回滚
1.页面配置
拉取分支设置为变量

设置两个参数控制发版还是回滚

Build Steps

04.写脚本
[root@jenkins scripts]# cat deploy.sh
#!/bin/bash
IP=10.0.0.7
TAR(){
tar zcf /opt/web.tar.gz ./*
}
SCP(){
scp /opt/web.tar.gz $IP:/code/
}
XF(){
ssh $IP "mkdir -p /code/web_${git_version}"
ssh $IP "tar xf /code/web.tar.gz -C /code/web_${git_version}"
ssh $IP "rm -f /code/web.tar.gz"
}
LN(){
ssh $IP "[ -d /code/game ] && rm -rf /code/game"
ssh $IP "ln -s /code/web_${git_version} /code/game"
}
main(){
TAR
SCP
XF
LN
}
if [ ${deploy_env} = deploy ];then
main
elif [ ${deploy_env} = rollback ];then
LN
fi
05.构建测试
1.回Jenkins构建
2.hosts解析到web01
10.0.0.7 www.xbw.com
3.访问查看结果
www.xbw.com
06.避免重复构建-优化
方法1:使用jenkins变量
当重复执行构建后会生成多个相同版本的文件,利用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 版本号已部署"
exit
else
main
fi
elif [ ${deploy_env} = rollback ];then
LN
方法2:判断目标主机版本号是否存在
TE=`ssh 10.0.0.7 "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
LN
fi
07-jenkins自动打标签
新建一个项目game_tag
1.获取gitlab所有标签
2.拉取master的代码到当前项目中
3.执行shell命令
git tag -a ${tag_version} -m "${tag_version}"
git push -u origin ${tag_version}
1.获取gitlab所有标签jenkins配置

2.配置${tag_version}变量

3.shell命令配置

4.配置用户信息
[root@jenkins ~]# git config --global user.email "ops@example.com"
[root@jenkins ~]# git config --global user.name "ops"
5.构建测试

八、流水线pipeline-day063
01.流水线pipeline介绍
Jenkins Pipeline 是基于代码的持续交付流水线,用 Jenkinsfile 定义流程,分声明式(Declarative) 和脚本式(Scripted),日常优先用声明式(语法严谨、易维护、推荐官方标准)。
两种语法区别
| 类型 | 特点 | 适用场景 |
|---|---|---|
| 声明式 Declarative | 固定结构、语法严格、易读、容错高 | 绝大多数项目,首选 |
| 脚本式 Scripted | 原生 Groovy、灵活、自由度高 | 复杂逻辑、自定义脚本场景 |
02.声明式Pipline核心结构(标准模版)
1.最简可运行jenkinsfile
pipeline {
// 全局配置
agent any // 使用任意可用节点执行
environment { // 全局环境变量
NAME = "test"
}
// 流水线阶段
stages {
stage('拉取代码') {
steps {
git url: 'https://gitee.com/xxx/xxx.git', branch: 'main'
}
}
stage('编译构建') {
steps {
sh 'mvn clean package -Dmaven.test.skip=true'
}
}
stage('部署') {
steps {
sh 'echo 开始部署应用'
}
}
}
// 后置动作:无论成功/失败都会执行
post {
success {
echo "流水线执行成功"
}
failure {
echo "流水线执行失败"
}
always {
echo "流水线执行结束"
}
}
}
2.关键字段详解
- agent
- agent any:任意节点运行
- agent { label ‘node-name’ }:指定特定节点
- agent docker ‘maven:3.8-openjdk-8’:使用容器运行
- stages / stage / steps
- stages:所有阶段集合
- stage:单个阶段(名称自定义)
- steps:当前阶段执行的具体步骤
- 常用步骤指令
- sh ‘命令’:执行Linux shell命令
- bat ‘命令’:执行Windows cmd命令
- git:拉取代码
- echo:打印日志
- input:人工确认卡点
- post后置处理
- always:无论成败都执行
- success:仅成功执行
- failure:仅失败执行
- aborted:任务被手动终止时执行
03.常用进阶用法
1.环境变量(局部/全局)
pipeline {
agent any
// 全局变量
environment {
VERSION = "1.0.0"
// 引用 Jenkins 系统凭据(密码/密钥)
PWD = credentials('pwd-id')
}
stages {
stage('测试变量') {
// 阶段局部变量
environment {
TMP = "local"
}
steps {
sh 'echo 版本: ${VERSION}'
}
}
}
}
2.并行执行阶段(并行构建/测试)
pipeline {
agent any
stages {
stage('并行任务') {
parallel {
stage('任务A') { steps { sh 'sleep 3 && echo 任务A完成' } }
stage('任务B') { steps { sh 'sleep 2 && echo 任务B完成' } }
}
}
}
}
3.条件判断(when)
满足条件才执行当前stage
stage('仅分支为 dev 时执行') {
when {
branch 'dev'
}
steps {
sh 'echo 只在 dev 分支运行'
}
}
4.缓存依赖(加速构建)
搭配 Pipeline Cache 插件缓存 maven/npm 依赖:
options {
buildDiscarder(logRotator(numToKeepStr: '10')) // 保留10条构建记录
}
stages {
stage('构建') {
steps {
cache(path: '~/.m2/repository', key: "maven-${checksum 'pom.xml'}") {
sh 'mvn package'
}
}
}
}
5.超时、重试
steps {
timeout(time: 5, unit: 'MINUTES') { // 5分钟超时
retry(2) { // 失败重试2次
sh 'sh deploy.sh'
}
}
}
04.jenkins配置流水线两种方式
方式1:执行在任务内编写(临时测试)
- 新建/进入流水线任务
- 找到 Pipeline 定义 → 选择 Pipeline script
- 粘贴 Jenkinsfile 代码 → 保存构建
方式2:代码仓库托管(生产推荐)
- Pipeline 定义选择 Pipeline script from SCM
- 选择 Git,填写仓库地址、分支
- Script Path:填写 Jenkinsfile(默认仓库根目录)
- 保存,每次代码提交自动触发流水线
05.Pipeline案例-方式1
1.创建一个流水线项目

2.配置流水线

示例脚本
pipeline{
agent any
stages{
stage("get code"){
steps{
echo "get code from scm"
}
}
stage("package"){
steps{
echo "packge code"
}
}
stage("deploy"){
steps{
echo "deploy packge to node1"
}
}
}
}
3.build now构建测试

06.Pipeline案例-方式2
1. Pipeline 定义选择 **Pipeline script from SCM**
2. 选择 Git,填写仓库地址、分支
3. Script Path:填写 Jenkinsfile(默认仓库根目录)
4. 保存,每次代码提交自动触发流水线
5. 编写Jenkinsfile上传到gitlab
1.定义选择 Pipeline script from SCM,选择 Git,填写仓库地址、分支

2.Script Path:填写 Jenkinsfile(默认仓库根目录)

3.编写Jenkinsfile上传到gitlab,或在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 --exclude=.git --exclude=jenkinsfile ./*'
}
}
stage("deploy"){
steps{
sh 'ssh 10.0.0.8 "cd /code/web_bak/ && mkdir web-${BUILD_ID}"'
sh 'scp /opt/web-${BUILD_ID}.tar.gz 10.0.0.8:/code/web_bak/web-${BUILD_ID}/'
sh 'ssh 10.0.0.8 "cd /code/web_bak/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 /code/web_bak/web-${BUILD_ID} /code/game"'
}
}
}
}
4.build now测试

九、java项目发布流程
01.Maven介绍
Maven是一个项目管理和综合工具。Maven提供给开发人员构建一个完整的生命周期框架。
开发团队可以自动完成该项目的基础设施建设,Maven使用标准的目录结构和默认构建生命周期。
Apache的开源项目主要服务于JAVA平台的构建、依赖管理、项目管理。
Project Object Model,项目对象模型。通过xml格式保存的pom.xml文件。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。该文件是由开发维护,我们运维人员可以不用去关心。
02.Maven安装
1、下载Maven 安装包
官网:http://maven.apache.org/download.cgi
清华镜像:https://mirrors.tuna.tsinghua.edu.cn/apache/maven/
代码上线软件里已经准备直接上传
[root@jenkins ~]# ll apache-maven-3.3.9-bin.tar.gz
-rw-r--r-- 1 root root 8491533 9月 27 2024 apache-maven-3.3.9-bin.tar.gz
2、安装Maven
[root@jenkins ~]# tar -xf apache-maven-3.3.9-bin.tar.gz
[root@jenkins ~]# mv apache-maven-3.3.9 /usr/local/
[root@jenkins ~]# ln -s /usr/local/apache-maven-3.3.9/ /usr/local/maven
[root@jenkins ~]# /usr/local/maven/bin/mvn -v
which: no javac in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sonar-scanner/bin/:/root/bin)
Warning: JAVA_HOME environment variable is not set.
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T00:41:47+08:00)
Maven home: /usr/local/maven
Java version: 11.0.30, vendor: BiSheng
Java home: /usr/lib/jvm/java-11-openjdk-11.0.30.7-4.p01.ky10.x86_64
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "4.19.90-52.22.v2207.ky10.x86_64", arch: "amd64", family: "unix"
3、编辑/etc/profile文件,在末尾添加
export PATH="$PATH:/usr/local/sonar-scanner/bin/:/usr/local/maven/bin/"
[root@jenkins ~]# source /etc/profile
[root@jenkins ~]# mvn -v #查看版本号
03.Maven编译打包
1、上传一个简单的java项目包hello‐world.tar.gz进行解压
[root@jenkins ~]# ll hello-world.tar.gz
-rw-r--r-- 1 root root 18950 9月 27 2024 hello-world.tar.gz
[root@jenkins ~]# tar -xf hello-world.tar.gz
2、进入目录执行打包命令
mvn [参数]
validate(验证): 验证项目正确,并且所有必要信息可用。
compile(编译): 编译项目源码
test(测试): 使用合适的单元测试框架测试编译后的源码。
package(打包): 源码编译之后,使用合适的格式(例如JAR格式)对编译后的源码进行打包。
integration‐test(集成测试): 如果有需要,把包处理并部署到可以运行集成测试的环境中去。
verify(验证): 进行各种测试来验证包是否有效并且符合质量标准。
install(安装): 把包安装到本地仓库,使该包可以作为其他本地项目的依赖。
deploy(部署): 在集成或发布环境中完成,将最终软件包复制到远程存储库,以与其他开发人员和项目共享。
mvn clean (清除) : 清除上次编译的结果
mvn package ‐Dmaven.test.skip=true 跳过测试用例
-----------------------------
`进入项目目录`
[root@jenkins ~]# cd hello-world-war/
[root@jenkins hello-world-war]# ll
总用量 8
drwxr-xr-x 2 root root 29 5月 23 2014 dist
-rw-r--r-- 1 root root 931 5月 14 2024 pom.xml
-rw-r--r-- 1 root root 213 5月 23 2014 README.md
drwxr-xr-x 3 root root 18 5月 23 2014 src
`编译打包`
[root@jenkins hello-world-war]# mvn package
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:43 min
[INFO] Finished at: 2026-06-13T16:10:48+08:00
[INFO] Final Memory: 14M/83M
[INFO] ------------------------------------------------------------------------
3.手动部署war
服务器:10.0.0.8 Tomcat
1.部署Tomcat
上传jdk和Tomcat包安装
[root@web02 ~]# ll apache-tomcat-9.0.113.tar.gz jdk-8u181-linux-x64.rpm
-rw-r--r-- 1 root root 13049663 12月 3 2025 apache-tomcat-9.0.113.tar.gz
-rw-r--r-- 1 root root 170023183 9月 27 2024 jdk-8u181-linux-x64.rpm
[root@web02 ~]# rpm -ivh jdk-8u181-linux-x64.rpm
[root@web02 ~]# tar -xf apache-tomcat-9.0.113.tar.gz
[root@web02 ~]# mv apache-tomcat-9.0.113 /usr/local/
[root@web02 ~]# ln -s /usr/local/apache-tomcat-9.0.113/ /usr/local/tomcat
[root@web02 ~]# cd /usr/local/tomcat/webapps/ROOT/
[root@web02 ROOT]# rm -rf *
2.将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/
[root@web02 ROOT]# ll
总用量 4
-rw-r--r-- 1 root root 1933 6月 13 16:21 hello-world-war-1.0.0.war
3.解压war包 启动Tomcat访问
[root@web02 ROOT]# unzip hello-world-war-1.0.0.war
[root@web02 ROOT]# rm -rf hello-world-war-1.0.0.war
[root@web02 ROOT]# ll
总用量 4
-rw-r--r-- 1 root root 210 10月 15 2019 index.jsp
drwxr-xr-x 3 root root 38 6月 13 16:22 META-INF
drwxr-xr-x 3 root root 36 6月 13 16:10 WEB-INF
[root@web02 ROOT]# /usr/local/tomcat/bin/startup.sh

4.Maven优化-将默认的maven仓库源修改为国内的
[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>
</mirrors>
...
---------------
测试配置是否成功
[root@jenkins hello-world-war]# mvn clean
[root@jenkins hello-world-war]# mvn package
注意:如果公司部分java代码 不需要配置nexus私服,只需要将maven配置文件仓库指向阿里云仓库
如果公司是重java项目,则需要搭建私有的仓库nexus
04.集成到Jenkins
1.将java代码推送到gitlab
2.jenkins创建java项目拉取java代码
3.使用maven编译拿到war包
4.将war包推送到web服务器的Tomcat站点目录下
1.将java代码推送到gitlab
1.上传解压代码
root@gitlab:git_data# ll hello-world.tar.gz
-rw-r--r-- 1 root root 18950 Sep 27 2024 hello-world.tar.gz
root@gitlab:git_data# tar -xf hello-world.tar.gz
root@gitlab:git_data# cd hello-world-war/
root@gitlab:hello-world-war# ll
total 16
drwxr-xr-x 2 root root 4096 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 4096 May 23 2014 src/
2.gitlab页面创建项目
新建项目-java
3.将gitlab创建的项目配置为本地的远程仓库
root@gitlab:hello-world-war# git remote
origin
root@gitlab:hello-world-war# git remote -v
origin git@10.0.0.200:test/hello-world.git (fetch)
origin git@10.0.0.200:test/hello-world.git (push)
#删除默认的远程仓库
root@gitlab:hello-world-war# git remote remove origin
root@gitlab:hello-world-war# git remote
#配置远程仓库
root@gitlab:hello-world-war# git remote add origin git@10.0.0.200:oldboy/java.git
root@gitlab:hello-world-war# git remote
origin
root@gitlab:hello-world-war# git remote -v
origin git@10.0.0.200:oldboy/java.git (fetch)
origin git@10.0.0.200:oldboy/java.git (push)
4.将代码提交到本地仓库
root@gitlab:hello-world-war# git add .
root@gitlab:hello-world-war# git commit -m "java"
[master 6e80d6c] java
2 files changed, 3 insertions(+), 3 deletions(-)
root@gitlab:hello-world-war# git push -u origin master
Enumerating objects: 44, done.
Counting objects: 100% (44/44), done.
Delta compression using up to 2 threads
Compressing objects: 100% (29/29), done.
Writing objects: 100% (44/44), 4.90 KiB | 716.00 KiB/s, done.
Total 44 (delta 11), reused 0 (delta 0), pack-reused 0
To 10.0.0.200:oldboy/java.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
2.jenkins创建maven项目
创建

配置git

3.配置Maven编译
在系统配置—>全局工具配置maven家目录位置

回到Jenkins项目配置

4.build now测试
1.点击build now测试
2.查看结果
[root@jenkins java_job]# ll target/
总用量 4
drwxr-xr-x 4 root root 54 6月 13 17:40 hello-world-war-1.0.0
-rw-r--r-- 1 root root 1933 6月 13 17:40 hello-world-war-1.0.0.war
drwxr-xr-x 2 root root 28 6月 13 17:40 maven-archiver
5.写shell将war推送到目标服务器

6.构建测试
1.点击build now
2.访问查看结果

05.配置nexus私服
1.下载部署
服务器:10.0.0.202 nexus
部署私服 xenus 下载
https://www.sonatype.com/download‐oss‐sonatype
配置仓库两个选项
1、项目下的pom.xml配置、只生效当前的项目
2、在maven配置全局所有项目生效
------
`上传JDK安装`
[root@nexus ~]# ll jdk-8u181-linux-x64.rpm
-rw-r--r-- 1 root root 170023183 9月 27 2024 jdk-8u181-linux-x64.rpm
[root@nexus ~]# rpm -ivh jdk-8u181-linux-x64.rpm
`上传nexus安装包`
[root@nexus ~]# ll nexus-3.13.0-01-unix.tar.gz
-rw-r--r-- 1 root root 122904706 9月 27 2024 nexus-3.13.0-01-unix.tar.gz
`解压`
[root@nexus ~]# tar xf nexus-3.13.0-01-unix.tar.gz
# 移动到/usr/local
[root@nexus ~]# mv nexus-3.13.0-01 /usr/local/nexus
# 启动nexus
[root@nexus ~]# /usr/local/nexus/bin/nexus start
WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended!
WARNING: ************************************************************
Starting nexus
#浏览器访问
10.0.0.202:8081 admin admin123
2.页面配置
登录

配置nexus仓库指向阿里云


3.配置Maven全局配置文件
# 修改jenkins编译指向nexus私服
[root@jenkins ~]# cd /usr/local/maven/conf/
[root@jenkins conf]# mv settings.xml settings.xml.bak
# 上传配置文件
[root@jenkins conf]# ll
总用量 28
drwxr-xr-x 2 root root 37 11月 11 2015 logging
-rw-r--r-- 1 root root 11280 9月 27 2024 settings.xml
-rw-r--r-- 1 root root 10374 6月 13 16:28 settings.xml.bak
-rw-r--r-- 1 root root 3649 11月 11 2015 toolchains.xml
# 修改配置信息(一样就不用改)
地址改成nexus地址:http://10.0.0.202:8081/repository/maven-public/

4.编译测试
[root@jenkins ~]# cd hello-world-war/
[root@jenkins hello-world-war]# mvn clean
[root@jenkins hello-world-war]# mvn package

06.Java_job项目触发拉取webhook
1.Jenkins配置

2.gitlab配置

3.推送测试
回服务器:10.0.0.200
1.修改代码,提交
root@gitlab:hello-world-war# pwd
/root/git_data/hello-world-war
root@gitlab:hello-world-war# vim src/main/webapp/index.jsp
root@gitlab:hello-world-war# git add .
root@gitlab:hello-world-war# git commit -m "v1.1"
[master 331b707] v1.1
1 file changed, 1 insertion(+), 1 deletion(-)
2.push上传gitlab
root@gitlab:hello-world-war# git push -u origin master
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 2 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 439 bytes | 439.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0), pack-reused 0
To 10.0.0.200:oldboy/java.git
6e80d6c..331b707 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
3.查看是否触发

07.集成sonarqube
sonarqube新建一个分析项目java项目
1.方式1:配置Pre Steps-编译前
sonar.projectName=Pre Steps
sonar.projectKey=java
sonar.sources=.

2.方式2:配置Build-编译中
clean package sonar:sonar -Dsonar.host.url=http://10.0.0.203:9000 -Dsonar.login=7a36363b865026680bbd2130b12e074c35a35a2a
#解决环境版本问题
--add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED

3.方式3:配置Post Steps-编译后
sonar.projectName=Post Steps
sonar.projectKey=html
sonar.sources=.

4.build now测试

08.配置企业微信
前置条件:已经装好插件注册好企业微信
`jenkins配置`
构建环境-》Add Changelog Information to Environment
Entry Format
填入:%3$s(at %4$s via %1$s)
Date Format
填入:yyyy‐MM‐dd HH:mm:ss
`构建后操作`
py脚本前面已经配置,jenkins填入下面命令就行
echo "==========StartNotify=============="
echo ${SCM_CHANGELOG} > /tmp/${JOB_NAME}_change.log
python /server/scripts/jenkins_notify.py ${BUILD_URL} /tmp/${JOB_NAME}_change.log ${JOB_NAME}
rm ‐f /tmp/${JOB_NAME}_change.log
`测试`
1.push代码触发jenkins
index.jsp v1.1修改为v1.2
root@gitlab:hello-world-war# git add .
root@gitlab:hello-world-war# git commit -m "v1.2"
root@gitlab:hello-world-war# git push -u origin master
2.查看结果

09.线上发布流程
方式1:
1.创建一个java_tag项目
作用:
获取所有标签
编译打包
打上tag版本标签
推送到gitlab # 注意git默认忽略war包和jar包需要将.gitignore里的target删掉
2.创建一个自由风格项目
作用:
获取所有标签
拉取编译好的war包
推送部署到线上服务器
方式2:不通过gitlab,通过Jenkins
方式3:直接手动部署
方式1:示例
1.java_tag项目配置
`配置`
1.创建一个maven风格项目,项目名称java_tag
2.配置Git Parameter:获取所有标签,项目配置-》General-》This project is parameterized勾选-》添加参数Git Parameter
3.配置String Parameter:打tag,项目配置-》General-》This project is parameterized勾选-》添加参数String Parameter
4.源码管理配置:配置git地址,分支默认master分支
5.Build配置:clean package
6.Post Steps配置:
git add .
git commit -m "${tag_version}"
git tag -a ${tag_version} -m "${tag_version}稳定版本"
git push -u origin ${tag_version}
`构建测试`
2.deploy_java项目配置
`配置`
1.创建一个自由风格项目,项目名称deploy_java
2.配置Git Parameter:获取所有标签,项目配置-》General-》This project is parameterized勾选-》添加参数Git Parameter
3.源码管理配置:git项目地址,分支${git_version}
4.Build Steps:
ssh 10.0.0.8 "cd /usr/local/tomcat/webapps && mkdir -p web_${git_version}"
scp target/hello-world-war-1.0.0.war 10.0.0.8:/usr/local/tomcat/webapps/web_${git_version}
ssh 10.0.0.8 "cd /usr/local/tomcat/webapps/web_${git_version} && unzip *.war"
ssh 10.0.0.8 "cd /usr/local/tomcat/webapps && rm -rf ROOT && ln -s web_${git_version} ROOT"
ssh 10.0.0.8 "/usr/local/tomcat/bin/shutdown.sh && /usr/local/tomcat/bin/startup.sh"
`构建测试`
1.gitlab修改源码提交
2.执行java_tag项目
3.执行deploy_java项目

十、配置Jenkins的分布式
如果项目需要定期集成,同时每次集成都需要较长时间。如果都运行在master服务器上,会消耗过多资源,导致其他项目搁置无法集成,这时就需要在建立多台设备,并配置作为slave机器来为master提供负载服务。
01.部署分布式服务器
1.找一台或者目前已有的服务器配置为slave端 #当前使用lb02服务器作为slave测试
在lb02上安装java jdk git和soanr客户端 # 如已安装则略过
企业微信用的py脚本的依赖
2.jenkins服务器操作 注意带版本号做软链接-sonar-maven
3.将所有用到的shell脚本拷贝到lb02
4.jenkins服务端添加从节点lb02
1.lb02环境准备-jdk-py脚本依赖
`jdk`
方式1:rpm安装
1.上传jdk
[root@lb02 ~]# ll jdk-17_linux-x64_bin.rpm
-rw-r--r-- 1 root root 182486159 9月 27 2024 jdk-17_linux-x64_bin.rpm
2.安装
[root@lb02 ~]# rpm -ivh jdk-17_linux-x64_bin.rpm
-------
方式2:yum安装
yum -y install java
`py依赖`
[root@lb02 ~]# yum -y install python2
[root@lb02 ~]# pip2.7 install requestsp
2.从节点安装sonar,maven
[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/
----
[root@lb02 ~]# ll /usr/local/sonar-scanner/
[root@lb02 ~]# ll /usr/local/maven/
3.将所有用的的shell脚本拷贝到lb02
[root@lb02 ~]# mkdir -p /server/scripts
[root@jenkins ~]# scp /server/scripts/* 10.0.0.6:/server/scripts/
[root@lb02 ~]# ll /server/scripts
总用量 8
-rw-r--r-- 1 root root 880 6月 15 15:08 deploy.sh
4.jenkins服务端添加从节点lb02
PS: 把lb02的公钥上传至gitlab 拥有下载代码的权限
[root@lb02 ~]# cat .ssh/id_rsa.pub # 前面dev账号已经添加可以不用
-----
1.jenkins添加从节点lb02
点击系统管理->节点管理->新建节点


5.从节点配置

注意是将Jenkins私钥放到key里面


6.从节点配置工具的家目录位置
maven,sonar

配置成功

02.测试
jenkins 默认优先使用从节点
lb02和web服务器做免秘钥
[root@lb02 ~]# ssh-copy-id 10.0.0.8
1.回gitlab服务器修改Java源码
root@gitlab:hello-world-war# vim src/main/webapp/index.jsp
2.提交触发钩子
root@gitlab:hello-world-war# git add .
root@gitlab:hello-world-war# git commit -m "v2.1"
root@gitlab:hello-world-war# git push -u origin master
3.查看结果

相关面试题
01.代码发布流程?
两套流程:
1.测试流程
开发写代码-》提交到代码仓库-》合并到测试分支触发webhook-》Jenkins拉取代码到sonarqube进行代码检测-》Jenkins推送到测试服务器-》企业微信通知
Java项目还需要进行maven编译
2.线上发布
jenkins拉取测试好可以发布线上的版本-》通过参数化构建和打标签,推送到gitlab-然后在拉取对应的版本进行发布或者回滚,底层是用shell脚本实现,推送到目标服务器后通过软链接链接到对应目录
相关发布概念:
灰度发布
金丝雀发布
滚动发布
蓝绿发布
02.jenkins用过哪些插件?
git
GitLab
SSH server
Git Parameter 参数化构建
webhook 钩子触发
SonarQube Scanner 代码扫描
Maven Java项目编译
Post build task 企业微信
Blue Ocean 流水线页面