
實(shí)驗(yàn)中相應(yīng)服務(wù)的安裝步驟略
準(zhǔn)備相關(guān)服務(wù)器,并實(shí)現(xiàn)各個(gè)服務(wù)器的DNS解析
[root@ubuntu2204 ~]#cat /etc/bind/hailin.org.zone
$TTL 1D
@ IN SOA master admin (
1 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS master
master A 10.0.0.201
gitlab A 10.0.0.179
jenkins A 10.0.0.171
harbor A 10.0.0.201
sonarqube A 10.0.0.178
安裝 GitLab 和準(zhǔn)備項(xiàng)目
安裝Gitlab ,安裝步驟略,并準(zhǔn)備JAVA項(xiàng)目
#可以從gitee碼云導(dǎo)入項(xiàng)目代碼
https://gitee.com/lbtooth/spring-boot-helloworld.git
?文章來源地址http://www.zghlxwxcb.cn/news/detail-785566.html
#生成如下項(xiàng)目倉庫路徑
http://gitlab.hailin.org/example/spring-boot-helloworld.git

安裝 Harbor 并配置 Jenkins 連接Harbor
安裝略
創(chuàng)建名為example的項(xiàng)目

在 Jenkins 服務(wù)器安裝Docker,并配置連接 Harbor
#在jenkins服務(wù)器安裝Docker
[root@jenkins ~]#apt update && apt install docker.io
?
[root@jenkins ~]#vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://si7y70hh.mirror.aliyuncs.com"],
"insecure-registries": ["harbor.hailin.org"]
}
?
[root@jenkins ~]#systemctl restart docker
?
[root@jenkins ~]#docker info
Client:
Context: default
Debug Mode: false
?
Server:
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 3
Server Version: 20.10.21
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd
Cgroup Version: 2
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version:
runc version:
init version:
Security Options:
apparmor
seccomp
Profile: default
cgroupns
Kernel Version: 5.15.0-60-generic
Operating System: Ubuntu 22.04.1 LTS
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 3.799GiB
Name: jenkins
ID: 5SU7:WISV:KNTZ:LJ7Y:VCTM:6K7Y:VX2V:SS4U:SDDE:32SX:OMWT:HUEL
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
harbor.hailin.org
127.0.0.0/8
Registry Mirrors:
https://si7y70hh.mirror.aliyuncs.com
Live Restore Enabled: false
?
#測試登錄harbor,登錄后可在/root/.docker/config.json下存儲(chǔ)登錄信息
[root@jenkins sonarqube-scanner]#docker login harbor.hailin.org
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
?
Login Succeeded
?
[root@jenkins ~]#cat /root/.docker/config.json
{
"auths": {
"10.0.0.201": {
"auth": "YWRtaW46MTIzNDU2"
},
"harbor.hailin.org": {
"auth": "YWRtaW46MTIzNDU2"
}
}
}
安裝Jenkins并安裝相關(guān)插件
安裝Jenkins步驟略
安裝相關(guān)插件和軟件:
#GitLab
#pipeline
#企業(yè)微信通知插件Qy Wechat Notification



在 Jenkins 創(chuàng)建憑據(jù)連接 Harbor

在 Jenkins 安裝Maven 工具
[root@jenkins ~]#apt update;apt install maven -y
?
#優(yōu)化編譯環(huán)境,提升編譯速度
[root@jenkins ~]#vim /etc/maven/settings.xml
.......................
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
........................
?
#查看Maven信息,記錄執(zhí)行結(jié)果供后續(xù)與Jenkins對接使用
[root@jenkins ~]#mvn -version
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 11.0.17, vendor: Ubuntu, runtime: /usr/lib/jvm/java-11-openjdk-amd64
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-60-generic", arch: "amd64", family: "unix"
在Jenkins是配置Maven環(huán)境
Mange Jenkins --- Global Tool configuration -- Maven
根據(jù)上面的 mvn -version 命令結(jié)果,填寫 MAVEN_HOME 的路徑


在 Jenkins 創(chuàng)建連接 GitLab 的憑據(jù)

在 Jenkins上修改 Docker 的 socket 文件權(quán)限
默認(rèn)Jenkins以Jenkins用戶啟動(dòng),無法訪問Docker,構(gòu)建時(shí)會(huì)提示如下錯(cuò)誤:

