Jenkins集成Sonarqube

1. docker安装sonar

docker -search sonar

docker pull sonarqube

docker run --restart=always -d --name sonarqube -p9000:9000 -e sonar.jdbc.username=admin -e sonar.jdbc.password=admin sonarqube

Admin@123

访问localhost:9000,可以成功进入sonar的ui界面

生成token



记住token的内容

2. jenkins配置

安装jenkins-sonar插件

配置系统全局变量,认证方式选择“secret text”

把生成的token内容填入Secret里面

3. pipeline构建

编写jenkinsfile文件

pipeline {

    agent none

    stages {

      stage('Preparation') {

            agent { node { label 'master' } }

            steps {

              checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'binbin', url: 'https://github.com/zbbkeepgoing/springboot-demo.git']]])

            }

        }


        stage('Build') {

            agent { node { label 'master' } }

            steps {

                dir(env.WORKSPACE){

                  sh "mvn clean install"

                  junit allowEmptyResults: true, keepLongStdio: true, testResults: 'target/surefire-reports/*.xml'

                  sh "mv target/sample-0.0.1-SNAPSHOT.jar target/sample.jar"

                }

            }

        }

        stage('Sonarqube') {

stage('测试代码静态扫描') {                                                                                                           steps    { echo 'This is a sonar step' sh "mvn sonar:sonar -Dsonar.host.url=http://10.10.15.172:9000 -Dsonar.login=sonartoken" //指定sonar的ip和token }

        }

    }

}

4. jenkins构建成功

