一、Jenkins項目構(gòu)建類型
1.1 Jenkins項目構(gòu)建類型介紹
Jenkins中自動構(gòu)建項目的類型有很多,常用的有如下的三種:
- 自由風(fēng)格軟件項目(FreeStyle Project)
- Maven項目(Maven Project)
- 流水線項目(Pipeline Project)
每種類型的構(gòu)建其實都可以完成一樣的構(gòu)建過程和結(jié)果,只是在操作方式、靈活度等方面有所區(qū)別,在實際開發(fā)中可以根據(jù)自己的需求和習(xí)慣靈活選擇(強烈推薦流水線類型,因為靈活度非常高)。
1.2 自由風(fēng)格項目構(gòu)建
1.2.1 安裝Publish over SSH插件

1.2.2 配置Publish over SSH
-
Manage Jenkins–>Configure System。
-
找到Publish over SSH,新增SSH Servers:
-
添加遠程服務(wù)器的配置信息:
目的
- 這里的遠程服務(wù)器密碼便是登錄該服務(wù)器的root密碼。
- 這一步驟,就是配置我們將要發(fā)布遠程服務(wù)器的配置信息。
- remote directory:配置了我們項目打包后,發(fā)布遠程服務(wù)器的文件目錄,不填,就默認發(fā)布在登錄用戶的根目錄下。
1.2.3 創(chuàng)建項目

1.2.4 配置源碼管理,從Gitlab上拉取代碼

目的:配置項目拉取地址和憑證
1.2.5 編譯打包
- 構(gòu)建–>添加構(gòu)建步驟–>Executor Shell。
echo "構(gòu)建開始"
mvn clean install -Dmaven.test.skip=true
echo "構(gòu)建成功"

目的:將拉取到的項目,通過maven工具打包安裝
1.2.6 部署
將SpringBoot生成的jar包發(fā)送到遠程服務(wù)器。
在遠程服務(wù)器192.168.18.102新建stop.sh和start.sh文件:
cd /usr/local
vim stop.sh
#!/bin/bash
echo "stop SpringBoot BAppApiServerApplication"
pid=`ps -ef | grep java | grep -v grep | awk '{print $2}'`
echo “舊應(yīng)用進程id:$pid”
if [ -n "$pid" ]
then
kill -9 $pid
rm -rf springboot2-1.0.jar
fi
vim start.sh
#!/bin/bash
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.9.11-2.el7_9.x86_64
echo ${JAVA_HOME}
echo "授權(quán)當(dāng)前用戶"
chmod 777 /usr/local/springboot2-1.0.jar
echo "執(zhí)行...."
cd /usr/local/
nohup ${JAVA_HOME}/bin/java -jar springboot2-1.0.jar > /dev/null &
echo "啟動成功"
目的:在遠程服務(wù)器上配置好停止和啟動項目的腳本。
-
構(gòu)建后操作–>Send Build artifacts over SSH:
cd /usr/local
chmod 777 *.sh
./stop.sh
./start.sh
目的:配置發(fā)布遠程服務(wù)器的相關(guān)信息,發(fā)布到的目標路徑,以及發(fā)布后要執(zhí)行的腳本。
-
點擊Build Now即可。
可能會出現(xiàn)的錯誤,若沒有出錯,可以忽略不看
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from ssh://git@gitlab.lucy.com:2222/lucy/test.git
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:888)
at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1155)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1186)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:120)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:90)
at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:77)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1$1.call(SynchronousNonBlockingStepExecution.java:50)
at hudson.security.ACL.impersonate(ACL.java:290)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1.run(SynchronousNonBlockingStepExecution.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --progress ssh://git@gitlab.lucy.com:2222/lucy/test.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout:
stderr: Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2016)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:1735)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$300(CliGitAPIImpl.java:72)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:420)
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:886)
... 13 more
解決方案
刪除Jenkins workspace下的工程后重新構(gòu)建。
1.3 Maven項目構(gòu)建
- Maven項目構(gòu)建和自由風(fēng)格項目構(gòu)建差不多,唯一不同的是,只能只用Maven來構(gòu)建項目。
- 需要安裝Maven Integration插件。