解決方式
#other用戶無docker任何權(quán)限,所以導(dǎo)致jenkins權(quán)限啟動(dòng)的jenkins服務(wù)無權(quán)限訪問Docker
[root@jenkins ~]#ll /var/run/docker.sock
srw-rw---- 1 root docker 0 2月 1322:15 /var/run/docker.sock=
?
[root@jenkins ~]#id jenkins
用戶id=109(jenkins) 組id=113(jenkins) 組=113(jenkins)
?
#解決方式一,將jenkins賬號添加至docker組
[root@ubuntu2204 ~]#usermod -G docker jenkins
?
[root@ubuntu2204 ~]#id jenkins
用戶id=113(jenkins) 組id=118(jenkins) 組=118(jenkins),119(docker)
?
[root@ubuntu2204 ~]#systemctl restart jenkins
?
#解決方式二,修改jenkins以root權(quán)限啟動(dòng)
[root@jenkins ~]#vim /lib/systemd/system/jenkins.service
.................
#User=jenkins
User=root
#Group=jenkins
Group=root
.................
安裝 SonarQube 并創(chuàng)建用戶和令牌
安裝 Sonarqueb Server步驟略, ,并在 Sonarqube 創(chuàng)建用戶并授權(quán)




更新令牌,用于后續(xù)與jenkins對接



記錄令牌用于后續(xù)使用,此token 只能復(fù)制一次
squ_791bc70e1771a1d52a10b8b3cbb6d430111e3037

給與新建用戶相應(yīng)權(quán)限,這里給用戶分配置"執(zhí)行分析"和"置備項(xiàng)目"權(quán)限

在 SonarQube 添加 Jenkins 的回調(diào)接口
在SonarQube上添加webhook(網(wǎng)絡(luò)調(diào)用),以便于Jenkins通過SonarQube Quality Gate插件調(diào)用其"質(zhì)量閾"信息,決定是否繼續(xù)執(zhí)行下面的構(gòu)建步驟
配置 --- 網(wǎng)絡(luò)調(diào)用 webhook --- 創(chuàng)建


輸入名稱和下面 Jenkins的URL地址注意: 密碼防止攻擊使用, 可以隨意輸入注意:此處的密碼沒有復(fù)雜度和長度要求,但建議使用符合安全的密碼
http://jenkins.hailin.org:8080/sonarqube-webhook
?
#使用openssl生成secret,長度3的倍數(shù)
[root@jenkins ~]#openssl rand -base64 21
kzwrC4L+TkUDXSTpBZKzIcRQxn5T


Jenkins安裝SonarQube Scanner 插件
Jenkins—系統(tǒng)管理--插件管理
安裝插件 SonarQube Scanner


創(chuàng)建Jenkins訪問 Sonarqube的令牌憑據(jù)
用前面小節(jié)生成的Sonarqube令牌,在Jenkins 創(chuàng)建憑據(jù)
secret:squ_791bc70e1771a1d52a10b8b3cbb6d430111e3037
ID:sonarqube-jenkins-token




在 Jenkins 上配置系統(tǒng)的 SonarQube 服務(wù)器信息
Manage Jenkins -- Configure System
注意:Name名稱區(qū)分大小寫
注意:http://sonarqube.hailin.org:9000 地址最后不能加/
否則會(huì)報(bào)錯(cuò)誤 hudson.remoting.ProxyException: net.sf.json.JSONException: Invalid JSON String



注意:地址后面別有空格或者/,否則會(huì)導(dǎo)致異常

在 Jenkins 上安裝 SonarQube Scanner
[root@jenkins ~]#cd /usr/local/src
?
[root@jenkins ~]#wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.8.0.2856-linux.zip
?
[root@jenkins src]#ls
sonar-scanner-cli-4.8.0.2856-linux.zip
?
[root@jenkins src]#unzip sonar-scanner-cli-4.8.0.2856-linux.zip
?
[root@jenkins src]#ln -s /usr/local/src/sonar-scanner-4.8.0.2856-linux/ /usr/local/sonar-scanner
?
[root@jenkins src]#ln -s /usr/local/sonar-scanner/bin/sonar-scanner /usr/bin/
?
#配置sonar-scanner連接sonarqube服務(wù)器
[root@jenkins src]#vim /usr/local/sonar-scanner/conf/sonar-scanner.properties
?
#指向sonarqube服務(wù)器的地址和端口
sonar.host.url=http://sonarqube.hailin.org:9000
sonar.sourceEncoding=UTF-8
?
[root@jenkins ~]#sonar-scanner -v
INFO: Scanner configuration file: /usr/local/src/sonar-scanner-4.8.0.2856-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.8.0.2856
INFO: Java 11.0.17 Eclipse Adoptium (64-bit)
INFO: Linux 5.15.0-60-generic amd64
準(zhǔn)備微信機(jī)器人
注冊企業(yè)微信添加WebHook機(jī)器






