Docker/jenkins/SonarQube

When you get the proposed install versions for the software you may encounter that the LATEST is greater than the one shown here, this is because software changes to add new features or bug fixes.

It is recommended to use the LATEST and greatest

Here you find the step by step process for creating a local environment using CICD practices.

Create Spring Boot App

Here the process is simple as we assume you know how to create a basic Spring Boot using the best starter link ever Start Spring IO.

Spring Boot Initializr
Figure 1. Spring Boot Initializr

Generate the project and extract downloaded zip file. This way we have the initial project configuration.

Eventually this code would be pushed to a Git repository. Use your favorite IDE to edit files and continue with the tutorial.

A copy of this code is in the Tutorial CICD Jenkins Sonar git repository.

Configure Jenkins

Here we are going to document only the necessary steps required for our setup to properly work.

The assumption here is that you already configured Jenkins using the Docker image or the standalone WAR file (remember that Jenkins requires JDK 8).

In this document we are going to use the Dockerized version of Jenkins. Please refer to the Docker INSERT LINK REFERENCE HERE section for details. We need to know what it is installed in the Docker image, to do so, type the following command to access the command line interface for such image.

$ docker ps (1)
$ docker exec -it <container_name>|<container_id> /bin/bash (2)
1 To gather the container_name or container_id for the _Docker image.
2 To access the container command line interface.

As we built our sample application using Java 8, Spring Boot and Maven, we need to guarantee them inthe Docker image.

$ java -version (1)
$ git --version (2)
$ mvn --version (3)
1 Java verification. If it works we know it has JDK installed (and it must as it is a requirement for Jenkins). We need JDK 8 and in fact it uses OpenJDK 1.8.0_181, which it is already installed in Docker image.
2 Git verification. If it works then we can use it for the sake of pulling source code in the Jenkins pipeline, otherwise it needs to be installed NOTE: It is present in the Docker image.
3 Maven verification. If it works then we can use it for the sake of building our source code, otherwise it needs to be installed. NOTE: This is not present in Docker image, so we need to install it.

As the last verification fails for Maven we must proceed to guarantee it is present in the Docker image. To do so, there are two ways, one is to let Jenkins set it up, or in the Docker image command line interface install it by hand. In this guide we will follow the Jenkins, which we describe next.

Build Pipeline

In order to run the Jenkins pipeline for this guide, there are some other plugins to be installed. Select the Manage Jenkins Plugins as shown below.

Jenkins Manage Plugins
Figure 2. Jenkins Manage Plugins

Next select the Available plugins and use the filter (search) option to search for these plugins.

Jenkins Plugin JaCoCo
Figure 3. Jenkins Plugin JaCoCo
Jenkins Plugin SonarQube Scanner
Figure 4. Jenkins Plugin SonarQube Scanner
Jenkins Plugin Build Pipeline
Figure 5. Jenkins Plugin Build Pipeline
Use the latest version for plugin at the moment besides the names may also differ.

Once they are selected, use the Install without restart button.

Jenkins Installing Plugins
Figure 6. Jenkins Installing Plugins
It is necessary to restart in order to use these plugins, do it now.

Configure Maven in Jenkins

Open your Jenkins home page.

Jenkins Home Login
Figure 7. Jenkins Home Login

Introduce your valid credentials and you will be presented with the main page.

Jenkins Home Page
Figure 8. Jenkins Home Page

Locate the option Manage Jenkins and next the option Global Tool Configuration.

Jenkins Manage Jenkins Global Tools
Figure 9. Jenkins Manage Jenkins Global Tools

Next locate the Maven section.

Jenkins Maven Setup
Figure 10. Jenkins Maven Setup

To achieve what is shown in the picture above you must click the Add Maven button and set as the image depicts.

Select the version that is more convenient according to your project.

Next click the Save to finish and this way you have maven setup.

Configure SonarQube Scanner

After all required plugins are installed in the previous section, we need to install the SonarQube Scanner.

