基于 Gitlab、Jenkins與Jenkins分布式、SonarQube 、Nexus 的 CiCd 全流程打造

前言

在當今數字化飛速發展的時代,軟件開發與交付的效率和質量成為了企業競爭的關鍵要素。為了滿足市場對軟件快速迭代和高質量交付的需求,越來越多的企業開始探索和實踐持續集成與持續交付(CI/CD)的開發模式。而 GitLab、Jenkins、SonarQube 和 Nexus 作為當前軟件開發領域中廣受歡迎且功能強大的工具,各自在版本控制、自動化構建與部署、代碼質量檢測以及依賴管理等方面發揮著重要作用。

本博文將深入探討如何巧妙地整合 GitLab、Jenkins、SonarQube 和 Nexus 這四大工具,打造一個高效、穩定、可靠的 CI/CD 全流程。通過這一流程的構建,開發團隊能夠實現代碼從提交到部署的自動化與規范化,提升軟件開發效率,保障代碼質量,同時降低人工干預帶來的風險,為企業在激烈的市場競爭中贏得先機。接下來,讓我們一起深入了解這一強大的 CI/CD 生態系統的搭建與運作。

角色IP地址/OS
Gitlab10.0.0.200/Ubuntu22.04LTS
Jenkins+Nexus10.0.0.202/KylinV10SP3
Jenkins分布式從節點10.0.0.5/KylinV10SP3
SonarQube10.0.0.203/KylinV10SP3
web01測試環境10.0.0.7/kylinV10SP3
web02測試環境10.0.0.8/kylinV10SP3

一、安裝與使用Gitlab

tips: 不熟悉Git分布式版本控制軟件使用的可以看我之前發布的一篇有關Git使用的博文:?Git 與 GitLab:開啟代碼協同的實戰之旅-CSDN博客https://blog.csdn.net/shjwkzkzk/article/details/148625177?spm=1001.2014.3001.5501

1.? 安裝Gitlab

gitlab-ce安裝包下載_開源鏡像站-阿里云

# 關閉防火墻
root@git:~# ufw disable
Firewall stopped and disabled on system startup
# Ubuntu的deb國內包倉庫
https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/jammy/main/g/gitlab-ce/
# 安裝依賴
apt-get install -y curl openssh-server ca-certificates tzdata perl
# 上傳我們的deb包
root@git:~# ll
total 1055524
drwxr-xr-x 3 root root       4096 Feb 17 11:57 git_code
-rw-r--r-- 1 root root 1080842786 Feb 17 04:03 gitlab-ce_16.5.2-ce.0_amd64.deb
# 執行安裝命令
root@git:~# dpkg -i gitlab-ce_16.5.2-ce.0_amd64.deb 
出現以下頁面表示安裝成功:

Please configure a URL for your GitLab instance by setting `external_url`
configuration in /etc/gitlab/gitlab.rb file.
Then, you can start your GitLab instance by running the following command:sudo gitlab-ctl reconfigure

2.?配置external_url

我們在配置文件中添加我們的url  ------->我們的IP的地址對應的URL
root@git:~# sed -n '/^external_url/p' /etc/gitlab/gitlab.rb
external_url 'http://10.0.0.200'
執行以下命令:
root@git:~# gitlab-ctl reconfigure然后訪問:
URL:
http://10.0.0.200user:
rootPasswd: #存在與我們的文件包里面
root@git:~# sed -n '/^Password/p' /etc/gitlab/initial_root_password
Password: v2cXZszGVo1iekuEvMZGgQ2d1GYew653c/dFTXUcy+g=

3.使用gitlab

a.修改密碼

輸入我們的用戶名和密碼進行登入

密碼太復雜我們修改一下密碼 ?

?修改即可

b.修改語言為中文

刷新即可

?這里最好不要允許任何人都可以注冊

c.添加項目

空的代碼倉庫,在gitlab創建代碼倉庫,然后拉取到本地服務器

?這里導入空白項目時最好創建一下群組

?

d.打通系統與gitlab root 賬戶的SSH
root@git:~# ssh-keygenroot@git:~# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC2iDEj+0P1fdawoyPOTGgvnOil/HiWfCLN3COS0GcNN8795ib6OhRB4FlTWKNgEZdFsl6vEFNOUm8q6Y2/oARyUMFCnXeYnvhg1ysHF0y48s1BC/uK/GOF32gQ+uJSTj2kapzYepUcCR7oOVbbIg5jbm24HGLTd+BhIOjKnX6zwKimWlaSeo6pha9xbpPzbLnG3XaZY8myojJrm1VaNCUbvDuO/2EzX1+uL8GlTPiXOPNyj8bOjClFZ4vcluMnLzDnhAJiv9tW/oktLYh5ro0hxTz+j5/a0Kj1/SM82jJA/oO4glzkn/wyb8il8fwkiPC/QQcifLMMAuPYH2lwcqFsaC++oj8zrxO7/nzX/PFMW40IicvYBbG0y2alik7kMjxx53XAEZFs33dAu1YMJKkVKcFzxZAk94n/+qJOa7T+Dx1e1BSVjMK2WmlpQtrftzrZROpbU0tzFnd4tgFKzh85Tpc/i4xqPEh+DmzvgUPiQLKIYNBzb29VthBuYxPsaDs= root@git

??

e.使用項目倉庫

