Continuous Code Quality for PL/SQL with Docker

In this blog post I show step by step how to set up a continuous code quality inspection environment for a PL/SQL project hosted on GitHub. I’m going to use a Docker container for SonarQube and another container for Jenkins.

Here is the table of content of the major steps.

  1. Install Docker
  2. Create SonarQube und Jenkins Container
  3. Install PL/SQL Cop for SonarQube
  4. Install PL/SQL Cop (Command Line Utility)
  5. Configure SonarQube
  6. Configure Jenkins
  7. Create Code Analysis Job
  8. View Result in SonarQube
  9. Summary

In the summary of this post you find an audioless video completing the process within 3.5 minutes.

1. Install Docker

I’m assuming that you already have installed Docker on your machine. If not, download and install the free Docker Community Edition including Docker Compose. I’m going to use Docker for Mac. But you may use Docker for Windows or a Docker server for one of the various supported Linux distributions. It is also possible to use a cloud provider, but the installation procedure will differ slightly.

You are ready for the next steps when the command

produces this output

in your terminal window.

2. Create SonarQube and Jenkins Container

In this step we create a container for SonarQube and another one for Jenkins. Jenkins will need to communicate with SonarQube. Docker Compose allows us to use hostnames instead of hard coded IP addresses and to manage all involved containers together in a single YAML configuration file.

Create a “docker-compose.yml” file in a “plsqlcop” directory. The directory name is used to derive names for containers and volumes. In this case the volumes will be named “plsqlcop_sonardata” and “plsqlcop_cidata”. For the container names the default naming has been overridden.

Make sure you are located in the directory where the “docker-compose.yml” file is stored. Then create the containers in the background by running

You should get an output similar to the following:

3. Install PL/SQL Cop for SonarQube

To install the current version of PL/SQL Cop for SonarQube within the “sonar” container run

Windows users please note, that the \ (backslash) at the end of the first line is just the Unix line-continuation character (as the ` (grave accent) in PowerShell), you should omit it. The wget command will be executed within the sonar container.

To load the plugin we need to restart the container.

4. Install PL/SQL Cop (Command Line Utility)

To install the current version of PL/SQL Cop within the “ci” container run

Why do we need to install this component in the “ci” container and not in the “sonar” container? Well, the SonarQube scanner is executed in the “ci” container and gets the code analyzer from the SonarQube server. This increases the scalability of the code analyzers since just the analysis reports need to be sent to the SonarQube server. However, the PL/SQL Cop SonarQube plugin is basically a wrapper to the command line utility. This means that every Jenkins slave needs to have PL/SQL Cop installed in the location configured on the SonarQube server.

In a environment with multiple servers and multiple Jenkins slaves the PL/SQL Cop installations need to be identical on every slave.

5. Configure SonarQube

Open “http://localhost:9010” in your web browser.

Press “Log in” in the upper right corner and login into SonarQube with the username “admin” and password “admin”.

Click on “Settings” and the Category “Trivadis PL/SQL Cop” and change the “Path to PL/SQL Cop command line tvdcc executable” to “/opt/tvdcc/tvdcc.sh”.

Press “Save Trivadis PL/SQL Cop Settings” and your done.

6. Configure Jenkins

Open “http://localhost:9020” in your web browser.

Jenkins generated during the container creation a password for the user admin. To get this password execute in a terminal window the following:

Copy the output into the clipboard and paste it into the “Administrator password” field in the browser window and press “Continue”.

Press on “Install Suggested Plugins”

Wait until all plugins are installed.

Enter the requested fields and press “Save and Finish”. And on the next page Click on “Start Using Jenkins”

Click on “Mange Jenkins”.

Click on “Manage Plugins”.

Select “SonarQube Scanner for Jenkins” in the “Available” tab. Use the filter to find the entry. Press “Install without restart”.

Click on “Go back to top page” when the plugins have been installed successfully.

Click on “Manage Jenkins” (again) and then on “Global Tool Configuration”.

Within the Global Tool Configuration click on “Add SonarQube Scanner”, add a name for the scanner and press “Save”.

Click on “Configure System”.

Scroll down to the “SonarQube servers” section and click on “Add SonarQube”.

Enter “SonarQube 5.1.2” for name, enter “http://sonar:9000” for “Server URL”, select “5.1 or lower” in “Server version” and enter “jdbc:h2:tcp://sonar/sonar” for “Database URL”. Click on “Save” to complete the Jenkins configuration.

Please note, that we are accessing the host sonar via the internal network Docker Compose has set up for us. Therefore we have to use the default port 9000 and not port 9010 which we are using from outside, e.g. to access SonarQube in the web browser.

 

7. Create Code Analysis Job

Click on “create new jobs”.

Enter “plscope-utils” and click on “Freestyle project”

Click on the “Source Code Management” tab, select “Git”, enter “https://github.com/PhilippSalvisberg/plscope-utils” for “Repository URL”, add “Sparse Checkout paths” in “Additional Behaviours”, enter “database/utils” for “Paths” and click on “Apply”.

Click on “Build Triggers” tab, select “Poll SCM”. Enter “H/15 * * * *” for “Schedule” and press “Apply”. This will look for changes in the Git repository every 15 minutes and starts a build if a change is detected.

Click on “Build” tab and Scroll down to the Build section and add a “Execute SonarQube Scanner” build step.

Copy the following lines into the “Analysis properties”.

and click on “Save”.

Wait until the initial build is started or click on “Build Now” and then on “#1” (the first build job in the Build History).

Click on “Console Output”.

The analysis result has been stored successfully in SonarQube and can now be queried via http://localhost:9010 .

Please note, that the link to SonarQube provided in the console output is not working, since it is referring a port of the internal Docker Compose network which is not accessible to your browser. However, it is possible to configure the external IP address and port for SonarQube in Jenkins and than this link will work.

8. View Result in SonarQube

Open “http://localhost:9010” in your web browser.

Click on “plscope-utils”,  select the “Issues” tab for this project and select all rules.

Click on “>” at the right side of an issue to see the source code line causing this issue.

See https://www.sonarqube.org/features/clean-code/ for more information.

9. Summary

Setting up an continuous code quality inspection environment for a PL/SQL project with Docker is quite simple. The audioless video documents the complete process.

Granted, for production use you need to use a different database as SonarQube backbone, define some roles and manage users and probably integrate an Active Directory. That should not be too difficult, at least not technijcally.

But the most challenging part will be to agree on some rules, adapt your development process and really improve your code quality over time. So, let’s get started.

Leave a Reply

Your email address will not be published. Required fields are marked *