-
使用Maven項目構(gòu)建的方式和自由風(fēng)格項目構(gòu)建的方式類似,只是在構(gòu)建的時候強制使用Maven工具來構(gòu)建項目而已。
1.4 Pipeline流水線項目構(gòu)建(強烈推薦)
1.4.1 Pipeline的概念
- Pipeline:簡單來說,就是一套運行在Jenkins上的工作流框架,將原本獨立運行于單個或多個節(jié)點的任務(wù)連接起來,實現(xiàn)單個任務(wù)難以完成的復(fù)雜流程編排和可視化的工作。
1.4.2 Pipeline的好處
- 代碼:Pipeline以代碼的形式實現(xiàn),通常被檢入源代碼控制,使團隊能夠編輯,審查和迭代其傳送流程。
- 持久:無論是計劃內(nèi)還是計劃外的服務(wù)器重啟,Pipeline都是可恢復(fù)的。
- 可停止:Pipeline可以接受交互式輸入,以確定是否繼續(xù)執(zhí)行Pipeline。
- 多功能:Pipeline支持現(xiàn)實世界中復(fù)雜的持續(xù)交付要求。它支持fork/join,循環(huán)執(zhí)行,并行執(zhí)行任務(wù)的功能。
- 可擴展:Pipeline插件支持其DSL的自定義擴展,以及和其他插件集成的多個選項。
1.4.3 如何創(chuàng)建Jenkins的Pipeline?
- Pipeline腳本是由Groovy語言實現(xiàn)的,但是我們沒必要單獨去學(xué)習(xí)Pipeline。
- Pipeline支持兩種語法:Declarative(聲明式)和Script Pipeline(腳本式)語言。
- Pipeline支持兩種創(chuàng)建方法:可以直接在Jenkins的Web UI界面輸入腳本,也可以通過創(chuàng)建一個Jenkinsfile腳本文件放入到項目源代碼庫中(推薦)。
1.4.4 安裝Pipeline插件
- Manage Jenkins–>Manage Plugins–>可選插件:

-
安裝插件后,創(chuàng)建項目的時候就會多“流水線”類型:
1.4.5 Pipeline語法快速入門之Declarative聲明式
-
創(chuàng)建項目:
-
流水線–>選擇Hello World模板:
生成的內(nèi)容如下:
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
stages:代表整個流水線的所有執(zhí)行階段。通常而言,stages只有1個,里面包含多個stage。
stage:代表流水線中的某個階段,可能會出現(xiàn)多個。一般分為拉取代碼、編譯構(gòu)建、部署等階段。
steps:代表一個階段內(nèi)需要執(zhí)行的邏輯。steps里面是shell腳本,git拉取代碼,ssh遠程發(fā)布等任意內(nèi)容。
編寫一個簡單聲明式的Pipeline:
pipeline {
agent any
stages {
stage('拉取代碼') {
steps {
echo '拉取代碼'
}
}
stage('編譯構(gòu)建') {
steps {
echo '編譯構(gòu)建'
}
}
stage('項目部署') {
steps {
echo '項目部署'
}
}
}
}
-
點擊構(gòu)建,可以看到整個構(gòu)建過程:
1.4.6 Pipeline語法快速入門之腳本式
- 創(chuàng)建項目和聲明式一樣。
- 流水線–>選擇Script Pipeline:

