SonarQube Scanner CLI 安裝與使用
安裝
# 啟動本地分析伺服器 http://localhost:9000/,預設帳密 admin/admin
# docker run -d --name sonarqube -p 9000:9000 sonarqube:community
# 我們改用 9870 埠 http://localhost:9870/
docker run -d --name sonarqube -p 9870:9000 sonarqube:community
# 安裝 SonarQube Scanner CLI
brew install sonar-scanner
專案設定
在專案根目錄建立 sonar-project.properties,內容如下(以 paymentpool 為例):
# 專案唯一標識
sonar.projectKey=paymentpool
sonar.projectName=Paymentpool
sonar.projectVersion=3.2.17
# 掃描的路徑
sonar.sources=app, resources
sonar.tests=tests
# 語言特定設定(通常會自動識別,但可手動微調)
# 排除不需要分析的目錄
sonar.exclusions=**/node_modules/**, **/vendor/**, **/public/**, **/storage/**, **/bootstrap/**, **/config/**, **/database/**, **/routes/**, **/artisan, **/composer.json, **/composer.lock
# 伺服器位址
sonar.host.url=http://localhost:9870
# 登入 Token (可在 Web UI 產生,初次測試可用管理員權限直接掃描)
sonar.token=sqp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# 測試覆蓋率報告
sonar.php.tests.reportPath=docs/build/report.junit.sonar.xml
sonar.php.coverage.reportPaths=docs/build/coverage-sonar.xml
執行掃描
sonar-scanner
執行完畢後,終端會輸出:
INFO ANALYSIS SUCCESSFUL, you can find the results at: http://localhost:9870/dashboard?id=paymentpool
INFO Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO More about the report processing at http://localhost:9870/api/ce/task?id=b6d7a30c-772e-4cbd-8207-f84baa296d7d
直接在終端列出報告
curl -u admin:<password> "http://localhost:9870/api/measures/component_tree?component=paymentpool&metricKeys=cognitive_complexity" | jq '.components[] | {name: .name, complexity: .measures[0].value}'