SonarQube, TeamCity and SQL Server

Today, I was at a nice meeting with fellow IT architects in Aarhus. At the meetup, Jesper Færgemann gave a nice presentation on static and dynamic code analysis, and I got inspired. So, in the train on my way back home, I decided to try out SonarQube.

In my curent project, we are using TeamCity for CI purposes, and I decided to go ahead and integrate SonarQube in our TeamCity setup, using the SQL Server standard edition that we already had running in our dev env. Turned out, that the train ride was not enough time to complete this setup for me – below are the issues that I struggled with. I spent approx. 3 hours of effective work on this. Some of the issues seem obvious when I read them now, admittedly. Anyway, this might save you some time, in case you want to do a similar setup.

Using localhost for the SQ web server

Since I already had a SQL Server standard edition running on my dev/build server, I decided to use that DB with SonarQube.

Initially, I followed along with the documentation rather headless and used 192.0.0.1:9018 as web server URL. This gave me:

Failed to initialize end point associated with ProtocolHandler ["http-bio-192.0.0.1-9018"]

– in the SonarQube log file.

Solutions:

If you only want to be able to access SonarQube from the localhost, use the loopback ip: 127.0.0.1 .

If you want to able to access the SonarQube web server/dashboard from remote, use ip: 0.0.0.0. (This is what I currently use).

See this SO article for details.

Fail to request server version

After having configured the SQ plug-in in TeamCity as described here, my TeamCity build failed with the following:

[16:41:35][Step 5/5] ERROR: Fail to request server version
[16:41:35][Step 5/5] INFO: ------------------------------------------------------------------------
[16:41:35][Step 5/5] ERROR: Caused by: no protocol: 127.0.0.1:9018/api/server/version
Solution: I missed entering the protocol (http) – duh! Once added, I could move on to the next error.

Fail to connect to database

[17:36:21][Step 5/5] ERROR: Error during Sonar runner execution
[17:36:21][Step 5/5] ERROR: Unable to execute Sonar
[17:36:21][Step 5/5] ERROR: Caused by: Fail to connect to database
[17:36:21][Step 5/5] ERROR: Caused by: Cannot create PoolableConnectionFactory (Login failed for user 'sonar'.)
[17:36:21][Step 5/5] ERROR: Caused by: Login failed for user 'sonar'.
Solution: Tried a lot of things here, and finally ended up re-entering the pwd for my DB user in SQL Server. Now, I could have sworn that the pwd was correct – and since SQ was able to populate the DB schema using that exact same user (I could see all the SQ tables in the DB) – I still believe the user/pwd was correct. Anyway – if you see this issue, make sure the username/pwd in SQL Server matches the one entered in the TC setup screen:

No results!

Solution: Read the documentation :) and realize, that in order to run analysis on VS/C# projects, you’ll need two plugins for SQ:
These must be installed from the “Update Center” in SQ. If you (like me) cannot find the Update Center from the SQ dashboard – you need to login as the administrator (upper right-corner, see screenshot below). Again, reading the documentation helps surprisingly well – default username/pwd for the login is admin/admin.
sq find the update center

Passing parameters to SonarQube from TeamCity

In order to trigger SQ to use the VS plugin, you need to pass a few properties to the SonarQube Runner.

Solution: When using TeamCity, you have two options here:

1) Create the sonar-project.properties file and add it to your source control. Place the file next to the VS .sln file that you want to analyze. Use eg. the sample file provided here.

- OR -

2) Add -Dsonar.visualstudio.enable=true to the command line params in the TeamCity build step for SonarQube.

So far, my choice is option 2) since I prefer to have all configuration done from within TeamCity.

My current build step is configured like this:

tc sq build step

A lot of .cs files display with LOC == 0?

Also, the total LOC for the entire solution is way off.

Solution: Add -Dsonar.sourceEncoding=UTF-8 to the command line params in the TeamCity build step for SQ.

(Note that setting the encoding is also included in the sample .properties file mentioned above, so since I’m using TeamCity for the configuration I need to include this command line option as well).

Installing SonarQube as a Windows Service

The SonarQube web server can be started simply from the command line, but it can also be installed as a Windows service.

This is simply done by going to the SonarQube home dir on your server, and run /bin/windows-x86-64/InstallNTService.bat.

After that, start the service. I did see the same error as described in this article – and the fix (using a domain user) worked for me too.

Trigger a daily build for daily SonarQube analysis

I set up TeamCity to use a daily build at 23:00 which includes a build step for running the SonarQube analysis.

Setting up the daily trigger is easy, just remember to clear the Trigger build only if there are pending changes checkbox, in order to run analysis consistently every day:

tc daily sonarqube build

By now, you should have a daily SonarQube analysis run for your solution, using SQL Server and TeamCity. Mission accomplished.

Great, I have data. Now what?

So, it is up and running. Now I need to read up on the Developer’s Seven Deadly Sins and SQ’s take on Technical Debt/SQALE etc. in order to start using the data. But that’s a different story…

SonarQube success

Posted in Programming Tagged with: ,

Leave a Reply

About Thomas

My name is Thomas Hjorth Biilmann. I'm a pragmatic, humorous, floorball-playing computer programmer living in Denmark, along with 5,5 million other shiny happy people.
I work for DanPilot - Denmark's largest maritime pilotage service provider - where I design and implement systems that are at the very core of the business.
I am also the father of three little princesses - because that is what real Kings produce.
Subscribe via RSS
Use: http://biilmann.dk/?feed=rss2