Continuous Code Analysis with SonarQube and Codefresh
Tags: devops, sonarqube, testing, cicd
Published: 2020-11-25 10:09:20

<a href="https://www.sonarqube.org/">SonarQube</a> is a universal tool for code analysis that provides continuous inspection of your code to highlight existing and newly introduced issues. This allows you to “Clean as You Code”, which aims to reach the maximum code quality in your newly written code.

This post will:
<ul>
 	<li>Provide an overview of SonarQube and how you can use it locally</li>
 	<li>Introduce you to Codefresh Pipelines</li>
 	<li>And lastly, we will show you how to integrate SonarQube directly in Codefresh with our SonarQube plugin</li>
</ul>
<h1><b>An Overview of SonarQube</b></h1>
The SonarQube architecture consists of a server and a SonarScanner. The Scanner is a separate client type application that is used to analyze projects in connection with SonarQube. The Scanner is usually included in your CI/CD pipeline.

In case you want to use SonarQube locally, there are <a href="https://docs.sonarqube.org/latest/setup/get-started-2-minutes/">two different ways to install it</a>; either through the zip file or using the official Docker image that you can find on the <a href="https://hub.docker.com/_/sonarqube/">Docker Hub</a>. The local version can be used to scan your code during development. Alternatively, using the <a href="https://sonarcloud.io/">SonarCloud</a>, once the code is pushed to your remote git repository, you can access their online UI. The SonarCloud can be used for free for testing purposes.

To get started installing SonarQube locally, you need to have <a href="https://docs.docker.com/engine/install/">Docker installed</a>. This will allow you to pull the official Docker image of SonarQube:
<pre>docker pull sonarqube
</pre>
We can then run the image to access the UI locally:
<pre>docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:latest
</pre>
Now you should be able to access SonarQube on localhost:9000. Log in to the service with the System Administrator credentials (login=admin, password=admin).

<a href="https://codefresh.io/wp-content/uploads/2020/11/sonarqube-cloud.png"><img class="size-medium wp-image-18130" src="https://codefresh.io/wp-content/uploads/2020/11/sonarqube-cloud-300x200.png" alt="sonar-cloud" width="300" height="200" /></a> 

For more advanced use of SonarQube, it is recommended that you install the <a href="https://docs.sonarqube.org/latest/setup/install-server/">server</a>. In this case, you will use:
<ol>
 	<li>The SonarQube server running the following processes:
<ul>
 	<li>a web server that serves the SonarQube user interface.</li>
 	<li>a search server based on Elasticsearch.</li>
 	<li>the compute engine in charge of processing code analysis reports and saving them in the SonarQube database.</li>
</ul>
</li>
 	<li>The database to store the following:
<ul>
 	<li>Metrics and issues for code quality and security generated during code scans.</li>
 	<li>The SonarQube instance configuration.</li>
</ul>
</li>
</ol>
The process that SonarQube follows when analyzing your code is highly dependent on the programming language that your application is written in. While SonarQube has been used predominantly to analyze Java files, it can analyze 27 different languages. SonarQube can report on bugs, vulnerabilities, code smells, coverage, or duplication. Additionally, it also calculates more <a href="https://docs.sonarqube.org/latest/user-guide/metric-definitions/">advanced metrics</a>, such as the complexity of your code. For each category, the UI will show you the number of issues detected within the category as well as the percentage.

