Continuously Integrated Code Quality Management--Sonar

Sonar is an open platform for code quality management. Through plug-in mechanisms, Sonar can integrate different testing tools, code analysis tools, and continuous integration tools.Unlike continuous integration tools such as Hudson/Jenkins, Sonar does not simply display the results of different code checks (such as FindBugs, PMD, and so on) directly on the webOn the UI interface, these results are reprocessed through different plug-ins, and changes in code quality are measured quantitatively, which makes it easy to manage code quality for projects of different sizes and types.

In terms of support for other tools, Sonar not only provides IDE support, but also allows you to view results online in tools such as Eclipse and IntelliJ IDEA; Sonar also provides interface support for a number of continuous integration tools, making it easy to use Sonar in continuous integration.

In addition, Sonar's plug-in provides support for programming languages other than Java, internationalization, and document reporting.

Sonar's function is to check if the code has a BUG.In addition to checking for bugs in your code, there are other features, such as: what is your code comment rate, some suggestions for code, and suggestions for grammar.So it is called quality management.

1. Deployment of Sonar

Next configuration is based on blog posts Deploying Jenkins+Gitlab for continuous integration Environment deployment
All source packages and plugins are available here link (Extract Code: t976) Download

[root@jenkins ~]# unzip sonarqube-5.6.zip 
[root@jenkins ~]# mv sonarqube-5.6/ /usr/local/sonarqube
[root@jenkins ~]# ln -s /usr/local/sonarqube/bin/linux-x86-64/sonar.sh /usr/local/bin/

2. Install MySQL

sonar needs to use a database, here I use a MySQL database, if there is a database in the environment, then no deployment is required, just create the appropriate library and account

#Deploy MySQL as an RPM package
[root@jenkins ~]# mkdir mysql
[root@jenkins ~]# cd mysql/
[root@jenkins mysql]# rz
[root@jenkins mysql]# ls            # Identify the following packages
mysql-community-client-5.7.25-1.el7.x86_64.rpm 
mysql-community-libs-compat-5.7.25-1.el7.x86_64.rpm
mysql-community-common-5.7.25-1.el7.x86_64.rpm  
mysql-community-server-5.7.25-1.el7.x86_64.rpm
mysql-community-libs-5.7.25-1.el7.x86_64.rpm
[root@jenkins mysql]# yum -y localinstall mysql-community-*       # Install MySQL
[root@jenkins mysql]# systemctl start mysqld         # start-up
[root@jenkins mysql]# netstat -anput | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      11411/mysqld        
[root@jenkins mysql]# grep password /var/log/mysqld.log        # View login passwords in the MySQL log 
2020-03-19T17:29:37.381630Z 1 [Note] A temporary password is generated for root@localhost: DfkgCtPsu5:4
#The default root password for MySQL is at the end of the above
[root@jenkins mysql]# mysql -u root -p'DfkgCtPsu5:4'
mysql> alter user 'root'@'localhost' identified by 'Zyz@123.com';
mysql> create database sonar character set utf8 collate utf8_general_ci;
mysql> grant all on sonar.* to 'sonar'@'%' identified by 'Zyz@123.com';
mysql> grant all on sonar.* to 'sonar'@'localhost' identified by 'Zyz@123.com';
mysql> flush privileges´╝Ť

3. Configuring Sonar

[root@jenkins mysql]# cd /usr/local/sonarqube/conf/
[root@jenkins conf]# sed -i 's/#sonar.jdbc.username=/sonar.jdbc.username=sonar/g' sonar.properties 
#Define the user to connect to the database
[root@jenkins conf]# sed -i 's/#sonar.jdbc.password=/sonar.jdbc.password=Zyz@123.com/g' sonar.properties
#Specify the password of the database user
[root@jenkins conf]# echo 'sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance' >> sonar.properties 
#Used to define the address and port to connect to the database
[root@jenkins conf]# sed -i 's/#sonar.web.port=9000/sonar.web.port=9000/g' sonar.properties
#Configure sonar to support Chinese pages and PHP
[root@jenkins conf]# cd /usr/local/sonarqube/extensions/plugins/
[root@jenkins plugins]# cp ~/sonar-l10n-zh-plugin-1.11.jar ./
[root@jenkins plugins]# cp ~/sonar-php-plugin-2.9-RC1.jar ./
[root@jenkins plugins]# sonar.sh start            #Start sonar, giving it point initialization time
[root@jenkins conf]# tail -2 /usr/local/sonarqube/logs/sonar.log              #View sonar log
2020.03.20 02:00:29 INFO  ce[o.s.ce.app.CeServer] Compute Engine is up
2020.03.20 02:00:29 INFO  app[o.s.p.m.Monitor] Process[ce] is up
#When the above two lines end with "up", sonar starts normally.
[root@jenkins conf]# netstat -anput | grep 9000
tcp        0      0 0.0.0.0:9000            0.0.0.0:*               LISTEN      52610/java   

client accesses sonar's web interface:

4. Configuration code scanning function

