1. 程式人生 > 實用技巧 >SonarQube服務搭建

SonarQube服務搭建

SonarQube服務搭建

一 、 先決條件和概述

JDK

JavaServerScanners
Oracle JRE 11 ≥8
OpenJDK 11 ≥8

資料庫

資料庫版本備註
PostgreSQL ≥9.3 UTF-8 charset
Oracle ≥11g UTF-8 charset
MSSQL ≥14

硬體要求

例項至少需要2GB的RAM才能有效執行,而OS則需要1GB的可用RAM。

企業建議配置: 8C16G

Linux配置要求:

  • vm.max_map_count 大於或等於524288

  • fs.file-max

    大於或等於131072

  • 執行SonarQube的使用者可以開啟至少131072個檔案描述符

  • 執行SonarQube的使用者可以開啟至少8192個執行緒

檢查方法

sysctl vm.max_map_count
sysctl fs.file-max
ulimit -n
ulimit -u

核心支援seccomp過濾器

預設情況下,Elasticsearch使用seccomp filter

$ grep SECCOMP /boot/config-$(uname -r)

字型

  • Fontconfig已安裝在託管SonarQube的伺服器上

  • SonarQube伺服器上安裝了FreeType

    字型包。可用的確切軟體包會因分發而異,但是常用的軟體包是libfreetype6

二、 在Docker上安裝

2.1 配置系統引數

修改/etc/sysctl.d/99-sonarqube.conf

vm.max_map_count=534288
fs.file-max=141072

讓其生效

sysctl -p /etc/sysctl.d/99-sonarqube.conf

2.2 編寫docker-compose

/docker-compose/sonarqube/docker-compose.yml

參考: https://github.com/SonarSource/docker-sonarqube

version: "3"
​
services:
  sonarqube:
    image: sonarqube:8.2-community
    depends_on:
      - db
    ports:
      - "9000:9000"
    networks:
      - sonarnet
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: sonar
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
      - sonarqube_temp:/opt/sonarqube/temp
  db:
    image: postgres
    networks:
      - sonarnet
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
    volumes:
      - postgresql:/var/lib/postgresql
      # This needs explicit mapping due to https://github.com/docker-library/postgres/blob/4e48e3228a30763913ece952c611e5e9b95c8759/Dockerfile.template#L52
      - postgresql_data:/var/lib/postgresql/data
​
networks:
  sonarnet:
    driver: bridge
​
volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  sonarqube_temp:
  postgresql:
  postgresql_data:

  

2.3 啟動

cd /docker-compose/sonarqube
docker-compose up

訪問:http://192.168.0.80:9000/

初始賬戶和密碼: admin/admin

三、 伺服器配置

3.1 安裝中文外掛

Administration --> Marketplace

搜尋Chinese Pack

3.2 修改初始密碼

配置 > 許可權 > 使用者

選擇admin使用者,點選輪滑按鈕,選擇重置密碼

3.3 建立新使用者並生成token

配置 > 許可權 > 使用者 > 建立使用者

建立完成後點選令牌

並記住令牌

四、 Jenkins與SonarQube整合外掛的安裝與配置

4.1 安裝Jenkins的SonarScanner

  1. 系統管理 > 外掛管理 > 可選外掛

  2. 搜尋SonarQube Scanner

  3. 點選直接安裝

4.2 配置SonarQube伺服器

Jenkins面板點選:系統管理 > 系統配置 > SonarQube servers > Add SonarQube

填入

Name: Frog

Server URL: http://192.168.0.80:9000

Token: 注意新加token,選擇型別為Sercet file

4.3 安裝SonarQube Scanner

wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.4.0.2170-linux.zip
unzip sonar-scanner-cli-4.4.0.2170-linux.zip

4.4 配置SonarQube Scanner

編輯/opt/sonar-scanner-4.4.0.2170-linux/conf/sonar-scanner.properties