#復(fù)制創(chuàng)建機(jī)器人生成的webhook地址,用于后續(xù)jenkins的對接
https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3ec91b6e-d5fc-46dd-8464-12fc730b6c9f
準(zhǔn)備Pipeline部署腳本
pipeline {
agent any
tools {
maven 'maven-3.6.3'
}
environment {
codeRepo="http://gitlab.hailin.org/example/spring-boot-helloworld.git"
harborServer='harbor.hailin.org'
projectName='spring-boot-helloworld'
imageUrl="${harborServer}/example/${projectName}"
imageTag="${BUILD_ID}"
}
stages {
stage('Source') {
steps {
git branch: 'main', credentialsId: 'gitlab-zhao', url: "${codeRepo}"
}
}
stage('Build') {
steps {
sh'mvn -B -DskipTests clean package'
}
}
stage('Test') {
steps {
sh'mvn test'
}
}
stage("SonarQube Analysis") {
steps {
withSonarQubeEnv('SonarQube-Server') {
sh'mvn sonar:sonar'
}
}
}
stage("Quality Gate") {
steps {
timeout(time: 30, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
stage('Build Docker Image') {
steps {
sh'docker image build . -t "${imageUrl}:${imageTag}"'
// input(message: '鏡像已經(jīng)構(gòu)建完成,是否要推送?')
}
}
stage('Push Docker Image') {
steps {
withCredentials([usernamePassword(credentialsId: 'harbor-user-credential', passwordVariable: 'harborPassword', usernameVariable: 'harborUserName')]) {
sh"docker login -u ${env.harborUserName} -p ${env.harborPassword} ${harborServer}"
sh"docker image push ${imageUrl}:${imageTag}"
}
}
}
}
post{
success{
qyWechatNotification failNotify: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3ec91b6e-d5fc-46dd-8464-12fc730b6c9f'
}
failure{
qyWechatNotification failNotify: true, webhookUrl: 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3ec91b6e-d5fc-46dd-8464-12fc730b6c9f'
}
}
}
創(chuàng)建任務(wù)使用 Pipeline Script
根據(jù)前面的相應(yīng)配置創(chuàng)建Pipeline風(fēng)格的任務(wù)
直接使用Pipeline Script
任務(wù)名:pipeline-sonarqube-docker-spring-boot-helloWorld


安裝Pipeline Stage View 插件實(shí)現(xiàn)階段化視圖


執(zhí)行構(gòu)建驗(yàn)證結(jié)果

增加自動(dòng)觸發(fā)功能
Jenkins項(xiàng)目中配置自動(dòng)出發(fā)構(gòu)建功能


url地址:http://10.0.0.171:8080/project/pipeline-sonarqube-docker-spring-boot-helloWorld
token:0189b45ee628041d29b2015ccf8fc88d
Gitlab上設(shè)置webhook



測試推送事件


調(diào)整代碼實(shí)現(xiàn)代碼自動(dòng)上傳執(zhí)行構(gòu)建
#開發(fā)設(shè)備上調(diào)整代碼
[root@SonarQube-Server spring-boot-helloworld]#ls
deploy Jenkinsfile pom.xml sonar-project.properties
Dockerfile LICENSE README.md src
[root@SonarQube-Server spring-boot-helloworld]#vim src/
main/ test/
[root@SonarQube-Server spring-boot-helloworld]#vim sonar-project.properties
[root@SonarQube-Server spring-boot-helloworld]#git commit -m 'modify sonar-project.properties'
位于分支 main
您的分支與上游分支 'origin/main' 一致。
?
尚未暫存以備提交的變更:
(使用 "git add <文件>..." 更新要提交的內(nèi)容)
(使用 "git restore <文件>..." 丟棄工作區(qū)的改動(dòng))
修改: sonar-project.properties
?
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@SonarQube-Server spring-boot-helloworld]#git add .
[root@SonarQube-Server spring-boot-helloworld]#git commit -m 'modify sonar-project.properties'
[main e9b8191] modify sonar-project.properties
1 file changed, 1 insertion(+), 1 deletion(-)
[root@SonarQube-Server spring-boot-helloworld]#git push
Username for 'http://gitlab.hailin.org': zhao
Password for 'http://zhao@gitlab.hailin.org':
枚舉對象中: 5, 完成.
對象計(jì)數(shù)中: 100% (5/5), 完成.
使用 2 個(gè)線程進(jìn)行壓縮
壓縮對象中: 100% (3/3), 完成.
寫入對象中: 100% (3/3), 321 字節(jié) | 321.00 KiB/s, 完成.
總共 3(差異 2),復(fù)用 0(差異 0),包復(fù)用 0
To http://gitlab.hailin.org/example/spring-boot-helloworld.git
d69bcfc..e9b8191 main -> main文章來源:http://www.zghlxwxcb.cn/news/detail-785566.html
?



到了這里,關(guān)于DevOps之Jenkins 集成 SonarQube 、Gitlab實(shí)現(xiàn)代碼自動(dòng)提交質(zhì)量檢測的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!