Open your Jenkins home page.

Jenkins Home Login
Figure 11. Jenkins Home Login

Introduce your valid credentials and you will be presented with the main page.

Jenkins Home Page
Figure 12. Jenkins Home Page

Locate the option Manage Jenkins and next the option Global Tool Configuration.

Jenkins Manage Jenkins Global Tools
Figure 13. Jenkins Manage Jenkins Global Tools

Next locate the SonarQube Scanner section.

Jenkins Install SonarQube Scanner
Figure 14. Jenkins Install SonarQube Scanner

To achieve what is shown in the picture above you must click the Add SonarQube Scanner button and set as the image depicts.

Select the version that is more convenient according to your project.

Next click the Save to finish and this way you have maven setup.

Configure SonarQube

-----ZZZZZ TODO

Build a Pipeline

A pipeline in Jenkins is the recipe of tasks it needs to execute, sequentially or in parallel. In this section we are going to define our sample pipeline for demostration purposes.

Login to Jenkins and click the Create new Jobs or New Item.

Jenkins Create new Jobs
Figure 15. Jenkins Create new Jobs

The next step is to declare the type of project we are going to build. Here we create a folder style as a container for steps in the pipeline.

Jenkins Job Free Folder Style
Figure 16. Jenkins Job Free Folder Style

In the name of the project write JavaDemo. When you go back to main page, you will be presented with the following screen.

Jenkins Folder Setup View
Figure 17. Jenkins Folder Setup View

Notice the Sun in the second column, this will show you the status of the project, when in the Sun icon, it means that the project is healthy, and it is based in the last five executions of the job.

The next step is to configure the steps for the pipeline. Click in th Java Jenkins/Sonar Demo and it will show the following screen.

Jenkins Sonar Home
Figure 18. Jenkins Sonar Home

Next you must create a new item to be associated in the container. For this purpose locate the New Item in the menu. And in the choices for new items select the Freestyle project, and select Ok.

Jenkins Checkout Code
Figure 19. Jenkins Checkout Code

Next it is time to configure how to access the source code, which will be made by Git. First define what is the URL where the code will be pulled from.

Click in the Source Code Managemente tab link.

.Jenkins Checkout Code URL
Figure 20. Jenkins Checkout Code URL

Next click in the *Build Triggers*, this will schedule execution every 15 minutes.

Jenkins Checkout Code Build Triggers
Figure 21. Jenkins Checkout Code Build Triggers

At this point click in Apply and then Save buttons which lead you to here.

Jenkins Checkout Code Project View
Figure 22. Jenkins Checkout Code Project View

Next click in the + button to define more steps. Here we are going to add the Pipeline View.

026 pipeline view
Figure 23. Jenkins Pipeline View

And in the settings form put the following values (only one shown).

Jenkins Pipeline View Config
Figure 24. Jenkins Pipeline View Config

This way in the project home a new tab appears to show the Pipeline, as shown next.

Jenkins Link to Pipeline
Figure 25. Jenkins Link to Pipeline

Next we create the next step in the pipeline. Select the New Item when you are in this following screen.

Jenkins Compile Step
Figure 26. Jenkins Compile Step

Next we define the type of project.

Jenkins Compile Step Type Definition
Figure 27. Jenkins Compile Step Type Definition

Click Ok to define the settings for task and then click on tab Build Triggers and configure as shown next.

Jenkins Compile Build Triggers
Figure 28. Jenkins Compile Build Triggers

Next click the tab Build, click in the Add build step which will present a list of choices and the one to select is Invoke top-level Maven targets which will lead to the following settings.

Jenkins Compile Build Settings
Figure 29. Jenkins Compile Build Settings

And select the Advanced button and in the POM field set the path for the source code which is located under the Jenkins installation Project Workspace.

And this conclude this job. Based in these instructions you can create the next job which is to run the Java App Tests. Here name task as test and in the similar way for job package.