Let’s go ahead and scan the code in the <a href="https://github.com/anais-codefresh/sonarqube-example">following project</a>. Note that you can also follow along using your own project, as long as it is written in <a href="https://www.sonarqube.org/features/multi-languages/">a language that is supported by SonarQube</a>.
<ol>
 	<li>Click the <b>Create new project</b> button.</li>
 	<li>When asked <b>How do you want to create your project</b>, select <b>Manually</b>.</li>
 	<li>Give your project a <b>Project key</b> and a <b>Display name</b> and click the <b>Set Up</b> button.</li>
 	<li>Under <b>Provide a token</b>, select <b>Generate a token</b>. Give your token a name, click the <b>Generate</b> button, and click <b>Continue</b>.</li>
 	<li>Select your project's main language under <b>Run analysis on your project</b>, and follow the instructions to analyze your project. Here you'll download and execute a Scanner on your code (if you're using Maven or Gradle, the Scanner is automatically downloaded).</li>
</ol>
Now we have to do the following to add the SonarScanner to our PATH and scan our project:
<ol>
 	<li>Extract the zip file within your working directory</li>
 	<li>Access the following file <span class="ILfuVd"><span class="hgKElc">`</span></span>working-directory/sonar-scanner-cli-4.5.0.2216-linux/sonar-scanner-4.5.0.2216-linux/conf<span class="ILfuVd"><span class="hgKElc">`</span></span> and edit it as such that the following line is uncommented:
<pre>#----- Default SonarQube server
sonar.host.url=http://localhost:9000
</pre>
</li>
 	<li>Save and close the file</li>
 	<li>Sonar Scanner /bin directory to your path
<pre>export PATH="sonar-scanner-4.5.0.2216-linux/bin:$PATH"
</pre>
In our case, the folder sonar-scanner-4.5.0.2216-linux/bin is inside the directory that we run the command in. Note that we are on Ubuntu 20.04; if you are on another operating system, this command will vary.</li>
 	<li>Verify that the path is added correctly by running: sonar-scanner -h</li>
 	<li>Now we can scan our project with the command provided in the UI from step 5:</li>
</ol>
<pre>sonar-scanner \                                        
 -Dsonar.projectKey=go-test-app \
 -Dsonar.sources=./simple-go \
 -Dsonar.host.url=http://localhost:9000 \
 -Dsonar.login=the token you generated in step 4
</pre>
Once the command executed successfully, go ahead and refresh the UI. You should now be able to see the following output:

<a href="https://codefresh.io/wp-content/uploads/2020/11/sonarqube-test-result.png"><img class="size-medium wp-image-18131" src="https://codefresh.io/wp-content/uploads/2020/11/sonarqube-test-result-300x181.png" alt="sonarqube-test-result" width="300" height="181" /></a>

This might not have been the most straight forward process to get up and running. Next, we will show you an easier option using the custom SonarQube plugin in Codefresh.
<h1><b>Using SonarQube in Codefresh</b></h1>
To use SonarQube in Codefresh does not require custom installation. Instead, you can use the Codefresh SonarQube plugin to integrate SonarQube directly into your pipeline. The Codefresh pipeline is composed of a series of steps. In this case, SonarQube will become one of the steps. The official <a href="https://codefresh.io/steps/step/sonar-scanner-cli">SonarQube step</a> can be found in the <a href="https://codefresh.io/docs/docs/codefresh-yaml/steps/">Codefresh plugin directory</a>, along with several more.

For our Codefresh step, we need again an access token from the SonarQube application. In this case, we are going to use <a href="https://sonarcloud.io">SonarCloud</a>.

<a href="https://docs.sonarqube.org/latest/user-guide/user-token/">To generate a token</a>:
<ul>
 	<li>Go to your <b>User &gt; My Account </b>(top right corner)</li>
 	<li>Select the <b>Security</b> tap</li>
 	<li>Generate your token -- we named ours <span class="ILfuVd"><span class="hgKElc">`</span></span>codefresh-token<span class="ILfuVd"><span class="hgKElc">`</span></span></li>
 	<li>Save the token somewhere where you will be able to access it again easily.</li>
</ul>
<a href="https://codefresh.io/wp-content/uploads/2020/11/sonarqube-token-generator.png"><img class="size-medium wp-image-18132" src="https://codefresh.io/wp-content/uploads/2020/11/sonarqube-token-generator-300x212.png" alt="sonarqube-token-generator" width="300" height="212" /></a>

Next, we need to add a <a href="https://github.com/anais-codefresh/sonarqube-example/blob/master/sonar-project.properties">sonar-project.properties</a> file to our project. Not all environment variables are currently<a href="https://github.com/SonarSource/sonar-scanner-cli-docker/pull/50"> automatically defined</a> in the SonarScanner. Thus, we have to set-up a sonar-project.properties file in our root directory:
<pre>touch sonar-project.properties
</pre>
This file is used to define the sonar.organization and the sonar.projectKey
<pre># must be unique in a given SonarQube instance

sonar.projectKey=sonarqube-example

# organization name

sonar.organization=anais-codefresh
</pre>
Please replace the values with your project-specific values. You can find the organization name in your SonarCloud Account under <b>My Organizations</b>.

Now go ahead and <a href="https://codefresh.io/codefresh-signup/?utm_source=Blog&amp;utm_medium=Post&amp;utm_campaign=sonarqube">create a Codefresh account</a> (if you don’t have one yet, now is the time -- it’s free!!).

Create a new project and within the project, we want to create a new pipeline. Upon creating the pipeline, make sure that you select the right project from your git repository. We are going to use <a href="https://github.com/anais-codefresh/sonarqube-example">the same app</a> as before; which contains a basic go application and a Dockerfile. If you are using your own application, please make sure that it contains a Dockerfile.

<a href="https://codefresh.io/wp-content/uploads/2020/11/codefresh-create-pipeline.png"><img class="size-medium wp-image-18133" src="https://codefresh.io/wp-content/uploads/2020/11/codefresh-create-pipeline-253x300.png" alt="codefresh-create-pipeline" width="253" height="300" /></a>

Once you hit “CREATE”, you will be redirected to the workflow of your pipeline. This includes an automatically generated codefresh.yml file, which defines your pipeline. We now want to add the SonarQube step.

Looking at the <a href="https://codefresh.io/steps/step/sonar-scanner-cli">custom plug-in</a>, we have the following options:
<pre>Sonarqube:
  type: sonar-scanner-cli
  arguments:
    SONAR_HOST_URL: 'http://foo.acme:9000'
    SONAR_LOGIN: username or key
    SONAR_PASSWORD: password if username is used
    SONAR_PROJECT_BASE_DIR: set working directory for analysis
    SONAR_SCANNER_CLI_VERSION: latest</pre>
We will modify our Codefresh pipeline to include the custom step:
<pre class=""> sonarqube:
    type: "sonar-scanner-cli"
    stage: "push"
    arguments:
      SONAR_HOST_URL: 'https://sonarcloud.io/' # replace with your host url
      SONAR_LOGIN: "insert access token" # replace with your access token
      SONAR_PROJECT_BASE_DIR: "/codefresh/volume/sonarqube-example" # replace with your working directory
      SONAR_SCANNER_CLI_VERSION: "latest"
</pre>
For reference, have a look at the <a href="https://github.com/anais-codefresh/sonarqube-example/blob/master/codefresh.yml">codefresh.yml</a> in the repository.

Once added, save the pipeline and run the pipeline. You should then see the following output:

<a href="https://codefresh.io/wp-content/uploads/2020/11/sonarqube-pipeline.png"><img class="size-medium wp-image-18134" src="https://codefresh.io/wp-content/uploads/2020/11/sonarqube-pipeline-300x197.png" alt="sonarqube-pipeline" width="300" height="197" /></a>

You can then view the analysis of your project in the SonarCloud:

<a href="https://codefresh.io/wp-content/uploads/2020/11/sonarcloud-analysis.png"><img class="size-medium wp-image-18135" src="https://codefresh.io/wp-content/uploads/2020/11/sonarcloud-analysis-300x191.png" alt="sonarcloud-analysis" width="300" height="191" /></a>

Note that you can access different visualization formats of the report in the taps. Additional information is provided in the other sections on the platform. For more information please refer to the <a href="https://docs.sonarqube.org/latest/analysis/overview/">SonarQube official documentation</a>.
<h1><b>Summary</b></h1>
Congratulations! Once you add the SonarQube step to your Codefresh pipeline, you will receive analysis reports of your code automatically with every pipeline build.

In this tutorial, we looked at:
<ul>
 	<li>What SonarQube is</li>
 	<li>How you can try out SonarQube locally</li>
 	<li>Making everything easy peasy by adding a step to your Codefresh pipeline and automating the process</li>
</ul>
Please let us know in the comments how you liked this tutorial. If you have any questions, please<a href="https://community.codefresh.io/"> join our community chat</a>, we would love to hear from you.

You do not have a Codefresh account yet? -- <a href="https://codefresh.io/codefresh-signup/?utm_source=Blog&amp;utm_medium=Post&amp;utm_campaign=sonarqube">Create Your Free Account today</a>!