生成的內(nèi)容如下:
node {
def mvnHome
stage('Preparation') { // for display purposes
// Get some code from a GitHub repository
git 'https://github.com/jglick/simple-maven-project-with-tests.git'
// Get the Maven tool.
// ** NOTE: This 'M3' Maven tool must be configured
// ** in the global configuration.
mvnHome = tool 'M3'
}
stage('Build') {
// Run the maven build
withEnv(["MVN_HOME=$mvnHome"]) {
if (isUnix()) {
sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
} else {
bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore clean package/)
}
}
}
stage('Results') {
junit '**/target/surefire-reports/TEST-*.xml'
archiveArtifacts 'target/*.jar'
}
}
node:節(jié)點,一個Node就是一個Jenkins節(jié)點,Master或者Agent,是執(zhí)行Step的具體運行環(huán)境。
Stage:階段,一個Pipeline可以劃分為若干個Stage,每個Stage代表一組操作,比如Build、Test、Deploy、State是一個邏輯分組的概念。
Step:步驟,Step是最基本的操作單元,可以是打印一句話,也可以是構(gòu)建一個Docker鏡像,由各類Jenkins插件提供。比如命令:sh ‘make’,相當(dāng)于我們平時Shell終端中執(zhí)行的make命令一樣。
- 編寫一個簡單的腳本式Pipeline:
node {
def mvnHome
stage('拉取代碼') {
echo '拉取代碼'
}
stage('編譯構(gòu)建') {
echo '編譯構(gòu)建'
}
stage('項目部署') {
echo '項目部署'
}
}
- 構(gòu)建結(jié)果和聲明式一樣。
1.4.7 拉取代碼


拉取代碼的流水線的聲明式代碼:
pipeline {
agent any
stages {
stage('拉取代碼') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7d5c4945-2533-41e2-bd47-5dd97eb37f38', url: 'git@192.168.18.100:develop_group/springboot2.git']]])
}
}
}
}
1.4.8 編譯打包
-
和上面拉取代碼的步驟一樣。
編譯打包的流水線的聲明式代碼:
pipeline {
agent any
stages {
stage('拉取代碼') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7d5c4945-2533-41e2-bd47-5dd97eb37f38', url: 'git@192.168.18.100:develop_group/springboot2.git']]])
}
}
stage('編譯打包') {
steps {
sh '''echo "開始構(gòu)建"
mvn clean install -Dmaven.test.skip=true
echo "構(gòu)建結(jié)束"'''
}
}
}
}
1.4.9 遠程部署
-
和上面拉取代碼的步驟一樣。

遠程部署的流水線的聲明式代碼:
pipeline {
agent any
stages {
stage('拉取代碼') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7d5c4945-2533-41e2-bd47-5dd97eb37f38', url: 'git@192.168.18.100:develop_group/springboot2.git']]])
}
}
stage('編譯打包') {
steps {
sh '''echo "開始構(gòu)建"
mvn clean install -Dmaven.test.skip=true
echo "構(gòu)建結(jié)束"'''
}
}
stage('遠程部署') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.18.102', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /usr/local
chmod 777 *.sh
./stop.sh
./start.sh''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
1.4.10 Pipeline Script from SCM(強烈推薦)
- 之前都是直接在Jenkins的UI界面里面編寫Pipeline代碼,這樣很不方便腳本維護,建議是把Pipeline腳本放在項目中(一起進行版本控制)。
- 在項目的根目錄建立Jenkinsfile文件,把內(nèi)容復(fù)制到該文件中:

- 將Jenkinsfile文件上傳到Gitlab中。
- 在項目中引用該文件:

二、構(gòu)建細節(jié)
2.1 常用的構(gòu)建觸發(fā)器
2.1.1 概述
- Jenkins內(nèi)置4種構(gòu)建觸發(fā)器:
- 觸發(fā)遠程構(gòu)建。
- 其他工程構(gòu)建后觸發(fā)(Build after other projects are build)。
- 定時構(gòu)建(Build periodically)。
- 輪詢SCM(Poll SCM)。
2.1.2 觸發(fā)遠程構(gòu)建

- 觸發(fā)構(gòu)建的URL:http://192.168.18.101:8888/job/test04/build?token=666。
2.1.3 其他工程構(gòu)建后觸發(fā)

2.1.4 定時構(gòu)建

