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.
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
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'.
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:
– OR –
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:
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.
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:
By now, you should have a daily SonarQube analysis run for your solution, using SQL Server and TeamCity. Mission accomplished.