SonarQube代码质量扫描持续集成

1、安装JDK和配置JAVA_HOME和CLASSPATH2、安装mysql数据库3、创建数据库和用户mysql -u root -pmysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';mysql> FLUSH PRIVILEGES;4、下载sonarqubewget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.4.zipunzip sonarqube-5.4.zipvi /etc/profileSONAR_HOME=/usr/jenkins/sonarqube-5.4export SONAR_HOME    source /etc/profile5、配置sonarqube 一般需要配置以下属性(这里使用MySQL数据库,数据库的引擎必须是innodb):vi /usr/jenkins/sonarqube-5.4/conf/sonar.propertiessonar.jdbc.username=sonarsonar.jdbc.password=sonarsonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformancesonar.jdbc.maxActive=60sonar.jdbc.maxIdle=5sonar.jdbc.minIdle=2sonar.jdbc.maxWait=5000sonar.jdbc.minEvictableIdleTimeMillis=600000sonar.jdbc.timeBetweenEvictionRunsMillis=30000sonar.web.host=0.0.0.0#修改snoar端口,默认为9000sonar.web.port=90906、启动sonarqube服务    /usr/jenkins/sonarqube-5.4/bin/linux-x86-64/sonar.sh console 启动服务    /usr/jenkins/sonarqube-5.4/bin/linux-x86-64/sonar.sh start 启动服务/usr/jenkins/sonarqube-5.4/bin/linux-x86-64/sonar.sh stop 停止服务/usr/jenkins/sonarqube-5.4/bin/linux-x86-64/sonar.sh restart重启服务启动成功后在浏览器地址栏输入http://localhost:9090即可访问snoarQube。缺省用户名和密码是admin/admin7、汉化sonarqubehttps://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/安装目录:$SONAR_HOME/extensions/pluginswget https://github.com/SonarQubeCommunity/sonar-l10n-zh/releases/download/sonar-l10n-zh-plugin-1.9/sonar-l10n-zh-plugin-1.9.jar8、在jenkins平台上安装插件SonarQube Scanner for Jenkins9、在jenkins配置SonarQube 如果设置为5.3 or higher选择,就必须使用sonarQube account token进行连接,这个更安全。 
Server authentication token项需要在sonar server端生成 ;访问sonar server端 
菜单==》配置==》权限==》用户==》TOKENS==》Generate 生成token,将生成token添加到此处,完成添加。  10、下载安装和配置sonar scannerhttps://sonarsource.bintray.com/Distribution/sonar-scanner-cli/wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.8.zipvi /home/jenkins/.bash_profileSONAR_RUNNER_HOME=/usr/local/jenkins/sonar-scanner-2.8/export PATH=$SONAR_RUNNER_HOME/bin:$PATHsource /home/jenkins/.bash_profile测试是否安装sonner scanner成功输入命令:sonar-runner -version出现以下信息,则表示环境变量设置成功  vi conf/sonar-scanner.properties #Configure here general information about the environment, such as SonarQube DB details for example#No information about specific project should appear here#----- Default SonarQube serversonar.host.url=http://localhost:9090#----- Default source code encodingsonar.sourceEncoding=UTF-8#----- Global database settings (not used for SonarQube 5.2+)sonar.jdbc.username=sonarsonar.jdbc.password=sonar#----- MySQLsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf811、在jenkins中配置sonar scanner系统管理–》Global Tool Configuration  12、项目中添加sonar scanner分析13、要让jenkins编译完成,自动将代码传给sonarqube进行分析,则要配置jenkins中的目标项目。 
选择自己的project==》配置==》add pre-build step  14、遇到的问题在项目源码根目录执行命令时,可能会有svn错误,因为当sonar扫描的时候,会因为无法访问.svn而扫描失败,在sonar平台中用admin登录后,点击‘配置’-‘SCM’,如下图:    

相关内容推荐