- 定時字符串從左往右分別為:分 時 日 月 周。
一些定時表達式的例子(H代表形參 ):
每30分鐘構(gòu)建一次: H/30 10:02 10:32
每2個小時構(gòu)建一次: H H/2 *
每天的8點,12點,22點,一天構(gòu)建3次: (多個時間點中間用逗號隔開) 0 8,12,22 *
每天中午12點定時構(gòu)建一次:H 12 *
每天下午18點定時構(gòu)建一次: H 18 *
在每個小時的前半個小時內(nèi)的每10分鐘: H(0-29)/10
每兩小時一次,每個工作日上午9點到下午5點(也許是上午10:38,下午12:38,下午2:38,下午 4:38) :H H(9-16)/2 1-5
2.1.5 輪詢SCM(不建議)
-
輪詢SCM,是指定時掃描本地代碼倉庫的代碼是否發(fā)生變更,如果代碼有變更就觸發(fā)項目構(gòu)建:
注意:這種構(gòu)建觸發(fā)器,Jenkins會定時掃描本地整個項目的代碼,增大系統(tǒng)的開銷,不建議使用。
2.2 Git Hook自動觸發(fā)構(gòu)建
2.2.1 概述
- 在Jenkins的內(nèi)置觸發(fā)構(gòu)建器中,輪詢SCM可以實現(xiàn)GitLab代碼更新、項目自動構(gòu)建,但是該方案的性能不佳,有沒有更好的方案?有,就是利用Gitlab的webhook實現(xiàn)代碼push到倉庫的時候,立即觸發(fā)項目自動構(gòu)建。

2.2.2 安裝Gitlab Hook和Gitlab插件
- 略。
2.2.3 Jenkins設(shè)置自動構(gòu)建

2.2.4 Gitlab開啟webhook功能
- 使用root賬戶登錄到后臺,點擊Admin Area–>Settings–>Network。

2.2.5 在項目中添加webhook
- 點擊項目–>Settings–>integrations:

2.2.6 Jenkins取消Enable authentication for “/project” end-point
- Manage Jenkins–>Configure System。

2.3 Jenkins的參數(shù)化構(gòu)建
2.3.1 Jenkins的參數(shù)化構(gòu)建概述
- 有時在項目構(gòu)建的過程中,我們需要根據(jù)用戶的輸入動態(tài)傳入一些參數(shù),從而影響整個構(gòu)建結(jié)果,這時我們可以使用參數(shù)化構(gòu)建。
- Jenkins支持非常豐富的參數(shù)類型。

2.3.2 使用Gitlab的分支名稱來部署不同的分支項目
- 修改Jenkinsfile文件:
pipeline {
agent any
stages {
stage('拉取代碼') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7d5c4945-2533-41e2-bd47-5dd97eb37f38', url: 'git@192.168.18.100:develop_group/springboot2.git']]])
}
}
stage('編譯打包') {
steps {
sh '''echo "開始構(gòu)建"
mvn clean install -Dmaven.test.skip=true
echo "構(gòu)建結(jié)束"'''
}
}
stage('遠程部署') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.18.102', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /usr/local
chmod 777 *.sh
./stop.sh
./start.sh''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
“${branch}”是重點。
-
創(chuàng)建master和v1分支:略。
-
點擊Build with Parameters:
2.4 配置郵箱服務(wù)器發(fā)送構(gòu)建結(jié)果
2.4.1 需要安裝Email Extension Template插件
- 略。
2.4.2 Jenkins設(shè)置郵箱參數(shù)
- Manage Jenkins–>Configure System。

-
設(shè)置郵箱參數(shù):