[Pipeline] stage[Pipeline] { (测试代码静态扫描)[Pipeline] echoThis is a sonar step[Pipeline] sh+ mvn sonar:sonar -Dsonar.host.url=http://10.10.15.172:9000-Dsonar.login=13dc05980e001f8894ae6b3d21a4816ab02da799

[INFO] Scanning for projects...

[WARNING] [WARNING] Some problems were encountered while building the effective model for com.wpg:wpg-demo-project:pom:1.0-SNAPSHOT

[WARNING] 'dependencyManagement.dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: io.seata:seata-spring-boot-starter:jar -> duplicate declaration of version 1.4.1 @ com.wpg:wpg-cloud:1.7-SNAPSHOT, /home/repository/com/wpg/wpg-cloud/1.7-SNAPSHOT/wpg-cloud-1.7-SNAPSHOT.pom, line 127, column 25

[WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.

[WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.

[WARNING] [INFO] ------------------------------------------------------------------------[INFO] Reactor Build Order:[INFO] [INFO] wpg-demo-project[INFO] wpg-demo-yxproject-api[INFO] wpg-demo-yxproject-web[INFO] [INFO] ------------------------------------------------------------------------[INFO] Building wpg-demo-project 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[WARNING] The POM for com.google.guava:guava:jar:29.0-jre is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details[INFO] [INFO] --- sonar-maven-plugin:3.9.0.2155:sonar (default-cli) @ wpg-demo-project ---[INFO] User cache: /root/.sonar/cache[INFO] SonarQube version: 6.7.5[INFO] Default locale: "zh_CN", source code encoding: "UTF-8" (analysis is platform dependent)[INFO] Publish mode[INFO] Load global settings[INFO] Load global settings (done) | time=79ms[INFO] Server id: BF41A1F2-AXmNjGGJ4jMLrd5txmjD[INFO] User cache: /root/.sonar/cache[INFO] Load plugins index[INFO] Load plugins index (done) | time=53ms[INFO] Process project properties[INFO] Load project repositories[INFO] Load project repositories (done) | time=77ms[INFO] Load quality profiles[INFO] Load quality profiles (done) | time=35ms[INFO] Load active rules[INFO] Load active rules (done) | time=1203ms[INFO] Load metrics repository[INFO] Load metrics repository (done) | time=70ms[INFO] Project key: com.wpg:wpg-demo-project[INFO] ------------- Scan wpg-demo-yxproject-web[INFO] Load server rules[INFO] Load server rules (done) | time=586ms[INFO] Base dir: /root/jenkins/workspace/wpg-demo-yxproject-web/wpg-demo-yxproject-web[INFO] Working dir: /root/jenkins/workspace/wpg-demo-yxproject-web/wpg-demo-yxproject-web/target/sonar[INFO] Source paths: pom.xml, src/main/java[INFO] Test paths: src/test/java[INFO] Source encoding: UTF-8, default locale: zh_CN[INFO] Index files[INFO] 19 files indexed[INFO] Quality profile for java: Sonar way[INFO] Quality profile for xml: Sonar way[INFO] Sensor JavaSquidSensor [java][INFO] Configured Java source version (sonar.java.source): 8[INFO] JavaClasspath initialization[INFO] JavaClasspath initialization (done) | time=16ms[INFO] JavaTestClasspath initialization[INFO] JavaTestClasspath initialization (done) | time=6ms[INFO] Java Main Files AST scan[INFO] 16 source files to be analyzed[INFO] 16/16 source files have been analyzed[INFO] Java Main Files AST scan (done) | time=1617ms[INFO] Java Test Files AST scan[INFO] 2 source files to be analyzed[INFO] 2/2 source files have been analyzed[INFO] Java Test Files AST scan (done) | time=102ms[INFO] Sensor JavaSquidSensor [java] (done) | time=2178ms[INFO] Sensor SurefireSensor [java][INFO] parsing [/root/jenkins/workspace/wpg-demo-yxproject-web/wpg-demo-yxproject-web/target/surefire-reports][INFO] Sensor SurefireSensor [java] (done) | time=3ms[INFO] Sensor JaCoCoSensor [java][INFO] Sensor JaCoCoSensor [java] (done) | time=1ms[INFO] Sensor SonarJavaXmlFileSensor [java][INFO] 1 source files to be analyzed[INFO] Sensor SonarJavaXmlFileSensor [java] (done) | time=357ms[INFO] Sensor XML Sensor [xml][INFO] 1/1 source files have been analyzed[INFO] Sensor XML Sensor [xml] (done) | time=98ms[INFO] Sensor Analyzer for "php.ini" files [php][INFO] Sensor Analyzer for "php.ini" files [php] (done) | time=3ms[INFO] ------------- Scan wpg-demo-yxproject-api[INFO] Base dir: /root/jenkins/workspace/wpg-demo-yxproject-web/wpg-demo-yxproject-api[INFO] Working dir: /root/jenkins/workspace/wpg-demo-yxproject-web/wpg-demo-yxproject-api/target/sonar[INFO] Source paths: pom.xml[INFO] Source encoding: UTF-8, default locale: zh_CN[INFO] Index files[INFO] 1 file indexed[INFO] Quality profile for xml: Sonar way[INFO] Sensor SonarJavaXmlFileSensor [java][INFO] 1 source files to be analyzed[INFO] Sensor SonarJavaXmlFileSensor [java] (done) | time=37ms[INFO] 1/1 source files have been analyzed[INFO] Sensor XML Sensor [xml][INFO] Sensor XML Sensor [xml] (done) | time=8ms[INFO] Sensor Analyzer for "php.ini" files [php][INFO] Sensor Analyzer for "php.ini" files [php] (done) | time=0ms[INFO] ------------- Scan wpg-demo-project[INFO] Base dir: /root/jenkins/workspace/wpg-demo-yxproject-web

[INFO] Working dir: /root/jenkins/workspace/wpg-demo-yxproject-web/target/sonar[INFO] Source paths: pom.xml

[INFO] Source encoding: UTF-8, default locale: zh_CN

[INFO] Index files[INFO] 1 file indexed

[INFO] Quality profile for xml: Sonar way

[INFO] Sensor SonarJavaXmlFileSensor

[java][INFO] 1 source files to be analyzed

[INFO] Sensor SonarJavaXmlFileSensor [java] (done) | time=44ms

[INFO] Sensor XML Sensor [xml]

[INFO] 1/1 source files have been analyzed

[INFO] Sensor XML Sensor [xml] (done) | time=15ms[INFO] Sensor Analyzer for "php.ini" files

[php][INFO] Sensor Analyzer for "php.ini" files

[php] (done) | time=0ms

[INFO] Sensor Zero Coverage Sensor

[INFO] Sensor Zero Coverage Sensor (done) | time=28ms

[INFO] Sensor CPD Block Indexer

[INFO] Sensor CPD Block Indexer (done) | time=35ms[INFO] 8 files had no CPD blocks

[INFO] Calculating CPD for 8 files

[INFO] CPD calculation finished

[INFO] Analysis report generated in 93ms, dir size=134 KB[INFO] Analysis reports compressed in 43ms, zip size=65 KB

[INFO] Analysis report uploaded in 68ms

[INFO] ANALYSIS SUCCESSFUL, you can browsehttp://10.10.15.172:9000/dashboard/index/com.wpg:wpg-demo-project

[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 athttp://10.10.15.172:9000/api/ce/task?id=AXmOc8cJO8IdsgbY4ga-

[INFO] Task total time: 6.601 s

[INFO] ------------------------------------------------------------------------[INFO] Reactor Summary:[INFO] [INFO] wpg-demo-project ...................................SUCCESS [ 9.326 s]

[INFO] wpg-demo-yxproject-api ............................. SKIPPED

[INFO] wpg-demo-yxproject-web ............................. SKIPPED

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 10.009 s

[INFO] Finished at: 2021-05-21T18:23:42+08:00

[INFO] Final Memory: 93M/1165M

[INFO] ------------------------------------------------------------------------

5. sonar平台查看




                                            以下为非docker式傻瓜安装sonar平台

环境准备

(1) sonarqube-7.9.1

(2) sonar-scanner-4.0.0.1744-linux

(3) Sqlserver(sonar7.9之后版本不支持mysql,oracle必须使用ojdbc7或者更高版本)

(4) jenkins 

(5) jdk-12.0.1(jdk必须使用jdk11或者是更高版本)

注意:sonar的安装和启动不用root用户,而是用低权限的linux用户,建议自建用户sonar。 


1. 新增sonar用户

useradd sonar

passwd 

cd /home/sonar

mkdir sonar

2. 上传文件


unzip sonarqube-8.9.0.43852.zip

按如下配置修改 $SONAR_HOME/conf/sonar.properties 文件,配置数据库信息:

sonar.jdbc.username=sa

sonar.jdbc.password=Sqlserver123

sonar.jdbc.url=jdbc:sqlserver://10.102.7.210;databaseName=sonar

#sonar访问地址端口可根据情况修改或者保持默认

sonar.web.port=9000

3. 安装 sqlserver

yum install -y mssql-server


/opt/mssql/bin/mssql-conf setup

systemctl status mssql-server

开1433端口

firewall-cmd --add-port='1433/tcp' --permanent

firewall-cmd –reload

yum install -y mssql-tools

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile

echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc

source ~/.bashrc


创建sonar数据库

4. 安装jdk,注意sonar版本与jdk版本对应

yum install java

cd linux-x86-64

5.启动sonar

./sonar.sh start

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,657评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,889评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,057评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,509评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,562评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,443评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,251评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,129评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,561评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,779评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,902评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,621评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,220评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,838评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,971评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,025评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,843评论 2 354

推荐阅读更多精彩内容