Setup SonarQube in OpenShift for scanning projects via Jenkins

Setup Minishift

First things first, you need Minishift on your machine. I use a Mac for all my development so I used brew to install the latest Minishift executable. You can see other ways to setup Minishift here. I also use VirtualBox for my virtual machine driver for Minishift. I have a script that runs this for doing all my local Minishift development work that adds the vm-driver for VirtualBox, memory of 10GB, and disk-size of 40GB. I also run with the profile flag and save off the VM under a specific name so I can keep track of it. All personal preferences of course and you can run minishift -h to see the options. Figure out the options you wish to use and run minishift start with those options. It will pull down images, start the system, and give you information on how to log in. When that is up and running we are ready to setup SonarQube.

My Minishift Command

Optional: Enable the Admin-User AddOn

This is optional but I like doing this locally. I enable the admin-user addon for Minishift. This lets you log in with ‘god’ rights. You can run minishift addon enable admin-user and then run minishift addon apply admin-user to do this. Once enabled and then applied you can login with admin/admin and see all the inner workings of Minishift and get a glimpse of what OpenShift has under the covers. There are other addons you can see by running the command minishift addons list as well. (Che is the other one I am testing out.)

Setup the SonarQube Project

Now that we have Minishift setup, you can go to the URL that was listed when you started it in the terminal window and login with admin/admin. Click the Create Project button on the top right and enter sonarqube (all lower case) for the Name field and SonarQube for the Display Name field. You can enter a description if you like to ensure you know what it is later when you look at the project listings.

Add the SonarQube project on Minishift
SonarQube 7.4 Community Edition setup in Minishift

Let’s Dive In!

To get more familiar with what our YAML file actually did we need to explore a little. We setup 2 deployments inside our project, one for each application. One application is SonarQube itself and we are using the image from DockerHub for that. The other application is PostgreSQL and OpenShift comes with that image already. So we just used it. If you click the Applications menu on the left and then Deployments you can see the 2 deployments we have. Click further on the links to see what each one has setup. For each deployment OpenShift will list #1, #2, #3 for the number of the deployment you have running. We just did this so you should see the #1 hyperlinked. Click on it to get something like the below image.

SonarQube Deployment on Minishift

Log into SonarQube

Go back to the Overview section of your project and you will see the https://sonarqube-sonarqube.x.x.x listing. This is the Route exposing a URL to the internal SonarQube container running. We only have a route for SonarQube. We do not need an external route for PostgreSQL as only SonarQube has to talk to it. Click that link and log in with admin/admin to ensure you can get to SonarQube. If it starts a tutorial click Skip this tutorial. You should get into SonarQube and see a screen like below. Now get to work!

Use Jenkins in projects to scan code with SonarQube

Having SonarQube is great and all only if you use it. I have an example microservice API project in dotnet core 2.1 that you could put into a brand new project and then use its deployment file to deploy 3 pods: Jenkins, the database, and the API. Then log into Jenkins, approve the OpenShift login, and see how Jenkins works within OpenShift. For SonarQube to work you need to add the SonarQube plugin on the Manage Jenkins → Manage Plugins page and restart. Then go into the Manage Jenkins → Configure System and setup the SonarQube plugin with the name if the server (i.e. SonarQube-Server), the internal-to-Openshift URL (i.e. http://sonarqube.sonarqube.svc), and a generated token from your account page so you can push scan results from the Jenkins process. You also could fork this project linked above and play around to get more familiar. The best way to learn is to do!

Advanced: .NET Core Sonar Scanner Jenkins Slave

There is a .Net Core SonarQube Scanner Jenkins slave image on my other repo that you can import by using the YAML files and importing into the ‘openshift’ project in OpenShift. You can use this project here to create in OpenShift to test it out. The Jenkins slave setup in the Jenkins Configure System screen will need to match the name of the labels in the Jenkinsfile. If you have NO IDEA what I am talking about do not worry. I go into that in a third post that is related to this. For now get used to SonarQube and see what you can do! And look at the other blog post.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dale Bingham

Dale Bingham

CEO of Soteria Software. Developer on OpenRMF. Software Geek by trade. Father of three daughters. Husband. Love new tech where it fits. Follow at @soteriasoft