2.4.3 準備郵箱內(nèi)容
- 在項目的根目錄編寫email.html,并把文件推送到Gitlab中,內(nèi)容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次構(gòu)建日志</title>
</head>
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
offset="0">
<table width="95%" cellpadding="0" cellspacing="0"
style="font-size: 11pt; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif">
<tr>
<td>(本郵件是程序自動下發(fā)的,請勿回復(fù)!)</td>
</tr>
<tr>
<td><h2>
<font color="#0000FF">構(gòu)建結(jié)果 - ${BUILD_STATUS}</font>
</h2></td>
</tr>
<tr>
<td><br />
<b><font color="#0B610B">構(gòu)建信息</font></b>
<hr size="2" width="100%" align="center" /></td>
</tr>
<tr>
<td>
<ul>
<li>項目名稱 : ${PROJECT_NAME}</li>
<li>構(gòu)建編號 : 第${BUILD_NUMBER}次構(gòu)建</li>
<li>觸發(fā)原因: ${CAUSE}</li>
<li>構(gòu)建日志: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
<li>構(gòu)建 Url : <a href="${BUILD_URL}">${BUILD_URL}</a></li>
<li>工作目錄 : <a href="${PROJECT_URL}ws">${PROJECT_URL}ws</a></li>
<li>項目 Url : <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
</ul>
</td>
</tr>
<tr>
<td><b><font color="#0B610B">Changes Since Last
Successful Build:</font></b>
<hr size="2" width="100%" align="center" /></td>
</tr>
<tr>
<td>
<ul>
<li>歷史變更記錄 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
</ul> ${CHANGES_SINCE_LAST_SUCCESS,reverse=true, format="Changes for Build #%n:<br />%c<br />",showPaths=true,changesFormat="
<pre>[%a]<br />%m</pre>",pathFormat=" %p"}
</td>
</tr>
<tr>
<td><b>Failed Test Results</b>
<hr size="2" width="100%" align="center" /></td>
</tr>
<tr>
<td><pre
style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre>
<br /></td>
</tr>
<tr>
<td><b><font color="#0B610B">構(gòu)建日志 (最后 100行):</font></b>
<hr size="2" width="100%" align="center" /></td>
</tr>
<tr>
<td><textarea cols="80" rows="30" readonly="readonly"
style="font-family: Courier New">${BUILD_LOG, maxLines=100}</textarea>
</td>
</tr>
</table>
</body>
</html>
2.4.4 編寫Jenkinsfile添加構(gòu)建后發(fā)送郵箱
- Jenkinsfile:
pipeline {
agent any
stages {
stage('拉取代碼') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'bacbbbb1-2df9-470d-adf8-5cb6dc496807', url: 'git@192.168.209.100:develop_group/springboot2.git']]])
}
}
stage('編譯構(gòu)建') {
steps {
sh '''echo "開始構(gòu)建"
mvn clean install -Dmaven.test.skip=true
echo "構(gòu)建結(jié)束"'''
}
}
stage('項目部署') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.209.102', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''#!/bin/bash
echo "stop SpringBoot BAppApiServerApplication"
pid=`ps -ef | grep java | grep -v grep | awk \'{print $2}\'`
echo “舊應(yīng)用進程id:$pid”
if [ -n "$pid" ]
then
kill -9 $pid
rm -rf springboot2-1.0.jar
fi
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.9.11-2.el7_9.x86_64
echo ${JAVA_HOME}
echo "授權(quán)當(dāng)前用戶"
chmod 777 /usr/local/springboot2-1.0.jar
echo "執(zhí)行...."
cd /usr/local/
nohup ${JAVA_HOME}/bin/java -jar springboot2-1.0.jar > /dev/null &
echo "啟動成功"
''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
post {
always {
emailext (
subject: '構(gòu)建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
body: '${FILE,path="email.html"}',
to: 'xxx@qq.com'
)
}
}
}
三、Jenkins+SonarQube代碼審查
3.1 概述
- SonarQube是一個用于管理代碼質(zhì)量的開放平臺,可以快速的定位代碼中潛在的或者明顯的錯誤。目前支持Java、C#、C++、Python、PL/SQL、Cobol、JavaScript、Groovy等二十幾種編程語言的代碼質(zhì)量管理和檢測。
- 官網(wǎng)。
- 環(huán)境要求:
| 軟件 | 服務(wù)器 | 版本 |
|---|---|---|
| JDK | 192.168.18.101 | 11 |
| PostgreSQL | 192.168.18.101 | 5.7 |
| SonarQube | 192.168.18.101-8.6.0 | 8.6.0 |
3.2 安裝SonarQube
3.2.1 安裝PostgreSQL(也可以安裝Mysql數(shù)據(jù)庫)
- Docker安裝:
# 用戶名是 postgres ,密碼是123456
docker run --name postgres -v dv_pgdata:/var/lib/postgresql/data --restart=always -e POSTGRES_PASSWORD=123456 -p 5432:5432 -d postgres:12.1
在PostgreSQL中新建sonar數(shù)據(jù)庫:
CREATE DATABASE sonar;
3.2.2 安裝SonarQube
在PostgreSQL中新建sonar數(shù)據(jù)庫:
CREATE DATABASE sonar;
下載SonarQube:
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.6.0.39681.zip
解壓sonar:
unzip -d /usr/local/ sonarqube-8.6.0.39681.zip
cd /usr/local
mv sonarqube-8.6.0.39681 sonarqube-8.6.0
創(chuàng)建用戶,用于設(shè)置權(quán)限:
# 創(chuàng)建sonar用戶,sonar不能用root啟動,否則報錯
useradd sonar
passwd sonar
chown -R sonar /usr/local/sonarqube-8.6.0
修改sonar的配置文件:
vim /usr/local/sonarqube-8.6.0/conf/sonar.properties
# 內(nèi)容如下
sonar.jdbc.username=postgres
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonar
sonar默認自帶了ES,所以需要修改配置,防止啟動報錯:
vim /etc/security/limits.conf
# 追加內(nèi)容
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
vim /etc/security/limits.d/90-nproc.conf
# 追加內(nèi)容
* soft nproc 4096
vim /etc/sysctl.conf
# 追加內(nèi)容
vm.max_map_count=655360
sysctl -p
reboot
啟動sonar(sonar的默認端口是9000):
cd /usr/local/sonarqube-8.6.0/
# 啟動
su sonar ./bin/linux-x86-64/sonar.sh start
# 查看狀態(tài)
su sonar ./bin/linux-x86-64/sonar.sh status
# 停止
su sonar ./bin/linux-x86-64/sonar.sh stop
# 查看日志
tail -f logs/sonarxxx.logs
訪問sonar:http://192.168.18.101:9000。

