Jenkins 基本操作
1、關(guān)閉Jenkins
? 只需要在訪問jenkins服務(wù)器的網(wǎng)址url地址后加上exit和二。例如我jenkins的地址http://localhost:8080/边涕,那么我只需要在瀏覽器地址欄上敲下http://localhost:8080/exit 網(wǎng)址就能關(guān)閉jenkins服務(wù).
2蚊逢、重啟Jenkies
3聂宾、重新加載配置信息
? http://localhost:8080/reload
Jenkins 插件下載地址(包含多版本)
http://updates.jenkins-ci.org/download/plugins/
- 自己上傳插件并安裝(見下面 jenkins 安裝配置 sonar 插件)
sonar 安裝與使用
安裝
- 下載解壓
sonar 下載地址:https://www.sonarqube.org/downloads/
unzip sonarqube-6.7.7.zip
- 指定 jdk 1.8(如果系統(tǒng)環(huán)境變量已經(jīng)是1.8忽略此步)
下載好sonarQube后夕膀,解壓打開conf目錄,修改 wrapper.conf
# Path to JVM executable. By default it must be available in PATH.
# Can be an absolute path, for example:
#wrapper.java.command=/path/to/my/jdk/bin/java
wrapper.java.command=java
- 啟動 sonar
[root@localhost sonarqube-6.7.7]# ./bin/linux-x86-64/sonar.sh start
Starting SonarQube...
SonarQube is already running.
# 啟動失敗的原因可能是權(quán)限問題
- 觀察啟動日志
[root@localhost sonarqube-6.7.7]# tail -200f ./logs/sonar.log
- 訪問:localhost:9000
配置
- mysql 添加 sonar 用戶
安裝mysql,可以是遠(yuǎn)程連接(注:mysql版本需要是5.6+)
SonarQube 默認(rèn)服務(wù)端口為 9000美侦,默認(rèn)數(shù)據(jù)庫為 h2产舞,這些都是可以修改配置的,我們只需要修改<install_dir>/conf/sonar.properties
文件即可菠剩。以修改配置 Mysql 數(shù)據(jù)庫為例:
1易猫、修改 sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
# 以下自己需要可以設(shè)置
#sonar.web.host=你的本地IP
#sonar.web.context=/sonar
#sonar.web.port=9000
2、本地 Mysql 創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
3具壮、本地 Mysql 創(chuàng)建用戶并分配權(quán)限
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL PRIVILEGES ON *.* TO 'sonar'@'%' IDENTIFIED BY 'sonar' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar' WITH GRANT OPTION;
FLUSH PRIVILEGES;
- 重啟服務(wù)准颓,觀察日志
[root@localhost sonarqube-6.7.7]# ./bin/linux-x86-64/sonar.sh restart
[root@localhost sonarqube-6.7.7]# tail -200f ./logs/sonar.log
未修改數(shù)據(jù)庫使用 h2 的啟動日志:
2019.06.04 06:57:04 INFO app[][o.s.a.p.ProcessLauncherImpl] ...:/usr/local/sonarqube-6.7.7/lib/jdbc/h2/h2-1.3.176.jar org.sonar.ce.app.CeServer /usr/local/sonarqube-6.7.7/temp/sq-process5742909867491483230properties
使用 mysql 后的的啟動日志:
2019.06.04 07:14:24 INFO app[][o.s.a.p.ProcessLauncherImpl] ...:/usr/local/sonarqube-6.7.7/lib/jdbc/mysql/mysql-connector-java-5.1.42.jar org.sonar.ce.app.CeServer /usr/local/sonarqube-6.7.7/temp/sq-process3499505084016070096properties
簡單使用
默認(rèn)用戶名是 admin,密碼 admin
- 登入會提示生成 token棺妓,后面在 Jenkins 和 Sonar 集成的時候需要用到攘已。
name:jiaflu
token:e911bcba9ebec800d2f212ce4fa13d8b02f2bac4
Jenkins 安裝配置 sonar 插件
- 安裝 SonarQube Scanner 插件
Manages Jenkins --> Manage Plugins
若安裝失敗可以先下載再上傳插件安裝:
下載地址:https://updates.jenkins.io/download/plugins/sonar/
版本:2.4.3
Advanced:
- Jenkins Sonar 配置
Manages Jenkins --> Configure System
- SonarQube Scanner 配置
Manages Jenkins --> Global Tool Configuration
- Sonar 中質(zhì)量指標(biāo)(度量)
https://blog.csdn.net/lxlmycsdnfree/article/details/80166335
- Jenkins 所使用的 maven 配置,
conf/settings.xml
,不配置的話在執(zhí)行 maven 命令的時候默認(rèn) sonar host 為 http://localhost:9000
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- 配置 Sonar Host地址怜跑,默認(rèn):http://localhost:9000 -->
<sonar.host.url>
http://localhost:9000
</sonar.host.url>
</properties>
</profile>
</profiles>
jenkins 集成 sonarqube 生成測試覆蓋率報告
額外需要的插件
- Cobertura 1.9.8
- Junit
構(gòu)建一個 freestyle 的項目
配置截圖如下:
- Source Code Management
- Build
- Post-build Actions
build 后 sonarqube 上的結(jié)果
項目配置問題
maven 配置 setting.xml
<settings>
<pluginGroups>
<pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>
<profiles>
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- 配置 Sonar Host地址样勃,默認(rèn):http://localhost:9000 -->
<sonar.host.url>
http://myserver:9000
</sonar.host.url>
</properties>
</profile>
</profiles>
</settings>
修改 maven3 項目的默認(rèn)編譯級別(compile level),修改項目 pom.xml
,僅在修改項目上生效
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>