Catalogue
Measuring PHP Code Metrics with Jenkins + SonarQube!

Measuring PHP Code Metrics with Jenkins + SonarQube!

🌐 日本語で読む

Previously

In the previous article, I covered installing SonarQube on a server separate from Jenkins and getting to the point where it was accessible.

This time, I’ll walk through analyzing source code from Jenkins and displaying the metrics information in SonarQube.

Any language will do, but here I’ll use PHP.

Overview

Here is an overview.

Preparation on the SonarQube Side

Create a project and issue a project key.

1. Access the Login Page

http://:9000/sessions/new

By default, log in with the following admin:admin account.

Item Value
ID admin
PW admin

2. Create a Project

  • Click Administration in the header menu to go to the Administration page
  • Click Projects > Management
  • Click the Create button
  • Enter the Name and Key
  • You can see that the project has been added.

3. Install the PHP Plugin

  • On the Administration page, click System > Update Center
  • Select Available → type “PHP” in the search box → click Install on the PHP Plugin that appears
  • Restart to install the PHP Plugin into SonarQube
  • Confirm on the Installed tab that the PHP Plugin is installed

4. Issue an authentication token

  • Click Security > User
  • Click TOKENS to display the popup
  • Enter any string and click create
  • Copy the token.
    You’ll use it when configuring the Jenkins side.

That completes the preparation on the SonarQube side.

Preparation on the Jenkins Side

1. Install the SonarQube Plugin

Install the SonarQube Plugin from Manage Jenkins > Manage Plugins.

2. Install the SonarQube Scanner

Get the link from the official download page below.

Analyzing+with+SonarQube+Scanner

1
2
3
4
$ cd /var/lib/jenkins
$ wget https://sonarsource.bintray.com/Distribution/sonar-scanner-cli/sonar-scanner-2.6.1.zip
$ unzip sonar-scanner-2.6.1.zip
$ ln -s sonar-scanner-2.6.1 sonar-scanner

3. Jenkins System Configuration

  • Go to Manage Jenkins > Configure System

  • Fill in the required fields under JenkinsQube servers

  • Set the path of the sonar-scanner you installed earlier under SonarQube Scanner

Save after entering the above.

4. Create a New Job

Create a new job named “sonarqubeTest”.

  • Configure fetching the PHP project from the git repository
  • Configure the SonarScanner execution

That completes the configuration on the Jenkins side.

Verifying the SonarQube Results

Incidentally, this was an EC-CUBE 1.1 project.

EC-CUBE has a high code duplication rate, which shows just how much waste there is.

That’s all.

kenzo0107

kenzo0107