命令行將空的倉庫下載到本地
# 這里粗心了,創建了一個和git本地倉庫名稱一樣的項目倉庫....
root@git:~# git clone git@10.0.0.200:BestCode/git_code.git
fatal: destination path 'git_code' already exists and is not an empty directory.
root@git:~# ll
total 1055524
drwxr-xr-x 3 root root       4096 Feb 17 11:57 git_code
-rw-r--r-- 1 root root 1080842786 Feb 17 04:03 gitlab-ce_16.5.2-ce.0_amd64.deb
drwx------ 3 root root       4096 Oct 24 10:37 snap
root@git:~# rm -rf git_code/
root@git:~# git clone git@10.0.0.200:BestCode/git_code.git
Cloning into 'git_code'...
The authenticity of host '10.0.0.200 (10.0.0.200)' can't be established.
ED25519 key fingerprint is SHA256:Zk4qeVRp5S3WJiLnUHpyo04qjc9QdCC43c/F2Tdjfpg.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.200' (ED25519) to the list of known hosts.
warning: You appear to have cloned an empty repository.
root@git:~# cd git_code/
root@git:~/git_code# ll 
total 0
root@git:~/git_code# ll -a
total 12
drwxr-xr-x 3 root root 4096 Feb 17 12:49 .
drwx------ 6 root root 4096 Feb 17 12:49 ..
drwxr-xr-x 7 root root 4096 Feb 17 12:49 .git### 然后還要配置一下是誰在使用這個倉庫,配置一下使用人信息
root@git:~/git_code# git config --global user.name "caofacan"
root@git:~/git_code# git config --global user.email "cfc@can.com"# 創建新的文件提交到本地倉庫
root@git:~/git_code# touch game.jdk
root@git:~/git_code# ll
total 0
-rw-r--r-- 1 root root 0 Feb 17 12:52 game.jdk
root@git:~/git_code# git add .
root@git:~/git_code# git commit -m "gitlab-game----->V1.0"
[master (root-commit) ef66792] gitlab-game----->V1.01 file changed, 0 insertions(+), 0 deletions(-)create mode 100644 game.jdk# 查看遠程倉庫的名稱root@git:~/git_code# git remote 
origin
# 查看遠程倉庫的詳細地址
root@git:~/git_code# git remote -v
origin  git@10.0.0.200:BestCode/git_code.git (fetch)
origin  git@10.0.0.200:BestCode/git_code.git (push)
# 將本地倉庫的代碼提交到遠程倉庫
root@git:~/git_code# git push -u origin master
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 209 bytes | 209.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To 10.0.0.200:BestCode/git_code.git* [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.# 因為當前處于master分支,所以后面參數是master
root@git:~/git_code# git branch 
* master

刷新我們的項目頁面就可以看到了

如果代碼已經存在本地我們可以:
1.本地倉庫已經初始化
2.本地代碼已經存在?

root@git:~# mkdir code_data
root@git:~# cd code_data/
root@git:~/code_data# git init
Initialized empty Git repository in /root/code_data/.git/
root@git:~/code_data# ll -a
total 12
drwxr-xr-x 3 root root 4096 Feb 17 12:58 .
drwx------ 7 root root 4096 Feb 17 12:58 ..
drwxr-xr-x 7 root root 4096 Feb 17 12:58 .git
root@git:~/code_data# touch {1..5}.py
root@git:~/code_data# ll
total 0
-rw-r--r-- 1 root root 0 Feb 17 12:58 1.py
-rw-r--r-- 1 root root 0 Feb 17 12:58 2.py
-rw-r--r-- 1 root root 0 Feb 17 12:58 3.py
-rw-r--r-- 1 root root 0 Feb 17 12:58 4.py
-rw-r--r-- 1 root root 0 Feb 17 12:58 5.py
root@git:~/code_data# git add .
root@git:~/code_data# git commit -m "git_code-game------>V1.2py"
[master (root-commit) 9932a0a] git_code-game------>V1.2py5 files changed, 0 insertions(+), 0 deletions(-)create mode 100644 1.pycreate mode 100644 2.pycreate mode 100644 3.pycreate mode 100644 4.pycreate mode 100644 5.py## 這時候如果我們直接push,會出現報錯root@git:~/code_data# git push -u origin master
To 10.0.0.200:BestCode/git_code.git! [rejected]        master -> master (fetch first)
error: failed to push some refs to '10.0.0.200:BestCode/git_code.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.這個錯誤表明遠程倉庫的 master 分支包含了本地倉庫沒有的更新,所以 Git 拒絕了推送操作。你需要先拉取遠程倉庫的更新并合并到本地,再推送。解決: # 我們將本地倉庫的代碼提交到遠程倉庫如果直接pull的話,也會報錯
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
這個報錯表明本地和遠程的 `master` 分支發生了分叉,Git 無法自動決定如何合并它們,需要你指定合并策略(如合并、變基或僅快速合并)。
root@git:~/code_data# git config pull.rebase false
root@git:~/code_data# git config pull.rebase true
root@git:~/code_data# git pull origin master
From 10.0.0.200:BestCode/git_code* branch            master     -> FETCH_HEAD
Successfully rebased and updated refs/heads/master.
root@git:~/code_data# ll
total 4
-rw-r--r-- 1 root root    0 Feb 17 13:06 1.py
-rw-r--r-- 1 root root    0 Feb 17 13:06 2.py
-rw-r--r-- 1 root root    0 Feb 17 13:06 3.py
-rw-r--r-- 1 root root    0 Feb 17 13:06 4.py
-rw-r--r-- 1 root root    0 Feb 17 13:06 5.py
-rw-r--r-- 1 root root    0 Feb 17 13:06 game.jdk
drwxr-xr-x 3 root root 4096 Feb 17 13:02 git_code
root@git:~/code_data# git add .
warning: adding embedded git repository: git_code
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint: 
hint:   git submodule add <url> git_code
hint: 
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint: 
hint:   git rm --cached git_code
hint: 
hint: See "git help submodule" for more information.
root@git:~/code_data# git rm --cached git_code
error: the following file has staged content different from both the
file and the HEAD:git_code
(use -f to force removal)
root@git:~/code_data# git add .
root@git:~/code_data# git commit -m "py--->V1.2"
[master 7d53f65] py--->V1.21 file changed, 1 insertion(+)create mode 160000 git_code
root@git:~/code_data# git push origin master
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 454 bytes | 454.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
To 10.0.0.200:BestCode/git_code.gitef66792..7d53f65  master -> master

4. Gitlab權限控制

1. 創建普通用戶dev賦予對應權限

管理中心------>儀表盤----->新建用戶

?

點擊編輯,設置登錄密碼(dev@123.com) ?

使用普通賬號登錄gitlab 默認里面空的

開第二個窗口,因為cookie的原因,瀏覽器只能存儲一個cookie。

?將dev賬號添加到BestCode組中,dev用戶擁有game項目的權限。

?

模擬開發人員下載代碼到本地服務器 ?

dev開發使用的服務器10.0.0.7
1.開發需要再自己的linux系統中生成秘鑰對
[root@dev:~]#ssh-keygen
2.將公鑰復制到gitlab的 dev賬號中
[root@web01 ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC4ng0mdQxOaU0dq24KjIGC/rcwgWjMIGPgDsdGSyKS3f8FxqLL2y67/FUT1tMd1TGX5V6OcCEnIQC91P0wbOmPpqxWpGuBhdA+7sMxBJaIyVNJgwbO2jFnPc4S9i8zeUusqIdfjRmMNOtr5xu0u8tot6YuIpOA3KNxmpasIWPuSS2V46ab49uRMv92j9SNAFd0uCRzd+wQGztqOwzoi4N1qdzuuw/P8FTOSD3Ws9K0YNXzWGHT/JwIL7LdDWnd8XvskmkueS65OjLWYUSUYhL4qTSJ7f9qY0cUZ6HjM1Dh8xXfFGyA7pgrBayfGkyhS+lqMRruR7HgW4hyapzi7JX4oSrZICAb/VsHfXFY3Zd2ug57JNYDGyQg3Fq7yXWvrlmgZOi54Cvz6dXA+0H3I+mnXtM2X9kWOL7aFGaVuLe1gRQLAg8pBWw7MZ8BekIFjRAhmghrg3eJK4z330P5u4xAs5c15GWp+Gm6lX4Lxpl+qjVOR1rKusMZCsDF0nZ9f8s= root@web01

因為我們只有一個群組項目都在BestCode群組里面,所以我們普通用戶加入到這個群組之后,就能看到所以的項目了,這里沒有細分。

3.下載游戲代碼到本地
[root@web01 ~]# git clone git@10.0.0.200:BestCode/game.git
Cloning into 'game'...
The authenticity of host '10.0.0.200 (10.0.0.200)' can't be established.
ECDSA key fingerprint is SHA256:KmllkN7Me0V380RXxrpFR0hd+H6hNqtUgXGBl9Smykc.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.200' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 102, done.
remote: Counting objects: 100% (102/102), done.
remote: Compressing objects: 100% (99/99), done.
remote: Total 102 (delta 3), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (102/102), 14.86 MiB | 9.71 MiB/s, done.
Resolving deltas: 100% (3/3), done.
[root@web01 ~]# ll
total 4
drwxr-xr-x 7 root root  152 Feb 17 21:40 game
-rw-r--r-- 1 root root 2281 Feb 15 15:20 os_check.sh
[root@web01 ~]# ll game/
total 7764
-rw-r--r-- 1 root root   28032 Feb 17 21:40 bgm.mp3
drwxr-xr-x 2 root root      23 Feb 17 21:40 css
-rw-r--r-- 1 root root 7890373 Feb 17 21:40 game.zip
drwxr-xr-x 2 root root      23 Feb 17 21:40 images
-rw-r--r-- 1 root root    8956 Feb 17 21:40 index.html
drwxr-xr-x 2 root root     213 Feb 17 21:40 js
-rw-r--r-- 1 root root    6153 Feb 17 21:40 README.md
drwxr-xr-x 2 root root    4096 Feb 17 21:40 roms
-rw-r--r-- 1 root root     811 Feb 17 21:40 shuoming.html

?需要游戲實例代碼可通過主頁email聯系我。

?2. dev用戶上傳代碼流程
# 我們模擬一下修改代碼
[root@web01 ~/game]# vim index.html"經典": [['魂斗羅V1.0第一次修改', 'roms/Contra1(U)30.nes'],['超級瑪麗', 'roms/(W) Super Mario Bros. [!].nes'],......                
[root@web01 ~/game]# git add .
[root@web01 ~/game]# git commit -m "index.html---->第一次修改"
# 配置使用人信息
[root@web01 ~/game]# git config --global user.name "dev"
[root@web01 ~/game]# git config --global user.email "dev@123.com"
[root@web01 ~/game]# cat ../.gitconfig 
[user]name = devemail = dev@123.com
# 提交到遠程倉庫
默認不允許提交到master主干分支,需要先創建子分支,然后提交子分支,在發起代碼合并請求master 
[root@web01 ~/game]# git branch ops
[root@web01 ~/game]# git push -u origin ops

dev新建一下合并請求

root處理合并請求

?

二、 Jenkins集成Gitlab

Jenkins 是一個開源的自動化服務器,用于實現持續集成和持續交付,幫助開發團隊自動化構建、測試和部署軟件,提高開發效率和軟件質量。?

1.?安裝Jenkins

點我下載Jenkins軟件包owo

# 安裝JDK運行環境
[root@Jenkins ~]# yum -y install java
# 上傳Jenkins的RPM包
[root@Jenkins ~]# ll
total 91220
-rw-r--r-- 1 root root 93405530 Sep 27 17:20 jenkins-2.405-1.1.noarch.rpm
# 安裝Jenkins
[root@Jenkins ~]# rpm -ivh jenkins-2.405-1.1.noarch.rpm 
# 配置Jenkins
[root@Jenkins ~]# grep -n root /etc/sysconfig/jenkins 
31:JENKINS_USER="root"
[root@Jenkins ~]# grep -n root /usr/lib/systemd/system/jenkins.service
34:User=root
35:Group=root
# 啟動Jenkins服務
[root@Jenkins ~]# systemctl daemon-reload
[root@Jenkins ~]# systemctl start jenkins.service 
瀏覽器訪問: 10.0.0.202:8080

?

?因為Jenkins要依賴于多又強大的插件來完成工作的,這里我網絡不是很好所以我需要導入準備好的插件到Jenkins的插件目錄下。

# 上傳我們打包好的插件包
[root@Jenkins ~]# tar xf jenkins_plu.tar.gz -C /var/lib/jenkins/plugins/
# 重啟Jenkins服務
[root@Jenkins ~]# systemctl restart jenkins.service 
# 進入Jenkins修改密碼,因為插件中我們有中文插件

大致所需的插件如下(需要插件包一樣可以聯我email):

Jenkins有很多插件,以下是一些最常用的插件,它們能夠極大地增強Jenkins的功能:
1. Git Plugin ?
? ?用于與Git版本控制系統集成,支持從Git倉庫拉取代碼進行構建。
2. Maven Integration Plugin?
? ?用于與Maven構建工具集成,幫助執行Maven構建生命周期,自動化Maven項目的構建過程。
3. Pipeline Plugin ?
? ?使Jenkins支持聲明式和腳本化的流水線(Pipeline),可以定義和管理CI/CD工作流。
4. Blue Ocean Plugin?
? ?提供一個現代化的Jenkins用戶界面,支持直觀地查看流水線、作業和結果,幫助用戶輕松管理和監控流水線。
5. Slack Notification Plugin?
? ?可以將Jenkins構建的狀態(成功、失敗、穩定等)發送到Slack頻道,方便團隊實時查看構建結果。
6. Docker Pipeline Plugin ?
? ?使Jenkins流水線能夠輕松與Docker集成,支持在Docker容器中運行構建和測試。
7. JUnit Plugin ?
? ?用于展示JUnit測試結果,生成測試報告,并將其集成到Jenkins中,便于查看測試的成功率和失敗詳情。
8. GitHub Plugin ?
? ?用于將Jenkins與GitHub集成,自動觸發構建、同步GitHub代碼庫等。
9. Parameterized Trigger Plugin ?
? ?允許Jenkins作業觸發其他作業,并支持傳遞參數,從而實現復雜的作業依賴和多階段構建流程。
10. Credentials Binding Plugin?
? ? 讓你能夠在構建過程中安全地使用密鑰、用戶名等敏感信息,而不暴露在構建日志中。
這些插件能夠幫助你在Jenkins中實現自動化構建、測試、部署、通知等各種功能,極大提升工作流的效率。

Jenkins默認的用戶為admin

?下翻找到密碼

因為是測試環境我修改為了123456 ?

2.體驗Jenkins的自動化

a.克隆代碼

進入到代碼目錄,下載game的代碼

[root@web01 /code]# git clone git@10.0.0.200:BestCode/game.git
[root@web01 /code]# mv game/* .
[root@web01 /code]# ll
total 7764
-rw-r--r-- 1 root root   28032 Feb 18 19:29 bgm.mp3
drwxr-xr-x 2 root root      23 Feb 18 19:29 css
drwxr-xr-x 3 root root      18 Feb 18 19:29 game
-rw-r--r-- 1 root root 7890373 Feb 18 19:29 game.zip
drwxr-xr-x 2 root root      23 Feb 18 19:29 images
-rw-r--r-- 1 root root    8976 Feb 18 19:29 index.html
drwxr-xr-x 2 root root     213 Feb 18 19:29 js
-rw-r--r-- 1 root root    6153 Feb 18 19:29 README.md
drwxr-xr-x 2 root root    4096 Feb 18 19:29 roms
-rw-r--r-- 1 root root     811 Feb 18 19:29 shuoming.html
b.新建一個Jenkins任務

c.查看Jenkins的執行命令的默認路徑

?

?d.Jenkins拉取gitlab的代碼
# 1.Jenkins生成密鑰對
[root@Jenkins ~]# ssh-keygen 
+---[RSA 3072]----+
|.oo. ..    + E   |
|.o. =+.o. ooo    |
|. +o+=o.o.o o    |
| = =+.+o . o .   |
|. . +*o S . +    |
|   ..o o . + o   |
|    =   . o o    |
|     o    .+     |
|        .o..o    |
+----[SHA256]-----+
[root@Jenkins ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC7M3Q/3QYGwSbb3lxDcgmaLaTk9aRF7zEFG3g5ImuLJnKED7/e2LfPAr0hGd8a2WrQWQQEhP5u4qGfMdPVduekXdhj0rbUQMS8V+jJ/o7mOYEiBZ4IAd2xlKoDhIM3Lj7pof8vrNEIwLqblfVQqDefPKdAVnHnSTdSDFv5DFE32xpb6LcpNKpZS8zGefhTKPwcOEmQ85Br5tqWAfSpj4Huwm8c/kv7rOOVKq/FJE+ly0n774Fga9Z8Q0AmYkHr7/6jZ+SDAFg3ha/grfePhVDrv7z9B5VubZgHSzIUI7G3AhQ/6IWInIUxNcz4WhODTKfZQoYqprDZ/mxsjeay7frwG0Kzh95CwkMcPXeZOg4tx/E8LO9EPYlcNFizA1kbjh4XEDtVw81QL4ObnPohoMW32EtI+Rh71EI3R2wgIhj/c8QhIA9gfTWhqMcLFBqAQ7dubOePgfsb7A8NAf9OvYB/BvuyRExs6rYDERVgu4hS3Elmefz67iHShFx1HTSTYcc= root@Jenkins

將公鑰復制到gitlab的root賬號中?

?

?e.配置Jenkins拉取game

出現報錯,分析錯誤,解決報錯

現在本地拉一下,把驗證過了之后,刪除即可
[root@Jenkins /tmp]# git clone git@10.0.0.200:BestCode/game.git

?完成后執行構建,綠色表示沒有問題

# 查看拉取的代碼
[root@Jenkins ~]# cd /var/lib/jenkins/workspace/
[root@Jenkins /var/lib/jenkins/workspace]# ll
total 0
drwxr-xr-x 7 root root 152 Feb 18 19:45 game_job
[root@Jenkins /var/lib/jenkins/workspace]# ll game_job/
total 7764
-rw-r--r-- 1 root root   28032 Feb 18 19:45 bgm.mp3
drwxr-xr-x 2 root root      23 Feb 18 19:45 css
-rw-r--r-- 1 root root 7890373 Feb 18 19:45 game.zip
drwxr-xr-x 2 root root      23 Feb 18 19:45 images
-rw-r--r-- 1 root root    8976 Feb 18 19:45 index.html
drwxr-xr-x 2 root root     213 Feb 18 19:45 js
-rw-r--r-- 1 root root    6153 Feb 18 19:45 README.md
drwxr-xr-x 2 root root    4096 Feb 18 19:45 roms
-rw-r--r-- 1 root root     811 Feb 18 19:45 shuoming.html
f.自動推送到web服務器上面
我們再將拉下來的代碼推送到web服務器上面
# 在此之前先做個免密鑰
[root@Jenkins /var/lib/jenkins/workspace]# ssh-copy-id 10.0.0.7# 再將我們web服務器的代碼目錄清空
[root@web01 /code]# ll
total 7764
-rw-r--r-- 1 root root   28032 Feb 18 19:29 bgm.mp3
drwxr-xr-x 2 root root      23 Feb 18 19:29 css
drwxr-xr-x 3 root root      18 Feb 18 19:29 game
-rw-r--r-- 1 root root 7890373 Feb 18 19:29 game.zip
drwxr-xr-x 2 root root      23 Feb 18 19:29 images
-rw-r--r-- 1 root root    8976 Feb 18 19:29 index.html
drwxr-xr-x 2 root root     213 Feb 18 19:29 js
-rw-r--r-- 1 root root    6153 Feb 18 19:29 README.md
drwxr-xr-x 2 root root    4096 Feb 18 19:29 roms
-rw-r--r-- 1 root root     811 Feb 18 19:29 shuoming.html
[root@web01 /code]# rm -rf *
[root@web01 /code]# ll
total 0

保存后構建,然后直接去web服務器查看代碼目錄是否已經存在

[root@web01 /code]# ll
total 7764
-rw-r--r-- 1 root root   28032 Feb 18 19:50 bgm.mp3
drwxr-xr-x 2 root root      23 Feb 18 19:50 css
-rw-r--r-- 1 root root 7890373 Feb 18 19:50 game.zip
drwxr-xr-x 2 root root      23 Feb 18 19:50 images
-rw-r--r-- 1 root root    8976 Feb 18 19:50 index.html
drwxr-xr-x 2 root root     213 Feb 18 19:50 js
-rw-r--r-- 1 root root    6153 Feb 18 19:50 README.md
drwxr-xr-x 2 root root    4096 Feb 18 19:50 roms
-rw-r--r-- 1 root root     811 Feb 18 19:50 shuoming.html

?web01測試環境準備NGINX環境

[root@web01 /etc/nginx/conf.d]# cat default.conf 
server {listen 80;server_name _;location / {root /code;index index.html;}
}
[root@web01 /etc/nginx/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@web01 /etc/nginx/conf.d]# systemctl restart nginx
[root@web01 /etc/nginx/conf.d]# curl localhost
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.26.1</center>
</body>
</html>

訪問一下我們的web服務器
瀏覽器打開: 10.0.0.7?

g.更新代碼后,使用Jenkins更新web
[root@web01 /code]# vim index.html
= new JSNES({'ui': $('#emulator').JSNESUI({"經典": [['魂斗羅V1.0第一次修改', 'roms/Contra1(U)30.nes'],['超級瑪麗'V1.0第一次修改, 'roms/(W) Super Mario Bros. [!].nes'],
[root@web01 /code]# git remote add origin git@10.0.0.200:BestCode/game.git
[root@web01 /code]# git remote -v
origin  git@10.0.0.200:BestCode/game.git (fetch)
origin  git@10.0.0.200:BestCode/game.git (push)
[root@web01 /code]# git commit -am "web01第二次修改魂斗羅"
[master 26f6c97] web01第二次修改魂斗羅1 file changed, 1 insertion(+), 1 deletion(-)
[root@web01 /code]# git push -u origin testing
Enumerating objects: 100, done.
Counting objects: 100% (100/100), done.
Compressing objects: 100% (98/98), done.
Writing objects: 100% (100/100), 14.86 MiB | 8.94 MiB/s, done.
Total 100 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), done.
remote: 
remote: To create a merge request for testing, visit:
remote:   http://10.0.0.200/BestCode/game/-/merge_requests/new?merge_request%5Bsource_branch%5D=testing
remote: 
To 10.0.0.200:BestCode/game.git* [new branch]      testing -> testing
Branch 'testing' set up to track remote branch 'testing' from 'origin'.

訪問gitlab頁面合并一下代碼

切換到root用戶處理一下請求

4.配置自動觸發webhook勾子

# 保存一下我們的token
6ee0bae8420a9901385f33d4957ca68c
# 保存一下URL:
http://10.0.0.202:8080/project/game_job

跳到我們的gitlab添加鉤子

?

然后構建Jenkins測試鉤子
root@git:~/game# git pull
root@git:~/game# vim index.html 
'ui': $('#emulator').JSNESUI({"經典": [['魂斗羅V1.2第二次修改', 'roms/Contra1(U)30.nes'],['超級瑪麗V1.0第一次修改', 'roms/(W) Super Mario Bros. [!].nes'],['坦克V1.0master第一次修改', 'roms/(Ch) Missile Tank.nes'],
root@git:~/game# git commit -am "master第一次修改index.html"
[master 3a515cf] master第一次修改index.html1 file changed, 1 insertion(+), 1 deletion(-)
root@git:~/game# git push -u origin master 
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 326 bytes | 326.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
To 10.0.0.200:BestCode/game.gitf089fe6..3a515cf  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
然后等著鉤子觸發,web服務器直接就push到了最新的代碼。
[root@web01 /code]# grep 'master' index.html ['坦克V1.0master第一次修改', 'roms/(Ch) Missile Tank.nes'],

?Jenkins沒有問題

三、SonarQube代碼掃描工具

jenkins將代碼拉取到jenkins本地,先將代碼推送到sonar服務器上代碼掃描檢測,檢測代碼的漏洞 邏輯 壞味道。

1.安裝sonarqube

# 安裝Java
[root@SonarQube ~]# yum -y install java
# 安裝數據庫
[root@SonarQube ~]# wget dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
[root@SonarQube ~]# rpm -ivh mysql-community-release-el6-5.noarch.rpm
#禁止gpgcheck
[root@SonarQube ~]# 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@SonarQube ~]# yum -y install mariadb-server
[root@SonarQube ~]# service mysqld start
[root@SonarQube ~]# mysqladmin -uroot password caofacan123
Warning: Using a password on the command line interface can be insecure.
[root@SonarQube ~]# mysql -uroot -pcaofacan123 -e "CREATE DATABASE sonar DEFAULT CHARACTER SET utf8;"
Warning: Using a password on the command line interface can be insecure.
[root@SonarQube ~]# mysql -uroot -pcaofacan123 -e "show databases;"
Warning: Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sonar              |
+--------------------+
# 安裝sonarqube
[root@SonarQube ~]# unzip sonarqube-7.0.zip -d /usr/local/
[root@SonarQube ~]# cd /usr/local/
[root@SonarQube /usr/local]# ln -s sonarqube-7.0/ sonarqube
[root@SonarQube /usr/local/sonarqube]# pwd
/usr/local/sonarqube
# 修改配置文件
[root@SonarQube /usr/local/sonarqube]# sed -n '16p;17p;26p' conf/sonar.properties 
sonar.jdbc.username=root
sonar.jdbc.password=caofacan123
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
26行取消注釋即可# 創建普通用戶sonar
sonarqube服務必須由普通用戶運行。
[root@SonarQube /usr/local/sonarqube]# useradd sonar
[root@SonarQube /usr/local/sonarqube]# chown -R sonar.sonar /usr/local/sonarqube-7.0/
# 使用sonar用戶運行服務
[root@SonarQube ~]# su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start"
Starting SonarQube...
Started SonarQube.# 瀏覽器訪問:10.0.0.203:9000

安裝插件

[root@SonarQube ~]# cd /usr/local/sonarqube/extensions/
[root@SonarQube /usr/local/sonarqube/extensions]# ll
total 4
drwxr-xr-x 2 sonar sonar    6 Feb 18 21:10 downloads
drwxr-xr-x 3 sonar sonar   20 Feb  2  2018 jdbc-driver
drwxr-xr-x 2 sonar sonar 4096 Feb 18 21:10 plugins
[root@SonarQube /usr/local/sonarqube/extensions]# cd plugins/
[root@SonarQube /usr/local/sonarqube/extensions/plugins]# rm -rf *
[root@SonarQube /usr/local/sonarqube/extensions/plugins]# tar xf /root/sonar_plugins.tar.gz -C .
[root@SonarQube /usr/local/sonarqube/extensions/plugins]# mv plugins/* .
[root@SonarQube /usr/local/sonarqube/extensions/plugins]# su - sonar -c "/usr/local/sonarqube/bin/linux-x86-64/sonar.sh restart"

登錄sonarqube:用戶名和密碼都是admin

登錄之后

?令牌與標識都為html

## 信息要保留下來
sonar-scanner \-Dsonar.projectKey=html \-Dsonar.sources=. \-Dsonar.host.url=http://10.0.0.203:9000 \-Dsonar.login=73b11ffacb7d24ecabb161c592797a2e0bda3d57

2.客戶端掃描代碼后上傳sonar服務端

# 安裝客戶端在Jenkins
[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/
total 0
drwxr-xr-x 2 root root 18 Dec  2 16:00 bin
drwxr-xr-x 2 root root  6 Mar  6  2021 etc
drwxr-xr-x 2 root root  6 Mar  6  2021 games
drwxr-xr-x 2 root root  6 Mar  6  2021 include
drwxr-xr-x 2 root root  6 Mar  6  2021 lib
drwxr-xr-x 3 root root 17 Dec  2 23:59 lib64
drwxr-xr-x 2 root root  6 Mar  6  2021 libexec
drwxr-xr-x 2 root root  6 Mar  6  2021 sbin
drwxr-xr-x 5 root root 49 Dec  2 23:59 share
drwxr-xr-x 6 root root 51 Oct  1  2019 sonar-scanner
drwxr-xr-x 2 root root  6 Mar  6  2021 src
[root@Jenkins ~]# export PATH="$PATH:/usr/local/sonar-scanner"
[root@Jenkins ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root/bin:/usr/localsonar-scanner/bin
# 寫入到環境變量
[root@Jenkins ~]# tail -1 /etc/profile
export PATH="$PATH:/usr/local/sonar-scanner/bin"# 執行代碼掃描
[root@Jenkins ~/game]# pwd
/root/game
在代碼目錄執行
[root@Jenkins ~/game]# sonar-scanner \-Dsonar.projectKey=html \-Dsonar.sources=. \-Dsonar.host.url=http://10.0.0.203:9000 \-Dsonar.login=73b11ffacb7d24ecabb161c592797a2e0bda3d57INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 8.098s
INFO: Final Memory: 7M/116M
INFO: ------------------------------------------------------------------------
成功

3.配置jenkins集成soanrqube

?

# 修改配置文件指向 203的9000端口,默認是指向自己再添加一行login信息
[root@Jenkins /usr/local/sonar-scanner/conf]# pwd
/usr/local/sonar-scanner/conf
[root@Jenkins /usr/local/sonar-scanner/conf]# grep -E 'sonar.host.url|sonar.login' /usr/local/sonar-scanner/conf/sonar-scanner.properties 
sonar.host.url=http://10.0.0.203:9000
sonar.login=73b11ffacb7d24ecabb161c592797a2e0bda3d57
之前的執行腳本的login編號

?下滑找到,添加Jenkins信息

?

回到我們的Jenkins的工程

?

?

sonar.projectName=${JOB_NAME} # 項目在sonarqube上的顯示名稱
sonar.projectKey=html # 項目的唯一表示,不能重復
sonar.sources=. # 掃描那個項目的源碼

然后Jenkins就可以立即構建了

這里我們走一遍完整的流程

root@git:~/game# vim index.html 
root@git:~/game# git commit -am "master第二次修改"
[master 5872f80] master第二次修改1 file changed, 1 insertion(+), 1 deletion(-)
root@git:~/game# git push -u origin master 
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 303 bytes | 303.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
To 10.0.0.200:BestCode/game.git3a515cf..5872f80  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.# 然后我們的Jenkins就會自動觸發

?

web服務器正常

?代碼也會自動掃描

[root@Jenkins ~]# date +%F-%H-%M
2025-02-19-14-48

?四、Jenkins的pipeline

1. Jenkins Pipeline 簡介

Jenkins Pipeline 是一種用于自動化構建、測試和部署的工具,基于聲明性或腳本式的語法,幫助開發團隊實現持續集成和持續交付 (CI/CD)。基于Java的動態編程groovy語言。
## 核心概念
- Pipeline:一系列自動化流程的定義,從代碼構建、測試到部署。
- Jenkinsfile:用來定義 Pipeline 的文件,通常存放在項目的根目錄下,支持兩種格式:聲明式和腳本式。

2.?Pipeline 類型

聲明式

1. 聲明式 Pipeline:- 提供了更簡潔和結構化的語法。- 示例:pipeline {agent anystages {stage('Build') {steps {echo 'Building...'}}stage('Test') {steps {echo 'Testing...'}}stage('Deploy') {steps {echo 'Deploying...'}}}}

?腳本式

2. 腳本式 Pipeline:- 更靈活,但語法較為復雜,適合需要自定義邏輯的場景。- 示例:node {stage('Build') {echo 'Building...'}stage('Test') {echo 'Testing...'}stage('Deploy') {echo 'Deploying...'}}

## 主要組成部分
- Agent:指定在哪個節點上執行 Pipeline(如 `any` 表示在任意可用的節點上執行)。
- Stages:定義多個階段,通常用于分隔不同的工作流程,如構建、測試、部署等。
- Steps:每個階段中的具體操作,可以是執行 shell 命令、構建工具或腳本等。
## 優勢
- 自動化:通過流水線自動化整個開發、構建、測試和部署過程。
- 可追蹤性:每個階段都有詳細日志,方便追蹤和排查問題。
- 版本控制:通過 `Jenkinsfile` 可以將流水線定義與代碼一同管理,支持版本控制。
## 總結
Jenkins Pipeline 是一種強大的工具,通過自動化的流程實現高效的持續集成與持續交付,幫助團隊更快地交付高質量的代碼。

3. 使用Jenkins-pipline項目?

?

?保存然后構建,構建完成之后鼠標落在綠色的方塊上可以看到具體的日志

?點擊旁邊的Blue Ocean可以看到具體的識圖

我們換一種方式重新定義

??然后我們去gitlab上面直接創建一個Jenkinsfile的文件

然后提交更改

我們可以看到存在了Jenkinsfile,然后我們再把倉庫的URL復制到Jenkins里面

完成之后保存即可

然后構建

4. 體驗Jenkins流水線語法轉換

當然Jenkins也給我們流水線的項目提供了語法參考與轉換

?

?

五、Java項目發布流程

apache-maven安裝包下載_開源鏡像站-阿里云

1.Maven介紹與安裝

Maven是一個項目管理和綜合工具。Maven提供給開發人員構建一個完整的生命周期框架。
開發團隊可以自動完成該項目的基礎設施建設,Maven使用標準的目錄結構和默認構建生命周期。
Apache的開源項目主要服務于JAVA平臺的構建、依賴管理、項目管理。
Project Object Model,項目對象模型。通過xml格式保存的pom.xml文件。該文件用于管理:源代碼、配置文
件、開發者的信息和角色、問題追蹤系統、組織信息、項目授權、項目的url、項目的依賴關系等等。該文件是由開
發維護,我們運維人員可以不用去關心。# 安裝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 ~]# ll /usr/local/
total 0
drwxr-xr-x 6 root root 99 Feb 19 16:59 apache-maven-3.3.9
。。。。
lrwxrwxrwx 1 root root 30 Feb 19 17:00 maven -> /usr/local/apache-maven-3.3.9/
[root@Jenkins ~]# tail -1 /etc/profile
export PATH="$PATH:/usr/local/sonar-scanner/bin:/usr/local/apache-maven-3.3.9/bin/"
[root@Jenkins ~]# source /etc/profile
[root@Jenkins ~]# 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:/usr/local/apache-maven-3.3.9/bin/:/usr/local/sonar-scanner/bin:/usr/local/apache-maven-3.3.9/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/apache-maven-3.3.9
Java version: 11.0.25, vendor: BiSheng
Java home: /usr/lib/jvm/java-11-openjdk-11.0.25.9-3.p01.ky10.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.19.90-52.22.v2207.ky10.x86_64", arch: "amd64", family: "unix"# 上傳一個簡單的java項目包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
[root@Jenkins ~/hello-world-war]# mvn package
....
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:01 min
[INFO] Finished at: 2025-02-19T17:05:50+08:00
[INFO] Final Memory: 14M/83M
[INFO] ------------------------------------------------------------------------進入目錄執行打包命令
validate(驗證): 驗證項目正確,并且所有必要信息可用。
compile(編譯): 編譯項目源碼
test(測試): 使用合適的單元測試框架測試編譯后的源碼。
package(打包): 源碼編譯之后,使用合適的格式(例如JAR格式)對編譯后的源碼進行打包。
integration‐test(集成測試): 如果有需要,把包處理并部署到可以運行集成測試的環境中去。
verify(驗證): 進行各種測試來驗證包是否有效并且符合質量標準。
install(安裝): 把包安裝到本地倉庫,使該包可以作為其他本地項目的依賴。
deploy(部署): 在集成或發布環境中完成,將最終軟件包復制到遠程存儲庫,以與其他開發人員和項目共享。
mvn clean (清除) : 清除上次編譯的結果
mvn package ‐Dmaven.test.skip=true 跳過測試用例# web02部署tomcat
[root@web02 ~]# netstat -nltup|grep 8080
tcp6       0      0 :::8080                 :::*                    LISTEN      3416/java           
# 將war包拷貝tomcat的站點目錄下
[root@jenkins:hello-world-war]#scp target/hello-world-war-1.0.0.war 10.0.0.8:/soft/tomcat/webapps/ROOT/# 解壓war包 啟動tomcat訪問
[root@web02:ROOT]#unzip hello-world-war-1.0.0.war
[root@web02:ROOT]#/usr/local/tomcat/bin/startup.sh# 瀏覽器打開訪問: 10.0.0.8:8080

?2.將默認的maven倉庫源修改為國內的

[root@Jenkins /usr/local/maven/conf]# pwd
/usr/local/maven/conf
[root@Jenkins /usr/local/maven/conf]# grep -n -C 5 aliyun settings.xml 
148-     | Specifies a repository mirror site to use instead of a given repository. The repository that
149-     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
150-     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
151-     |
152-    <mirror>
153:      <id>nexus-aliyun</id>
154-      <mirrorOf>*</mirrorOf>
155:      <name>Nexus aliyun</name>
156:      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
157-    </mirror>
158-     -->
159-  </mirrors>
160-
161-  <!-- profiles我們再次執行一下mvn package查看倉庫是否來源阿里云
[root@Jenkins ~/hello-world-war]# pwd
/root/hello-world-war
[root@Jenkins ~/hello-world-war]# mvn clean
[root@Jenkins ~/hello-world-war]# mvn package注意:如果公司部分java代碼 不需要配置nexus私服,只需要將maven配置文件倉庫指向阿里云倉庫
如果公司是重java項目,則需要搭建私有的倉庫nexus

3.集成到jenkins

我們的gitlab也創建一個新的Java倉庫,把我們的hello-world代碼上傳進去

?我們將代碼目錄上傳到倉庫中,在此之前,優化一下我們的倉庫

[root@Jenkins ~/hello-world-war]# ll -a
total 16
drwxr-xr-x  6 root root  105 Feb 19 17:17 .
dr-xr-x--- 13 root root 4096 Feb 19 17:15 ..
drwxr-xr-x  2 root root   29 May 23  2014 dist
drwxr-xr-x  8 root root  166 Oct 15  2019 .git
-rw-r--r--  1 root root   42 May 23  2014 .gitignore
-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
drwxr-xr-x  4 root root   90 Feb 19 17:17 target
[root@Jenkins ~/hello-world-war]# mvn clean
[root@Jenkins ~/hello-world-war]# git remote add origin git@10.0.0.200:BestCode/java.git
fatal: remote origin already exists.
[root@Jenkins ~/hello-world-war]# git remote remove origin 
[root@Jenkins ~/hello-world-war]# git remote add origin git@10.0.0.200:BestCode/java.git
[root@Jenkins ~/hello-world-war]# git config --global user.name "caofacan"
[root@Jenkins ~/hello-world-war]# git config --global user.email "cao@cfc.com"
[root@Jenkins ~/hello-world-war]# git add .
[root@Jenkins ~/hello-world-war]# git commit -m "first java code"
[master 7657698] first java code2 files changed, 3 insertions(+), 3 deletions(-)
[root@Jenkins ~/hello-world-war]# git push -u origin master 

回到Jenkins接入 Maven

?

?完成之后保存構建即可

進階一下,我們執行shell自動化拉取部署Java代碼,然后構建

scp target/*.war 10.0.0.8:/soft/tomcat/webapps/
ssh 10.0.0.8 "cd /soft/tomcat/webapps/ && mkdir java_${BUILD_ID}"
ssh 10.0.0.8 "cd /soft/tomcat/webapps/ && unzip *.war -d java_${BUILD_ID} && rm -rf *.war"
ssh 10.0.0.8 "cd /soft/tomcat/webapps/ && rm -rf ROOT && ln -s 	java_${BUILD_ID} ROOT" 
ssh 10.0.0.8 "/soft/tomcat/bin/shutdown.sh && sleep 1 && /soft/tomcat/bin/startup.sh"

?

[root@web02 /soft/tomcat/webapps]# ll
total 4
drwxr-x--- 16 root root 4096 Feb 19 17:08 docs
drwxr-x---  7 root root   99 Feb 19 17:08 examples
drwxr-x---  6 root root   79 Feb 19 17:08 host-manager
drwxr-xr-x  2 root root    6 Feb 19 17:45 java_2
drwxr-xr-x  4 root root   54 Feb 19 17:49 java_3
drwxr-x---  6 root root  114 Feb 19 17:08 manager
lrwxrwxrwx  1 root root    6 Feb 19 17:49 ROOT -> java_3web02直接訪問也沒有問題

4. 配置nexus私服

部署私服 xenus 下載https://www.sonatype.com/download‐oss‐sonatype
配置倉庫兩個選項
1、項目下的pom.xml配置、只生效當前的項目
2、在maven配置全局所有項目生效
3.上傳JDK和nexus的包
[root@nexus ~]# ll
total 286068
-rw-r--r-- 1 root root 170023183 Sep 27 17:19 jdk-8u181-linux-x64.rpm
-rw-r--r-- 1 root root 122904706 Sep 27 17:19 nexus-3.13.0-01-unix.tar.gz
[root@nexus ~]# rpm -ivh jdk-8u181-linux-x64.rpm 
[root@nexus ~]# tar xf nexus-3.13.0-01-unix.tar.gz
[root@nexus ~]# mv nexus-3.13.0-01 /usr/local/nexus
[root@nexus ~]# /usr/local/nexus/bin/nexus start
WARNING: ************************************************************
WARNING: Detected execution as "root" user.  This is NOT recommended!
WARNING: ************************************************************
Starting nexus# 瀏覽器訪問$IP:8081端口

?用戶名/密碼: admin/admin123

配置nexus倉庫指向阿里云

?修改jenkins編譯指向nexus私服

[root@jenkins:~]#cd /usr/local/maven/conf/
[root@jenkins:conf]#mv settings.xml settings.xml.bak
上傳配置文件修改配置文件信息:
[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>

再次mvn package

Downloaded: http://10.0.0.204:8081/repository/maven-public/commons-io/commons-io/2.6/commons-io-2.6.jar (0 B at 0.0 KB/sec)
[INFO] Packaging webapp
[INFO] Assembling webapp [hello-world-war] in [/root/hello-world-war/target/hello-world-war-1.0.0]
[INFO] Processing war project
[INFO] Copying webapp resources [/root/hello-world-war/src/main/webapp]
[INFO] Building war: /root/hello-world-war/target/hello-world-war-1.0.0.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:49 min
[INFO] Finished at: 2025-02-19T20:14:01+08:00
[INFO] Final Memory: 13M/101M
[INFO] ------------------------------------------------------------------------

就是先從私服下載,沒有再去阿里云倉庫。

還是那句話 ------> 注意:如果公司部分java代碼 不需要配置nexus私服,只需要將maven配置文件倉庫指向阿里云倉庫 如果公司是重java項目,則需要搭建私有的倉庫nexus.

六、配置Jenkins分布式(拓展)

如果項目需要定期集成,同時每次集成都需要較長時間。如果都運行在master服務器上,會消耗過多資源,導致其他項
目擱置無法集成,這時就需要在建立多臺設備,并配置作為slave機器來為master提供負載服務。

1. 部署分布式服務器

1.找一臺或者目前已有的服務器配置為slave端
[root@lb01 ~]# rpm -ivh jdk-17_linux-x64_bin.rpm 
2. 配置Jenkins與從節點免密鑰
[root@Jenkins ~]# ssh-copy-id 10.0.0.5
既然是從節點,那么配置就要和主節點相同
[root@jenkins:~]#scp -r /usr/local/sonar-scanner 10.0.0.5:/usr/local/
[root@jenkins:~]#scp -r /usr/local/maven/ 10.0.0.5:/usr/local/
3.jenkins服務端添加從節點lb01
PS: 把lb01的公鑰上傳至gitlab 擁有下載代碼的權限
點擊系統管理->節點管理->新建節點
[root@lb01 ~]# ssh-keygen 
[root@lb01 ~]# cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCukeQGhURJgDbIp189+pWRukYoLY34YtxFJ9ffjOcj79DrXSqvc2eudlfESVidnAcxi1+09+kZwiWgWBHGzMvAcWpf6BZQnPulWfdqJHUZvCL1OBKEiFenN+Dt9F/89dIvLJBjHyXzw63F8tzrZ3NjUEMhMIe3hRjrY+l9ZpX6xE+GfWY1G+1tZx+2LU9E7WJmdd+eOBXHO/zBPjx7EN3jLZbcx8X2pIUA60Czj0VD8sLWEWMFfeCaD55X+45/yDA47PNCwyuyihM+Bdr5vpG647RB8UwcyDzgfoGAFGnQ8yCKjCOjYCnxPe1eS/O5nGEJ4dqmVdAFLz2uDRj/cDczggvqoOPvlOQ700thsZIxkXRDU1J3Dq0SU5j7ZUqwvuiwr8+dwDKip/M8puB0BFVUVF/cN2p3/k/owx0d1omHU59JJGBmaPIT+uNGwA1fiE5NBsSpH+QUg0y8g0KIl8zXw/qiz61/mQ5kd/3qHXqsYXipgxOYt1CsgbxOKQPg2iM= root@lb01
[root@lb01 ~]# umount /tmp 
[root@lb01 ~]# systemctl mask tmp.mount 
Created symlink /etc/systemd/system/tmp.mount → /dev/null.Jenkins的主節點也最好做一個
[root@Jenkins ~]# umount /tmp 
umount: /tmp: target is busy.
[root@Jenkins ~]# umount -fl /tmp 
[root@Jenkins ~]# systemctl mask tmp.mount 
Created symlink /etc/systemd/system/tmp.mount → /dev/null.
[root@Jenkins ~]# systemctl restart jenkins.service

2. jenkins添加從節點lb01

?注意,這里最好取消機器的tmp的掛載,Jenkins默認將tmp作為機器的儲存。(kylin系統的老毛病,如果是其他系統默認不會掛載/tmp)

?

3. 配置工具的家目錄位置

保存后發現

4. 測試

多點幾個項目同時執行

?七、總結

本文詳細介紹了如何構建一個完整的CI/CD流程,整合GitLab、Jenkins、SonarQube和Nexus四大工具。首先在Ubuntu系統上安裝配置GitLab作為代碼倉庫,并演示了代碼提交與權限控制流程。接著安裝Jenkins實現自動化構建與部署,通過Webhook實現代碼提交自動觸發。然后部署SonarQube進行代碼質量檢測,并集成到Jenkins流程中。針對Java項目,介紹了Maven構建工具的使用,以及Nexus私服的配置方法。最后拓展了Jenkins分布式構建的實現,通過添加從節點提升構建效率。整個流程覆蓋了代碼版本控制、自動化構建、代碼質量檢測、依賴管理和分布式構建等關鍵環節,為企業提供了一個高效、可靠的持續集成與持續交付解決方案。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/88944.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/88944.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/88944.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[密碼學實戰]密評相關題庫解析

[密碼學實戰]密評相關題庫解析 一、背景 依據《密碼法》第二十二條&#xff0c;關鍵信息基礎設施&#xff08;關基&#xff09;運營者必須開展商用密碼應用安全性評估&#xff0c;且需定期進行&#xff08;不少于每年一次&#xff09;。 二、核心解析 2.1 測評標準框架&#x…

谷歌開源庫gflags詳細說明

目錄 一.gflags 介紹 二.gflags安裝 三.gflags使用 1.包含頭文件 2.定義參數 3.訪問參數 4.不同文件訪問參數 5.初始化所有參數 6.運行參數設置 7.配置文件的使用 8.特殊參數標識 四.總結 一.gflags 介紹 gflags 是 Google 開發的一個開源庫&#xff0c;用于 C 應用…

Python爬蟲實戰:研究XlsxWriter 庫相關技術

1. 研究背景與意義 1.1 網絡爬蟲技術價值 網絡爬蟲作為數據采集的核心工具,在金融、醫療、教育等領域發揮關鍵作用。據 Statista 數據顯示,2025 年全球大數據市場規模預計達 3250 億美元,高效的數據獲取能力成為企業核心競爭力。Python 以其 80% 的市場占有率成為爬蟲開發首…

ThreadLocal內部結構深度解析(Ⅰ)

目錄 使用ThreadLocal 例子 內部結構分析 源碼解析 圖示詳解 ThreadLocal是Java中一個非常重要且常用的線程局部變量工具類&#xff0c;它使得每個線程可以獨立地持有自己的變量副本&#xff0c;而不是共享變量&#xff0c;解決了多線程環境下變量共享的線程安全問題。下面我…

Python 數據挖掘之數據探索

在數據挖掘的流程中&#xff0c;數據探索是非常關鍵的第一步&#xff0c;它能幫助我們深入了解數據的特點&#xff0c;為后續的預處理和模型構建打下堅實的基礎。我們主要圍繞四個方面展開&#xff1a;數據對象與特征、數據統計描述、數據可視化以及相關性和相似性度量。一、數…

高并發點贊場景Synchronized、AtomicLong、LongAdder 和 LongAccumulator性能分析

在高并發點贊場景中&#xff0c;我們需要一個高效、線程安全的計數器來記錄點贊數。synchronized、AtomicLong、LongAdder 和 LongAccumulator 都是 Java 中用于實現原子操作的類&#xff0c;但它們的性能在高并發下差異顯著。性能主要取決于線程競爭程度&#xff1a;競爭越高&…

postgreSQL的sql語句

目錄 一&#xff1a;前提準備1.postgreSQL的安裝可以參考我下面一片文章&#xff1a; 二&#xff1a;SQL語句 1.相同點&#xff1a;支持標準sql類型 2.參考詳細學習地址&#xff1a; 3.postgresql與mysql的不同點 一&#xff1a;前提準備 1.postgreSQL的安裝可以參考我下面…

vue3 JavaScript 數據累加 reduce

在Vue 3中&#xff0c;你可以使用JavaScript的reduce方法來處理數據累加。reduce方法通常用在數組上&#xff0c;它將數組中的每個元素通過一個累加器函數&#xff08;accumulator&#xff09;從左到右累積&#xff0c;最終生成一個單一的值。這在計算總和、累加值等場景中非常…

史上最清楚!讀者,寫者問題(操作系統os)

讀者-寫者問題是另一個里程碑式的同步互斥問題。它比生產者-消費者更復雜&#xff0c;因為它引入了不對稱的訪問權限&#xff1a;讀者和讀者之間是共享的&#xff0c;但寫者和任何人&#xff08;包括讀者和其他寫者&#xff09;之間都是互斥的。我們用一個生動的比喻來解析這個…

使用Starrocks替換Clickhouse的理由

背景 Starrocks和clickhouse都是非常優秀的OLAP數據庫&#xff0c;那么什么情況下使用clickhouse&#xff0c;什么場景下使用starrocks呢&#xff0c;本文就簡單列舉一下他們的優缺點 理由 首先兩者都是列存儲&#xff0c;并且都實現了列壓縮&#xff0c;所以從存儲中兩者的壓縮…

Mybatis 兩級緩存可能導致的問題

Mybatis 兩級緩存可能導致的問題兩級緩存簡介一級緩存 localCache效果開關二級緩存兩級緩存可能導致的問題分布式環境下查詢到過期數據事務隔離級別失效讀已提交失效讀未提交失效總結兩級緩存簡介 一級緩存 localCache 效果 一級緩存是 session 或者說事務級別的&#xff0c…

vue3+uniapp 使用vue-plugin-hiprint中實現打印效果

前言&#xff1a; vue3uniapp 使用vue-plugin-hiprint中實現打印效果 官網地址&#xff1a;gitee https://gitee.com/ccsimple/vue-plugin-hiprinthttps://gitee.com/ccsimple/vue-plugin-hiprint 實現效果&#xff1a; 預覽打印內容&#xff1a; 實現步驟&#xff1a; 1、安…

【elementUI踩坑記錄】解決 el-table 固定列 el-table__fixed 導致部分滾動條無法拖動的問題

目錄一、問題背景二、 問題現象三、核心原因四、解決辦法增強方案&#x1f680;寫在最后一、問題背景 在使用 Element UI 的 el-table 組件時&#xff0c;固定列功能雖然實用&#xff0c;但會引發滾動條交互問題&#xff1a; 固定列區域懸浮顯示滾動條但無法正常拖動滾動條 …

【機器人編程基礎】python文件的打開和關閉

文件的打開和關閉 在Python中,文件操作是一項基本而重要的任務,涉及到打開、讀取、寫入、關閉文件等操作。正確地管理文件對于數據持久化、輸入輸出處理等至關重要。下面將詳細解釋如何在Python中打開和關閉文件,并提供相應的代碼示例。 文件打開 在Python中,可以使用內…

ShenYu實戰、問題記錄

概述 一款高性能的國產的Apache開源API網關&#xff0c;官方文檔。 在ShenYu v2.6.1, ShenYu注冊中心只支持http類型&#xff0c;中間件注冊類型已經被移除。 所以&#xff0c;請使用http注冊類型來注冊你的服務。不是微服務注冊中心&#xff0c;它只是將元數據、選擇器數據、…

走近科學IT版:EasyTire設置了ip,但是一閃之后就變回到原來的dhcp獲得的地址

EasyTier 是一款簡單、安全、去中心化的內網穿透和異地組網工具&#xff0c;適合遠程辦公、異地訪問、游戲加速等多種場景。無需公網 IP&#xff0c;無需復雜配置&#xff0c;輕松實現不同地點設備間的安全互聯。 上次實踐的記錄&#xff1a;適合遠程辦公、異地訪問的EasyTier…

rk3588平臺USB 3.0 -OAK深度相機適配方法

目錄 文件更改記錄表 1、usb規則添加 2、拉取相關依賴 3、安裝python3、安裝pip 4、安裝依賴 5、安裝ffmeg 6、攝像頭功能測試 7、將視頻拷貝到U盤查看 1、usb規則添加 由于OAK是USB設備,因此為了在使用 udev 工具的系統上與之通信, 您需要添加udev規則以使…

工廠模式總結

工廠模式1. 簡單工廠模式&#xff08;Simple Factory&#xff09; 核心思想 定義一個工廠類&#xff0c;根據輸入參數創建不同的具體對象。客戶端不直接調用具體類的構造函數&#xff0c;而是通過工廠類獲取對象。 示例代碼 #include <iostream> #include <memory>…

MySQL的三種安裝方式(mis、zip、yum)

目錄 2.0數據庫安裝 2.1windows上.mis格式 環境準備 MySQL的安裝 環境配置&#xff08;非必要&#xff09; 2.2windows上.zip格式安裝 環境準備 配置文件的內容 MySQL的安裝 附錄可能出現問題 圖形工具遠程連接數據庫 2.3Linux上安裝yum包 環境準備 過程命令 My…

串口學習和藍牙通信HC05(第八天)

&#x1f468;?&#x1f4bb;個人主頁&#xff1a;開發者-削好皮的Pineapple! &#x1f468;?&#x1f4bb; hello 歡迎 點贊&#x1f44d; 收藏? 留言&#x1f4dd; 加關注?! &#x1f468;?&#x1f4bb; 本文由 削好皮的Pineapple! 原創 &#x1f468;?&#x1f4b…