#Configuration Code Scan
[root@jenkins ~]# unzip sonar-scanner-cli-3.3.0.1492-linux.zip 
[root@jenkins ~]# mv sonar-scanner-3.3.0.1492-linux/ /usr/local/sonar-scanner
[root@jenkins ~]# ln -s /usr/local/sonar-scanner/bin/sonar-scanner /usr/local/bin/
[root@jenkins ~]# ln -s /usr/local/sonar-scanner/bin/sonar-scanner /usr/bin/
[root@jenkins ~]# cd /usr/local/sonar-scanner/conf/
[root@jenkins conf]# egrep -v '^#|^$' sonar-scanner.properties            # Modify the configuration file to the following
sonar.host.url=http://localhost:9000
sonar.sourceEncoding=UTF-8
#The following is copied from sonar's main configuration file: /usr/local/sonarqube/conf/sonar.properties to connect to the database
sonar.jdbc.username=sonar
sonar.jdbc.password=Zyz@123.com
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

5. Test Code Scanning Function

[root@jenkins ~]# unzip testalyzer-master.zip 
[root@jenkins ~]# cd testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests/
[root@jenkins php-sonar-runner-unit-tests]# cat sonar-project.properties 
#Look at what's contained in the file below
sonar.projectKey=org.sonarqube:php-ut-sq-scanner     #Custom keys, which automatically override previous test results if the keys are the same
sonar.projectName=PHP :: PHPUnit :: SonarQube Scanner        #Name of the web interface display
sonar.projectVersion=1.0      #Edition
sonar.sources=src        #Package Storage Path
sonar.tests=tests       #Test Path
sonar.language=php        #Language to test
sonar.sourceEncoding=UTF-8          #Encoding Format
#Testing PHP code
[root@jenkins php-sonar-runner-unit-tests]# pwd           # Determine Current Path
/root/testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests
[root@jenkins php-sonar-runner-unit-tests]# sonar-scanner 
 #Testing js code
[root@jenkins php-sonar-runner-unit-tests]# cd ../../javascript/javascript-sonar-runner
[root@jenkins javascript-sonar-runner]# sonar-scanner 

When you have finished testing js and PHP, you can see the following in the sonar's web interface:

Click to enter to see details:

6. Configure Jenkins to turn on Sonar

To log in to the web interface of Jenkins, you need to install plug-ins. There are two ways to install them online and offline. Here I choose to install them offline. You can install them online yourself.
1, click in turn: System Management==>> Plugin Management===>> Advanced, then drop-down page:
Download the plug-ins I provided and add them in the following order. If you install them online, search for "SonarQube Scanner", "Gerrit Trigger", "Sonar Gerrit Plugin" to install them:

2. Click in turn: System Administration==>> System Settings, and then configure as follows:


Keep points in mind to save
3. Click in turn: System Management==>> Global Tool Configuration, and then click as follows:


4. Enter the project you are building and click Enter as follows:



Copy the code at the terminal:

[root@jenkins /]# cd ~/testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests/
[root@jenkins php-sonar-runner-unit-tests]# egrep -v '^#|^$' sonar-project.properties 
#Copy the following code
sonar.projectKey=org.sonarqube:php-ut-sq-scanner
sonar.projectName=PHP :: PHPUnit :: SonarQube Scanner
sonar.projectVersion=1.0
sonar.sources=src
sonar.tests=tests
sonar.language=php
sonar.sourceEncoding=UTF-8
sonar.php.coverage.reportPath=reports/phpunit.coverage.xml
sonar.php.tests.reportPath=reports/phpunit.xml

5. Then paste the copied code in the diagram (it is recommended to modify the value of "sonar.projectKey" to distinguish test results):

6. Auto sonar scan can be achieved by returning to the terminal and submitting code to gitlab

[root@jenkins php-sonar-runner-unit-tests]# pwd
/root/testalyzer-master/projects/languages/php/php-sonar-runner-unit-tests
[root@jenkins php-sonar-runner-unit-tests]# cp -r * ~/test1/
#Copy the test code to the local git library (if there is no local git library, you can clone it again)
[root@jenkins php-sonar-runner-unit-tests]# cd ~/test1/           # Enter local git Library
[root@jenkins test1]# rm -rf sonar-project.properties              #Delete this file without using it to specify the code to be tested
#Because we have written its configuration file in the web interface just now.Prefer the detection code configured by the web interface.
#Do not delete this file
#Submit to remote gitlab Library
[root@jenkins test1]# git add *
[root@jenkins test1]# git commit -m "test sonar"
[root@jenkins test1]# git push origin master

When submitted to the remote gitlab library, you can see that the sonar's web interface has been scanned for code and the results are as follows:

7. Configure Jenkins Mail Alarm
Start configuring mail alerts:
In the web interface of Jenkins, click: System Administration===>> System Settings, then enter the mailbox address of the system administrator below and save:

Click again: System Management===>> System Settings

Configure the project as follows:




Click Project > Configure here




At this point, the mailbox alarm is configured, at which point you can close gitlab in the console or manually build code to test if you can receive an alarm message.

Tags: jenkins MySQL PHP JDBC

Posted on Fri, 20 Mar 2020 18:26:19 -0700 by Batosi