Monday 28 May 2018

Integrating SonarQube with Jenkins for PHP


SonarQube is a quality management tool popularly used among 85000 companies around the world. It has four editions.
  • Community Edition ( free & open source )
  • Developer Edition
  • Enterprise Edition
  • Data Center Edition

Note: Community Edition is more than enough for company perspective.



For more details Continuous Code Quality| SonarQube

Let's see how we can integrate SonarQube with Jenkins for PHP.

I assume that already CI/CD for projects with Jenkins and GitHub has been configured. If you want to CI/CD with Jenkins, follow Continuous Integration with Jenkins and GitHub to automate the deployment article.

Prerequisites

  • Ubuntu 16.04 server instance with at least 2GB RAM.
  • A sudo user
  • Jenkins server with reverse proxy enabled using Apache

Step 1: Perform a system update

sudo apt-get update
sudo apt-get -y upgrade
Note: Make sure java is already installed. If not follow the below steps to install java.
sudo apt-get install openjdk-7-jdk
java -version

Step 2: Download and configure SonarQube

wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.7.3.zip
For more versions you can check the link in SonarQube download page.
  • Install unzip by running below code.
sudo apt-get -y install unzip
  • Unzip the archive by using below command.
sudo unzip sonarqube-6.7.3.zip -d /opt
  • Rename the directory
sudo mv /opt/sonarqube-6.7.3 /opt/sonarqube
  • Open the SonarQube configuration file.
vi /opt/sonarqube/conf/sonar.properties
  • Find and uncomment the following line with the username,password and url of the SQL Azure and save the file.
sonar.jdbc.username=test
sonar.jdbc.password=test@123
sonar.jdbc.url=sqlserver://localhost;databaseName=_sonar
Note : If you don't change above lines,by default SonarQube will take embedded database for evaluation purpose. SonarQube will not start with root user from SonarQube 6.7, therefore we have to do necessary changes to up the sonarQube server. Give permission to your server user, for example in my case I have a user called qauser and I need to give below permission to support the startup.
chown -R qauser:qauser /opt/sonarqube
  • Now go to below path.
vi /opt/sonarqube/bin/linux-x86-64/sonar.sh
  • Find the below line and change it as shown
RUN_AS_USER=qauser

Step 3:Configure Systemd service 

  • SonarQube can be started directly using the startup script provided in the installer package. As matter of convenience, you should setup a Systemd unit file for SonarQube. In order to achieve it follow the below.
vi /etc/systemd/system/sonar.service
  • Now populate the file with below code.
[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=forking

ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop

User=qauser
Group=qauser
Restart=always

[Install]
WantedBy=multi-user.target
  • Start the server by running as shown below.
sudo systemctl start sonar
  • Enable the SonarQube service automatically start at boot time.
sudo systemctl enable sonar
  • To check if the service is running
sudo systemctl status sonar

Step 4: Configure reverse proxy

  • By default SonarQube listens to localhost port 9000. We will use Apache to reverse proxy the application.
  •  First, open to the below file
vi /opt/sonarqube/conf/sonar.properties
  • Uncomment and change the below code accordingly.
 sonar.web.context=/sonarqube
  • Open the file below
 vi /etc/apache2/site-available/default-ssl.conf
  • Finally add the below line and restart the Apache server.
 
ProxyPass         /sonarqube  http://localhost:9000/sonarqube nocanon
ProxyPassReverse  /sonarqube  http://localhost:9000/sonarqube


     Order deny,allow
     Allow from all


  • SonarQube is up and running on your server,access the dashboard at following address
http(s)://yourdomain.com/sonarqube
  • Log in using the initial administrator account (username,password- admin) and start analyzing the code.

Step 5: Configure SonarQube with Jenkins

  • Log in to Jenkins portal,go to Manage Jenkins tab.
  • Then select Manage Plugins and install SonarQube Scanner for Jenkins.
  • Go to Configure System in Manage Jenkins tab and configure the SonarQube server installed as shown below.


  • Now go to Configure Global Security and do the below changes under CSRF Protection.



  • Then go to Global Tool Configuration and add SonarQube Scanner as shown below.


  • Go to project configuration and under Build Environment tick 'Prepare SonarQube Scanner environment' checkbox.
  • Then click Add build step and select 'Execute SonarQube Scanner' and following lines.

sonar.projectKey=projectKey
sonar.projectName=projectName
sonar.projectVersion=${BUILD_NUMBER} (optional)
sonar.sources=.



  • Finally log in to SonarQube with administrator privilege,under 'Administration' select 'Marketplace' sub tab and search for SonarPHP (plugin for PHP) as shown below.




  • That's all, now restart the SonarQube server after SonarPHP installation and continue to analyze your code.




 Is there anything you need to clarify about SonarQube? Let me know in the comment below.

No comments:

Post a Comment

Integrating SonarQube with Jenkins for PHP

SonarQube  is a quality management tool popularly used among 85000 companies around the world. It has four editions. Community Edition ( ...