iOS+Jenkins持續(xù)構(gòu)建-Sonarqube

一、環(huán)境介紹

iOS的sonar掃描對(duì)環(huán)境要求很多,以下是我的環(huán)境。
系統(tǒng):macOS Mojave 10.14.4
Xcode:10.3
Jenkins:2.190
JDK:jdk1.8.0_191.jdk
sonar-runner:2.4(可以不用)
sonar-scanner:4.0.0.1744
oclint:0.13
SonarQube:7.3
Python:3.7
mysql:5.7.27

二、環(huán)境安裝

基本環(huán)境見前面幾篇文章,以下是新搭建環(huán)境。

  1. Sonarqube安裝
    下載地址
    包解壓后放置路徑:/usr/local/sonarqube-7.3

  2. mysql安裝
    下載地址

  3. 下載sonar-runner sonar-scanner
    下載后放置:
    /usr/local/sonar-runner-2.4
    /usr/local/sonar-scanner-4.0.0.1744-macosx

  1. 創(chuàng)建用戶及數(shù)據(jù)庫
    mysql修改密碼自行操作,我的修改為了:12345678
mysql -u root -p
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
mysql> FLUSH PRIVILEGES;
  1. sonar-runner sonar-scanner配置
    目錄:/usr/local/sonar-runner-2.4/conf/sonar-runner.properties
    增加以下內(nèi)容:
#--------------- -------------------- 手動(dòng)修改內(nèi)容 ------   -----------
sonar.host.url=http://localhost:9000/sonarqube
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
sonar.jdbc.username=root
sonar.jdbc.password=12345678
sonar.login=admin  
sonar.password=admin
#--------------- --------------------  ------   -----------

目錄:/usr/local/sonar-scanner-4.0.0.1744-macosx/conf/sonar-scanner.properties
增加以下內(nèi)容:

sonar.login=admin 
sonar.password=admin
  1. sonarqube配置
    目錄:/usr/local/sonarqube-7.3/conf/wrapper.conf
    增加以下內(nèi)容:
wrapper.java.command=/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java

目錄:/usr/local/sonarqube-7.3/conf/sonar.properties

#----- ----- ----- ----- ----- ----- 
sonar.host.url=http://localhost:9000/sonarqube
# mysql 用戶名
sonar.jdbc.username=root
# mysql 密碼
sonar.jdbc.password=12345678
#----- ----- ----- ----- ----- ----- 
  1. 啟動(dòng)sonarqube,并安裝中文插件
    命令:

cd /usr/local/sonarqube-7.3/bin/macosx-universal-64
sh sonar.sh start

查看日志

tail -f /usr/local/sonarqube-7.3/logs/sonar.log

也可以直接去目錄看: /usr/local/sonarqube-7.3/logs /usr/local/sonarqube-7.3/logs

啟動(dòng)后安裝中文插件(我已安裝,市場(chǎng)-輸入Chinese,再重啟):

sh sonar.sh restart

1.png
  1. sonarqube的oc,swift插件需要收費(fèi),去github上找到個(gè)開源的插件
    地址
    我下載的目前最新:0.4.5版本,該插件支持oc和swift
    將backelite-sonar-swift-plugin-0.4.5.jar拷貝至/usr/local/sonarqube-7.3/extensions/plugins
    重啟sonarqube

  2. 新建項(xiàng)目


  1. 項(xiàng)目配置
    項(xiàng)目根目錄下配置兩個(gè)文件
    sonar-project.properties
    內(nèi)容
sonar.projectKey=第七步中的標(biāo)識(shí)
sonar.projectName=項(xiàng)目名
sonar.projectVersion=1.0//版本號(hào)
sonar.language=objc // 語言,swift 就寫swift

sonar.objectivec.project=項(xiàng)目.xcodeproj
sonar.objectivec.workspace=項(xiàng)目.xcodeproj/project.xcworkspace

sonar.objectivec.appScheme=項(xiàng)目Scheme
sonar.objectivec.testScheme=項(xiàng)目testScheme
sonar.objectivec.simulator=platform=iOS Simulator,name=iPhone 8,OS=latest

//  掃描的路徑可以是. 或者項(xiàng)目下的某個(gè)文件夾,如:A/class
sonar.sources=
## 排除的路徑,使用正則匹配
sonar.objectivec.excludedPathsFromCoverage=*Resources

sonar.objectivec.oclint.report=oclint.xml

sonar.host.url=http://172.26.6.247:9000
sonar.login=admin
sonar.password=admin

# 這是我自定義的,用來排除sonar.sources里面的我不想掃描的文件
sonar.objectivec.excludePath=-e Common -e 3rdPartys 

run-sonar.sh

