• Jenkins+sonar7.3集成


    Jenkins安装请参考:https://blog.csdn.net/CheNorton/article/details/50327825?utm_source=copy

    Jenkins更新请参考:https://blog.csdn.net/u013201439/article/details/72984652

    sonar7.3安装

    整体的步骤是:

    1. 安装JDK支持
    2. 安装mySQL数据库
    3. 安装SonarQube

    安装包: https://pan.baidu.com/s/11pq11J3NhyciOsuF5gGLdw   提取码:z9qu

    上传安装包到 /tools/

    一:安装JDK

    rpm -ivh jdk-8u11-linux-x64.rpm
    [root@jason tools]# whereis java
    java: /usr/bin/java /usr/share/java

    将java配置加入环境变量

    [root@jason tools]# cat /etc/profile |grep -Ev '^$|^#'
    pathmunge () {
        case ":${PATH}:" in
            *:"$1":*)
                ;;
            *)
                if [ "$2" = "after" ] ; then
                    PATH=$PATH:$1
                else
                    PATH=$1:$PATH
                fi
        esac
    }
    if [ -x /usr/bin/id ]; then
        if [ -z "$EUID" ]; then
            # ksh workaround
            EUID=`/usr/bin/id -u`
            UID=`/usr/bin/id -ru`
        fi
        USER="`/usr/bin/id -un`"
        LOGNAME=$USER
        MAIL="/var/spool/mail/$USER"
    fi
    if [ "$EUID" = "0" ]; then
        pathmunge /usr/sbin
        pathmunge /usr/local/sbin
    else
        pathmunge /usr/local/sbin after
        pathmunge /usr/sbin after
    fi
    HOSTNAME=`/usr/bin/hostname 2>/dev/null`
    HISTSIZE=1000
    if [ "$HISTCONTROL" = "ignorespace" ] ; then
        export HISTCONTROL=ignoreboth
    else
        export HISTCONTROL=ignoredups
    fi
    export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
    if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
        umask 002
    else
        umask 022
    fi
    for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
        if [ -r "$i" ]; then
            if [ "${-#*i}" != "$-" ]; then 
                . "$i"
            else
                . "$i" >/dev/null
            fi
        fi
    done
    unset i
    unset -f pathmunge
    export PATH
    export JAVA_HOME=/usr/bin/java
    export SONAR_HOME=/tools/sonarqube-7.3/bin/linux-x86-64/
    export SONAR_RUNNER_HOME=/tools/sonar-scanner-2.8/bin/
    export PATH=$JAVA_HOME/bin:$SONAR_HOME:$SONAR_RUNNER_HOME:$PATH
    export CLASSPATH=./$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
    

     生效

    source /etc/profile

    二:安装mysql并创建sonar服务器数据库 

    安装数据库不赘述,创建sonar服务器数据库

    mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci; 
    mysql> CREATE USER 'sonar' IDENTIFIED BY 'jason_zhang';
    mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'jason_zhang';
    mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'jason_zhang';
    mysql> FLUSH PRIVILEGES;

    三:安装sonar7.3

    [root@jason tools]# unzip sonarqube-7.3
    [root@jason tools]# unzip sonar-scanner-2.8
    [root@jason tools]# chmod -R 777 /tools/
    [root@jason tools]# vim sonarqube-7.3/conf/sonar.properties
    [root@jason conf]# cat sonar.properties |grep -Ev '^$|^#'
    sonar.jdbc.username=sonar
    sonar.jdbc.password=jason_zhang
    sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerforman
    ce&useSSL=false
    sonar.web.host=0.0.0.0 sonar.web.port=9000

    由于sonar 是不允许root用户情况下运行的,因此需要先创建一个非root用户。 
    这里,创建一个名为sonar的用户,密码为jason_zhang 

    useradd sonar

    passwd sonar

    配置sonar用户环境变量

    [sonar@jason linux-x86-64]$ cd /home/sonar/
    [sonar@jason ~]$ cat .bash_profile |grep -Ev '^$|^#'
    if [ -f ~/.bashrc ]; then
        . ~/.bashrc
    fi
    PATH=$PATH:$HOME/.local/bin:$HOME/bin
    export PATH
    export JAVA_HOME=/usr/bin/java
    export SONAR_HOME=/tools/sonarqube-7.3/bin/linux-x86-64/
    export SONAR_RUNNER_HOME=/tools/sonar-scanner-2.8/bin/
    export PATH=$JAVA_HOME/bin:$SONAR_HOME:$SONAR_RUNNER_HOME:$PATH
    export CLASSPATH=./$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
    切换到sonar用户并启动sonar服务
    [root@jason bin]# su sonar
    [sonar@jason bin]$ cd /tools/sonarqube-7.3/bin/linux-x86-64/
    [sonar@jason linux-x86-64]$ ./sonar.sh start
    Starting SonarQube...
    Started SonarQube.

    查看sonar.log及es.log查看服务是否正常启动

    [root@jason ~]# tail -30f /tools/sonarqube-7.3/logs/sonar.log 
    2018.10.15 16:38:45 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='ce', ipcIndex=3, logFilenamePrefix=ce]] from [/tools/sonarq
    ube-7.3]: /usr/java/jdk1.8.0_11/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/tools/sonarqube-7.3/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:/tools/sonarqube-7.3/lib/jdbc/mysql/mysql-connector-java-5.1.46.jar org.sonar.ce.app.CeServer /tools/sonarqube-7.3/temp/sq-process7242945522727736634properties2018.10.15 16:38:57 INFO  app[][o.s.a.SchedulerImpl] Process[ce] is up
    2018.10.15 16:38:57 INFO  app[][o.s.a.SchedulerImpl] SonarQube is up
    TERM trapped.  Shutting down.
    2018.10.15 18:18:43 INFO  app[][o.s.a.SchedulerImpl] Stopping SonarQube
    2018.10.15 18:18:44 WARN  app[][o.s.a.p.AbstractProcessMonitor] Process exited with exit value [es]: 143
    2018.10.15 18:18:44 INFO  app[][o.s.a.SchedulerImpl] Process [es] is stopped
    2018.10.15 18:18:46 WARN  app[][o.s.a.p.AbstractProcessMonitor] Process exited with exit value [ce]: 143
    2018.10.15 18:18:46 INFO  app[][o.s.a.SchedulerImpl] Process [ce] is stopped
    2018.10.15 18:18:46 WARN  app[][o.s.a.p.AbstractProcessMonitor] Process exited with exit value [web]: 143
    2018.10.15 18:18:46 INFO  app[][o.s.a.SchedulerImpl] Process [web] is stopped
    2018.10.15 18:18:46 INFO  app[][o.s.a.SchedulerImpl] SonarQube is stopped
    <-- Wrapper Stopped
    --> Wrapper Started as Daemon
    Launching a JVM...
    Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
      Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.
    
    2018.10.16 11:19:50 INFO  app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /tools/sonarqube-7.3/temp
    2018.10.16 11:19:50 INFO  app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001
    2018.10.16 11:19:50 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/tools/sonarq
    ube-7.3/elasticsearch]: /tools/sonarqube-7.3/elasticsearch/bin/elasticsearch -Epath.conf=/tools/sonarqube-7.3/temp/conf/es2018.10.16 11:19:50 INFO  app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running
    2018.10.16 11:19:51 INFO  app[][o.e.p.PluginsService] no modules loaded
    2018.10.16 11:19:51 INFO  app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
    2018.10.16 11:20:07 INFO  app[][o.s.a.SchedulerImpl] Process[es] is up
    2018.10.16 11:20:07 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='web', ipcIndex=2, logFilenamePrefix=web]] from [/tools/sona
    rqube-7.3]: /usr/java/jdk1.8.0_11/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/tools/sonarqube-7.3/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:/tools/sonarqube-7.3/lib/jdbc/mysql/mysql-connector-java-5.1.46.jar org.sonar.server.app.WebServer /tools/sonarqube-7.3/temp/sq-process9015419538083373362properties2018.10.16 11:20:30 INFO  app[][o.s.a.SchedulerImpl] Process[web] is up
    2018.10.16 11:20:30 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='ce', ipcIndex=3, logFilenamePrefix=ce]] from [/tools/sonarq
    ube-7.3]: /usr/java/jdk1.8.0_11/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/tools/sonarqube-7.3/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:/tools/sonarqube-7.3/lib/jdbc/mysql/mysql-connector-java-5.1.46.jar org.sonar.ce.app.CeServer /tools/sonarqube-7.3/temp/sq-process8530700600938467792properties2018.10.16 11:20:42 INFO  app[][o.s.a.SchedulerImpl] Process[ce] is up
    2018.10.16 11:20:42 INFO  app[][o.s.a.SchedulerImpl] SonarQube is up

    [root@jason ~]# tail -30f /tools/sonarqube-7.3/logs/es.log
    2018.10.16 11:19:56 INFO es[][o.e.n.Node] initializing ...
    2018.10.16 11:19:57 INFO es[][o.e.e.NodeEnvironment] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [7.5gb], net total_space
    [16.9gb], spins? [unknown], types [rootfs]2018.10.16 11:19:57 INFO es[][o.e.e.NodeEnvironment] heap size [495.3mb], compressed ordinary object pointers [true]
    2018.10.16 11:19:57 INFO es[][o.e.n.Node] node name [sonarqube], node ID [biGYGRPKTfuUgAa7Ka6_bA]
    2018.10.16 11:19:57 INFO es[][o.e.n.Node] version[5.6.3], pid[7224], build[1a2f265/2017-10-06T20:33:39.012Z], OS[Linux/3.10.0-862.9.1.el7.x8
    6_64/amd64], JVM[Oracle Corporation/Java HotSpot(TM) 64-Bit Server VM/1.8.0_11/25.11-b03]2018.10.16 11:19:57 INFO es[][o.e.n.Node] JVM arguments [-XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiati
    ngOccupancyOnly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -Djdk.io.permissionsUseCanonicalPath=true, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Dlog4j.skipJansi=true, -Xms512m, -Xmx512m, -XX:+HeapDumpOnOutOfMemoryError, -Des.path.home=/tools/sonarqube-7.3/elasticsearch]2018.10.16 11:19:57 INFO es[][o.e.p.PluginsService] loaded module [aggs-matrix-stats]
    2018.10.16 11:19:57 INFO es[][o.e.p.PluginsService] loaded module [ingest-common]
    2018.10.16 11:19:57 INFO es[][o.e.p.PluginsService] loaded module [parent-join]
    2018.10.16 11:19:57 INFO es[][o.e.p.PluginsService] loaded module [percolator]
    2018.10.16 11:19:57 INFO es[][o.e.p.PluginsService] loaded module [reindex]
    2018.10.16 11:19:57 INFO es[][o.e.p.PluginsService] loaded module [transport-netty4]
    2018.10.16 11:19:57 INFO es[][o.e.p.PluginsService] no plugins loaded
    2018.10.16 11:20:00 INFO es[][o.e.d.DiscoveryModule] using discovery type [zen]
    2018.10.16 11:20:01 INFO es[][o.e.n.Node] initialized
    2018.10.16 11:20:01 INFO es[][o.e.n.Node] starting ...
    2018.10.16 11:20:02 INFO es[][o.e.t.TransportService] publish_address {127.0.0.1:9001}, bound_addresses {127.0.0.1:9001}
    2018.10.16 11:20:02 WARN es[][o.e.b.BootstrapChecks] max file descriptors [4096] for elasticsearch process is too low, increase to at least
    [65536]2018.10.16 11:20:02 WARN es[][o.e.b.BootstrapChecks] max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262
    144]2018.10.16 11:20:05 INFO es[][o.e.c.s.ClusterService] new_master {sonarqube}{biGYGRPKTfuUgAa7Ka6_bA}{LS_FmahOSBeh2VAzSqbAbw}{127.0.0.1}{127.
    0.0.1:9001}{rack_id=sonarqube}, reason: zen-disco-elected-as-master ([0] nodes joined)2018.10.16 11:20:05 INFO es[][o.e.n.Node] started
    2018.10.16 11:20:05 INFO es[][o.e.g.GatewayService] recovered [8] indices into cluster_state
    2018.10.16 11:20:07 INFO es[][o.e.c.r.a.AllocationService] Cluster health status changed from [RED] to [GREEN] (reason: [shards started [[co
    mponents][2], [metadatas][0]] ...]).

     

    http://192.168.199.251:9000

    默认账户:admin

    密码 :admin

    汉化

    生成SonarQube身份验证令牌

     

    Jenkins和sonarqube集成

    1.安装插件

    2.系统管理--系统设置

    系统管理--全局工具设置

     3.配置sonar-scan-2.8的sonar-scanner.properties配置文件

    root@jason sonar-scanner-2.8]# cat conf/sonar-scanner.properties |grep -Ev '^$|^#'
    
    sonar.host.url=http://192.168.199.251:9000
    sonar.sourceEncoding=UTF-8
    sonar.jdbc.username=root
    sonar.jdbc.password=jason_zhang
    sonar.jdbc.url=jdbc:mysql://192.168.199.251:3306/sonar?useUnicode=true&characterEncoding=utf8
    

     配置sonar-scanner

    cd /tools/sonar-scanner-2.8/bin/
    vim sonar-scanner
    将
    if [ -n "$JAVA_HOME" ]
    then
      JAVA_CMD="$JAVA_HOME/bin/java"
    else
      JAVA_CMD="`which java`"
    fi
    
    修改为
    if [ -n "$JAVA_HOME" ]
    then
      JAVA_CMD="/usr/bin/java"
    else
      JAVA_CMD="`which java`"
    fi
    

     将sonar-scanning-examples-master.zip测试源码放入Jenkins的test项目目录并解压

     

    [root@jason workspace]#cd /var/lib/jenkins/workspace
    [root@jason workspace]# ls
    jason  jason_test  maven  sw.test.c5game.cn.tar.gz  test  web

     

    [root@jason workspace]# cd test/
    [root@jason test]# ls
    copybooks  coverage-report  sonar-project.properties  src
    

    编辑sonar-project.properties

    sonar.projectKey=test
    sonar.projectName=test
    sonar.projectVersion=1.0
    
    #sonar.sources=src,copybooks
    sonar.sources=src,copybooks
    # Language
    
    
    sonar.sourceEncoding=UTF-8
    
    ## Cobol Specific Properties
    
    # comma-separated paths to directories with copybooks
    sonar.cobol.copy.directories=copybooks
    # comma-separated list of suffixes
    sonar.cobol.file.suffixes=cbl,cpy
    sonar.cobol.copy.suffixes=cpy
    
    
    
    ## Flex Specific Properties
    
    # retrieve code coverage data from the Cobertura report
    sonar.flex.cobertura.reportPath=coverage-report/coverage-cobertua-flex.xml
    
    
    
    # PL/I Specific Properties
    sonar.pli.marginLeft=2
    sonar.pli.marginRight=0
    

     chmod  777 sonar-project.properties

    修改Jenkins的test项目配置

     

     Analysis properties里面填写的就是sonar-project.properties的内容

     返回test项目,点击构建,成功后可以看到

     

    点击sonarqube跳转到

     

    以上这个是项目源存在于Jenkins服务器的情况,假如项目源和Jenkins不在同一台服务器上

    客户机 192.168.199.224

    思路:在工程中配置好sonar-project.properties后,直接bash调用进入到工程中,执行sonar-scan也是可以的,因为sonar-scan就是依据sonar-project.properties来执行的,和界面填写,效果一样) 

    上传sonar-scan客户端和测试源码到该服务器的tools文件夹

    [root@jason tools]# unzip sonar-scanning-examples-master.zip
    [root@jason tools]# unzip sonar-scanner-2.8.zip
    [root@jason tools]# mv sonar-scanning-examples-master  jason_zhang
    
    修改sonar-scanner-2.8.zip的sonar-scanner.properties和sonar-scanner 同上
    

    修改sonar-project.properties配置文件

    [root@jason conf]# cd  /tools/jason_zhang//sonarqube-scanner
    [root@jason sonarqube-scanner]# cat sonar-project.properties |grep -Ev '^$|^#'
    sonar.projectKey=jason_zhang
    sonar.projectName=jason_zhang
    sonar.projectVersion=1.0
    sonar.sources=src,copybooks
    sonar.sourceEncoding=UTF-8
    sonar.cobol.copy.directories=copybooks
    sonar.cobol.file.suffixes=cbl,cpy
    sonar.cobol.copy.suffixes=cpy
    sonar.flex.cobertura.reportPath=coverage-report/coverage-cobertua-flex.xml
    sonar.pli.marginLeft=2
    sonar.pli.marginRight=0
    

    chmod  -R  777  /tools/

    ln -s /tools/sonar-scanner-2.8/bin/sonar-scanner /usr/bin/sonar-scanner

    jenkins服务器安装插件 publish over ssh

    系统管理--系统设置

     

      创建jason_zhang项目并配置,注意路径

    构建完成

    登录sonarqube  http://192.168.199.251:9000

    sonar-project.properties配置参考https://my.oschina.net/jiangbianwanghai/blog/479285

    sonar.projectKey=jason
    sonar.projectName=jason
    sonar.projectVersion=1.0 
    sonar.sources=/var/lib/jenkins/workspace/jason
    sonar.language=js
    sonar.sourceEncoding=UTF-8
    

    开机自动启动sonarqube

    vim /etc/rc.d/rc.local
    su sonar
    sh /tools/sonarqube-7.3/bin/linux-x86-64/sonar.sh start
    
    chmod +x   /etc/rc.d/rc.local
    

    如果使用root账户启动过之后再使用sonar账户使用也是会失败的,需要进入/tools/sonarqube-7.3/temp 清空该目录再使用sonar账户启动sonarqube 

     

    参考:

    https://www.cnblogs.com/tongxiaoda/p/8484308.html

    https://blog.csdn.net/weixin_41004350/article/details/78492533

    https://blog.csdn.net/weixin_41004350/article/details/78491472 (安装Java)
    http://tool.uml.com.cn/iWork/SonarQube.asp

    https://www.cnblogs.com/weschen/p/6023153.html 
    https://segmentfault.com/a/1190000008716005
    https://www.jianshu.com/p/4c6f9361565b      (Elasticsearch 报错)

    http://www.cnblogs.com/zndxall/archive/2018/07/19/9336879.html

    https://www.cnblogs.com/parryyang/p/6270402.html
    https://www.cnblogs.com/nmap/p/6792148.html

  • 相关阅读:
    Angular @Input讲解及用法
    跨浏览器窗口通讯 ,7种方式
    map和forEach的区别
    纯CSS圆环与圆
    如何理解时间复杂度和空间复杂度
    毁灭
    P1631 序列合并
    plotly dash
    Tkinter
    mysql 和 sqlalchemy 的一个测试环境
  • 原文地址:https://www.cnblogs.com/xiaoyou2018/p/9796654.html
Copyright © 2020-2023  润新知