前言#
🔜什麼是SonarQube?🔚
SonarQube 是一個開源的代碼質量管理平台,通過一系列的規則庫對代碼的掃描檢查,提升代碼的質量。
運行 SonarQube 伺服器分析需要三個組件:SonarQube Server、存儲數據庫(本文中使用 PostgreSQL)、 scanners 掃描程序。
其中 UI 為SonarQube Server :負責提供 Web 界面、處理代碼分析報告並將其保存在 存儲數據庫中的計算引擎(SonarQube Server 中還集成有 Elasticsearch)。
存儲數據庫 :SonarQube Server 的配置;代碼掃描期間生成的代碼質量和安全性指標和問題。
scanners 掃描程序:用於分析項目,根據語言有所不同。
環境準備#
(1)使用腳本安裝 docker、docker-compose
bash <(curl -sSL https://linuxmirrors.cn/docker.sh)
(2)配置鏡像加速
vi /etc/docker/daemon.json
{
"data-root": "/data/dockerData",
"registry-mirrors": [
"https://docker.mirrors.sjtug.sjtu.edu.cn",
"https://docker.mirrors.ustc.edu.cn",
"https://mirror.iscas.ac.cn",
"https://docker.rainbond.cc",
"https://docker.kubesre.xyz"],
"log-driver":"json-file",
"log-opts":{"max-size" :"50m","max-file":"3"}
}
(3)啟動 docker 服務
systemctl start docker
systemctl enable docker
systemctl status docker
(4)設置進程可能具有的最大內存映射區域數 (vm.max_map_count) 大於或等於 524288,打開的文件描述符的最大數量 (fs.file-max) 大於或等於 131072。
echo "vm.max_map_count=524288
fs.file-max=131072" >> /etc/sysctl.conf
sysctl -p
(5)配置安全策略
firewall-cmd --add-port=9000/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-all
部署 sonarqube#
(1)創建所需目錄
mkdir -p /data/sonarqube/data /data/sonarqube/extensions /data/sonarqube/logs /data/sonarqube/temp
chmod -R 777 /data/sonarqube/
mkdir -p /data/sonarqube/sonarqube-sql /data/sonarqube/sonarqube-sql/data
(2)創建 docker compose 文件,編排創建 sonarqube、數據庫容器。
vi sonarqube.yml
*****************************************************
services:
sonarqube:
image: sonarqube:community
container_name: sonarqube
restart: always
volumes:
- /data/sonarqube/data:/opt/sonarqube/data
- /data/sonarqube/extensions:/opt/sonarqube/extensions
- /data/sonarqube/logs:/opt/sonarqube/logs
- /data/sonarqube/temp:/opt/sonarqube/temp
environment:
SONAR_JDBC_URL: jdbc:postgresql://sonarqube-sql:5432/postgres
SONAR_JDBC_USERNAME: postgres
SONAR_JDBC_PASSWORD: Qwer#1234
ports:
- "9000:9000"
depends_on:
sonarqube-sql:
condition: service_healthy
networks:
net:
ipv4_address: 172.20.112.11
sonarqube-sql:
image: postgres:15
hostname: postgresql
container_name: sonarqube-sql
restart: always
volumes:
- /data/sonarqube/sonarqube-sql:/var/lib/postgresql
- /data/sonarqube/sonarqube-sql/data:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_DB: postgres
POSTGRES_PASSWORD: Qwer#1234
TZ: "Asia/Shanghai"
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5
networks:
net:
ipv4_address: 172.20.112.12
networks:
net:
driver: bridge
ipam:
config:
- subnet: 172.20.112.0/24
*****************************************************
(3)執行命令,創建容器
docker compose -f sonarqube.yml up -d
部署SonarScanner 集成 VUE#
(1)在 VUE 項目的根目錄下創建 sonar-project.properties 文件,並添加配置項
vi sonar-project.properties
*****************************************************
sonar.projectKey=sonarqube中創建的項目名稱
sonar.projectName=VUE項目名
sonar.projectVersion=VUE項目版本
sonar.sources=. #VUE項目源文件的目錄
sonar.language=vue #項目語言
sonar.sourceEncoding=UTF-8 #項目編碼
sonar.host.url=http://192.168.32.12:9000/ #sonarqube伺服器端的地址
sonar.token=sonarqube中創建項目的token
*****************************************************
#修改文件權限
chmod -R 777 sonar-project.properties
(2)創建 SonarScanner 容器掃描項目代碼
docker run --rm \
-v "/home/code/vue-test:/usr/src" \
sonarsource/sonar-scanner-cli
根據項目代碼的多少時間會有所不同,以下是掃描完成後的提示。
09:13:23.107 WARN This may lead to missing/broken features in SonarQube
09:13:23.324 INFO CPD Executor 61 files had no CPD blocks
09:13:23.325 INFO CPD Executor Calculating CPD for 325 files
09:13:23.634 INFO CPD Executor CPD calculation finished (done) | time=309ms
09:13:23.641 INFO SCM revision ID '4941a614714697243a5a8f7824fc921ff5f84345'
09:13:24.131 INFO Analysis report generated in 462ms, dir size=18.8 MB
09:13:25.560 INFO Analysis report compressed in 1429ms, zip size=8.3 MB
09:14:10.518 INFO Analysis report uploaded in 44955ms
09:14:10.519 INFO ANALYSIS SUCCESSFUL, you can find the results at: http://192.168.32.12:9000/dashboard?id=test4
09:14:10.519 INFO Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
09:14:10.519 INFO More about the report processing at http://192.168.32.12:9000/api/ce/task?id=acfc4bc2-7b20-4166-bde6-93d970ac62b3
09:14:10.601 INFO Analysis total time: 3:49.166 s
09:14:10.602 INFO SonarScanner Engine completed successfully
09:14:10.636 INFO EXECUTION SUCCESS
09:14:10.637 INFO Total time: 17:51.245s
部署SonarScanner 集成 PHP#
(1)在 PHP 項目的根目錄下創建 sonar-project.properties 文件,並添加配置項
vi sonar-project.properties
*****************************************************
sonar.projectKey=sonarqube中創建的項目名稱
sonar.projectName=PHP項目名
sonar.projectVersion=PHP項目版本
sonar.sources=. #PHP項目源文件的目錄
sonar.language=php #項目語言
sonar.sourceEncoding=UTF-8 #項目編碼
sonar.host.url=http://192.168.32.12:9000/ #sonarqube伺服器端的地址
sonar.token=sonarqube中創建項目的token
*****************************************************
#修改文件權限
chmod -R 777 sonar-project.properties
(2)創建 SonarScanner 容器掃描項目代碼
docker run --rm \
-v "/home/code/php-test:/usr/src" \
sonarsource/sonar-scanner-cli
根據項目代碼的多少時間會有所不同,以下是掃描完成後的提示。
09:13:23.107 WARN This may lead to missing/broken features in SonarQube
09:13:23.324 INFO CPD Executor 61 files had no CPD blocks
09:13:23.325 INFO CPD Executor Calculating CPD for 325 files
09:13:23.634 INFO CPD Executor CPD calculation finished (done) | time=309ms
09:13:23.641 INFO SCM revision ID '4941a614714697243a5a8f7824fc921ff5f84345'
09:13:24.131 INFO Analysis report generated in 462ms, dir size=18.8 MB
09:13:25.560 INFO Analysis report compressed in 1429ms, zip size=8.3 MB
09:14:10.518 INFO Analysis report uploaded in 44955ms
09:14:10.519 INFO ANALYSIS SUCCESSFUL, you can find the results at: http://192.168.32.12:9000/dashboard?id=test4
09:14:10.519 INFO Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
09:14:10.519 INFO More about the report processing at http://192.168.32.12:9000/api/ce/task?id=acfc4bc2-7b20-4166-bde6-93d970ac62b3
09:14:10.601 INFO Analysis total time: 3:49.166 s
09:14:10.602 INFO SonarScanner Engine completed successfully
09:14:10.636 INFO EXECUTION SUCCESS
09:14:10.637 INFO Total time: 17:51.245s