-
修改sonar的默認密碼:
-
安裝中文插件:
-
生成令牌(需要將生成的令牌復(fù)制下來):
比如我此時的令牌是:1463a2ebf240a0c77bd9c965f76006fe03d166a6。
3.3 實現(xiàn)代碼審查
3.3.1 概述

3.3.2 安裝SonarQube Scanner插件
- 略。
3.3.3 安裝SonarQube Scanner
-
安裝SonarQube Scanner有兩種方式:
- 在Linux所在的服務(wù)器上直接安裝。
- 通過Jenkins幫我們自動安裝(本人選擇這種)。
-
Manage Jenkins–>Global Tool Configuration。
3.3.4 Jenkins配置Sonar Server

-
Manage Jenkins->Con?gure System->SonarQube servers:
3.3.5 非流水線項目添加SonarQube代碼審查
-
添加構(gòu)建步驟:
配置如下:
# must be unique in a given SonarQube instance
sonar.projectKey=springboot2
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 8.6.0
sonar.projectName=springboot2
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=11
sonar.java.target=11
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
sonar.java.binaries=**target/classes
3.3.6 流水線項目添加SonarQube代碼審查
在項目的根目錄中添加sonar-project.properties文件,內(nèi)容如下:
# must be unique in a given SonarQube instance
sonar.projectKey=springboot2
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 8.6.0
sonar.projectName=springboot2
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=11
sonar.java.target=11
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
sonar.java.binaries=**target/classes

Jenkinsfile:
pipeline {
agent any
stages {
stage('拉取代碼') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '7d5c4945-2533-41e2-bd47-5dd97eb37f38', url: 'git@192.168.18.100:develop_group/springboot2.git']]])
}
}
stage('編譯打包') {
steps {
sh '''echo "開始構(gòu)建"
mvn clean install -Dmaven.test.skip=true
echo "構(gòu)建結(jié)束"'''
}
}
stage('代碼檢查') {
steps {
script {
// 引入SonarQubeScanner工具
scannerHome = tool 'sonarqube-scanner'
}
// 引入了 SonarQube服務(wù)器的環(huán)境
withSonarQubeEnv('sonarqube-8.6.0') {
sh "${scannerHome}/bin/sonar-scanner"
}
}
}
stage('遠程部署') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.18.102', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''cd /usr/local
chmod 777 *.sh
./stop.sh
./start.sh''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.jar')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
參考:
https://blog.csdn.net/weixin_41979002/article/details/121706985





