從下面拿就好:
https://github.com/Backelite/sonar-swift/blob/develop/objclang/src/main/shell/run-sonar.sh

如果要用我自定義的sonar.objectivec.excludePath
run-sonar.sh 修改如下:
增加內(nèi)容

# Read coverage type
coverageType=''; readParameter coverageType 'sonar.objectivec.coverageType'

// 增加的是下面這句
excludePath=''; readParameter excludePath 'sonar.objectivec.excludePath'

第二處把excludePath加到runCommand no oclint-json-compilation-database -v $includedCommandLineFlags后面

  1. 安裝圈復(fù)雜度掃描插件(如果不用這個(gè)指標(biāo)可不裝)
    mac系統(tǒng)自帶python,但我要用pip命令需要升級(jí),所以重新下載python
    下載地址

  2. 統(tǒng)一搞下環(huán)境變量
    配置如下:

打開終端

open -e ./.bash_profile

export JAVA_HOME=/Library/Java/JavaVirtualMachines/?jdk1.8.0_191.jdk/Contents/Home
PATH=$JAVA_HOME/bin:$PATH

export MYSQL=/usr/local/mysql
PATH=$MYSQL/bin:$PATH

export SONAR_HOME=/usr/local/sonarqube-7.3
PATH=$SONAR_HOME/bin:$PATH

export SONAR_SCANNER_HOME=/usr/local/sonar-scanner-4.0.0.1744-macosx
PATH=$SONAR_SCANNER_HOME/bin:$PATH

export SONAR_RUNNER_HOME=/usr/local/sonar-runner-2.4
PATH=$SONAR_RUNNER_HOME/bin:$PATH

export OCLINT=/usr/local/Cellar/oclint/0.13
PATH=$OCLINT/bin:$PATH

export PYTHON=/Library/Frameworks/Python.framework/Versions/3.7
PATH=$PYTHON/bin:$PATH


alias python="/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7"

export PATH

alias python這句是為了設(shè)置新python為默認(rèn)

  1. 安裝lizard
    https://github.com/terryyin/lizard

  2. 配置jenkins
    配好git或svn后
    增加shell,內(nèi)容如下

#!/bin/bash -il
# cd ...... 找到run-sonar.sh 文件目錄
sh run-sonar.sh
  1. 執(zhí)行構(gòu)建

三、問題說明

  • 部分字符串不認(rèn)
    shell增加:export LC_ALL="en_US.UTF-8"

  • xcpretty 或者 oclint commond not fund
    ERROR - oclint is not installed or not in your PATH
    xcpretty: command not found

這是jenkins環(huán)境不同導(dǎo)致
修改shell如下:

#!/bin/bash -il
   
# 加載環(huán)境變量
source /Users/xxx/.bash_profile
export LC_ALL="en_US.UTF-8"
cd OneETripiOS
sh run-sonar.sh

如果還有沒找到的,說明你的環(huán)境變量還沒配全:找到對(duì)應(yīng)的插件目錄
如:/usr/local

open -e ./.bash_profile

增加環(huán)境變量配置:

export USRLOCAL=/usr/local
PATH=$USRLOCAL/bin:$PATH

使用以下命令生效

source ~/.bash_profile

  • ERROR: The rule 'OCLint:compiler warning' does not exist.

找到run-sonar.sh 增加如下配置-extra-arg=-Wno-everything
配置后如下

 runCommand no oclint-json-compilation-database -v $includedCommandLineFlags $excludePath -- -extra-arg=-Wno-everything -rc LONG_LINE=$longLineThreshold -max-priority-1 $maxPriority -max-priority-2 $maxPriority -max-priority-3 $maxPriority  -report-type pmd -o sonar-reports/$(echo $word | sed 's/\//_/g')-oclint.xml
  • ERROR: Caused by: Can not add the same measure twice on
    查看sonar-project.properties中的source配置是否寫對(duì)

  • you must install a plugin that supports the language 'objectivec'
    這是sonar-project.properties語言寫錯(cuò)了,不是objectivec是objc

  • UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 0: ordinal not in range(128)
    export LC_ALL="en_US.UTF-8"

查看效果:


4C795AA587D853A32530FF7BFDE3A70E.png

最后做了個(gè)sonarqube的自啟動(dòng)腳本

sonar.sh

#!/bin/bash

cd /usr/local/sonarqube-7.3/bin/macosx-universal-64

sh sonar.sh start

右鍵文件修改為終端打開,并修改權(quán)限讀寫


26ACCE7FA88ACE90028B10EC63E48141.png

然后加入執(zhí)行


9219E79F34B9E700E1C0B1C268B00888.png

打完收工!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過簡(jiǎn)信或評(píng)論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容