sonar.host.url=http://192.168.0.80:9000
sonar.sourceEncoding=UTF-8

4.5 配置 SonarQube Scanner

進入Jenkins系統管理 > 全域性工具配置

點選新增SonarQube Scanner,不勾選自動安裝,

配置SonarQube Scanner

Name: sonar

Sonar_Runner_Home: /opt/sonar-scanner-4.4.0.2170-linux

點選“儲存”

4.6 Jenkins配置檢查任務

Jenkins > 專案 > 增加構建任務

選擇Execute SonarQube Scanner

Task to run: scan # 即分析程式碼

Path to project properties: 這裡可以指定一個 sonar-project.properties 檔案,如果不指定的話會使用專案預設的 properties 檔案

Analysis properties : 這裡需要輸入一些配置引數用來傳遞給 SonarQube,

這裡的引數優先順序高於 sonar-project.properties 檔案裡面的引數,

所以可以在這裡來配置所有的引數以替代 sonar-project.properties 檔案

sonar.projectKey=eploicy:print
sonar.projectName=Winglung Epolicy Print
sonar.projectVersion=1.0
sonar.language=java
sonar.sourceEncoding=GBK
sonar.java.binaries=$WORKSPACE/webapps/WEB-INF/classes
sonar.sources=$WORKSPACE/component/com/sinosoft/application

註解:

sonar.projectKey:

sonar.java.binaries: 專案編譯後二進位制檔案存放目錄

sonar.sources: 需要分析的Java原始碼目錄

五、 使用Ant和SonarQube整合

5.1 下載SonarScanner for Ant

cd /opt
wget https://binaries.sonarsource.com/Distribution/sonarqube-ant-task/sonarqube-ant-task-2.7.0.1612.jar

5.2 編寫專案的build.xml

參考: https://github.com/SonarSource/sonar-scanning-examples

<?xml version="1.0" encoding="UTF-8"?>
<project name="Simple Project analyzed with the SonarQube Scanner for Ant" default="all" basedir="." xmlns:sonar="antlib:org.sonar.ant">
​
    <!-- ========= Define the main properties of this project ========= -->
    <property name="src.dir" value="src" />
    <property name="build.dir" value="target" />
    <property name="classes.dir" value="${build.dir}/classes" />
    
    <!-- Define the SonarQube global properties (the most usual way is to pass these properties via the command line) -->
    <property name="sonar.host.url" value="http://192.168.0.80:9000" />
​
    <!-- Define the Sonar properties -->
    <property name="sonar.projectKey" value="org.sonarqube:sonarqube-scanner-ant" />
    <property name="sonar.projectName" value="Example of SonarQube Scanner for Ant Usage" />
    <property name="sonar.projectVersion" value="1.0" />
    <property name="sonar.sources" value="src" />
    <property name="sonar.binaries" value="target" />
    <property name="sonar.sourceEncoding" value="UTF-8" />
    
    <!-- ========= Define "regular" targets: clean, compile, ... ========= -->
    <target name="clean">
        <delete dir="${build.dir}" />
    </target>
​
    <target name="init">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${classes.dir}" />
    </target>
​
    <target name="compile" depends="init">
        <javac srcdir="${src.dir}" destdir="${classes.dir}" fork="true" debug="true" includeAntRuntime="false" />
    </target>
​
    <!-- ========= Define SonarQube Scanner for Ant Target ========= -->
    <target name="sonar" depends="compile">
        <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
            <!-- Update the following line, or put the "sonar-ant-task-*.jar" file in your "$HOME/.ant/lib" folder -->
            <classpath path="/opt/sonarqube-ant-task-2.7.0.1612.jar" />
        </taskdef>
        
        <!-- Execute SonarQube Scanner for Ant Analysis -->
        <sonar:sonar />
    </target>
​
    <!-- ========= The main target "all" ========= -->
    <target name="all" depends="clean,compile,sonar" />
​
</project>