Integrate SonarQube into Pipelines

SonarQube is a popular continuous inspection tool for code quality. You can use it for static and dynamic analysis of a codebase. After it is integrated into pipelines in KubeSphere Container Platform, you can view common code issues such as bugs and vulnerabilities directly on the dashboard as SonarQube detects issues in a running pipeline.

This tutorial demonstrates how you can integrate SonarQube into pipelines. Refer to the following steps first before you create a pipeline using a Jenkinsfile.

Prerequisites

You need to enable the KubeSphere DevOps System.

Install the SonarQube Server

To integrate SonarQube into your pipeline, you must install SonarQube Server first.

  1. Install Helm first so that you can install SonarQube using the tool. For example, run the following command to install Helm 3:

    1. curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash

    View the Helm version.

    1. helm version
    2. version.BuildInfo{Version:"v3.4.1", GitCommit:"c4e74854886b2efe3321e185578e6db9be0a6e29", GitTreeState:"clean", GoVersion:"go1.14.11"}

    Note

    For more information, see the Helm documentation.

  2. Execute the following command to install SonarQube Server.

    1. helm upgrade --install sonarqube sonarqube --repo https://charts.kubesphere.io/main -n kubesphere-devops-system --create-namespace --set service.type=NodePort

    Note

    Make sure you use Helm 3 to install SonarQube Server.

  3. You will get this prompt:

    sonarqube-install

Get the SonarQube Console Address

  1. Execute the following command to get SonarQube NodePort.

    1. export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services sonarqube-sonarqube)
    2. export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonpath="{.items[0].status.addresses[0].address}")
    3. echo http://$NODE_IP:$NODE_PORT
  2. You can get the output as below (31434 is the port number in this example, which may be different from yours):

    1. http://192.168.0.4:31434

Configure the SonarQube Server

Step 1: Access the SonarQube console

  1. Execute the following command to view the status of SonarQube. Note that the SonarQube console is not accessible until SonarQube is up and running.

    1. $ kubectl get pod -n kubesphere-devops-system
    2. NAME READY STATUS RESTARTS AGE
    3. devops-jenkins-68b8949bb-7zwg4 1/1 Running 0 84m
    4. s2ioperator-0 1/1 Running 1 84m
    5. sonarqube-postgresql-0 1/1 Running 0 5m31s
    6. sonarqube-sonarqube-bb595d88b-97594 1/1 Running 2 5m31s
  2. Access the SonarQube console http://<Node IP>:<NodePort> in your browser.

  3. Click Log in in the upper-right corner and log in as the default account admin/admin.

    Note

    You may need to set up necessary port forwarding rules and open the port to access SonarQube in your security groups depending on where your instances are deployed.

Step 2: Create a SonarQube admin token

  1. Click the letter A and select My Account from the menu to go to the Profile page.

    sonarqube-config-1

  2. Click Security and enter a token name, such as kubesphere.

    sonarqube-config-2

  3. Click Generate and copy the token.

    sonarqube-config-3

    Warning

    Make sure you do copy the token because you won’t be able to see it again as shown in the prompt.

Step 3: Create a webhook server

  1. Execute the following command to get the address of SonarQube Webhook.

    1. export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services devops-jenkins)
    2. export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonpath="{.items[0].status.addresses[0].address}")
    3. echo http://$NODE_IP:$NODE_PORT/sonarqube-webhook/
  2. Expected output:

    1. http://192.168.0.4:30180/sonarqube-webhook/
  3. Click Administration, Configuration and Webhooks in turn to create a webhook.

    sonarqube-webhook-1

  4. Click Create.

    sonarqube-webhook-3

  5. Enter Name and Jenkins Console URL (for example, the SonarQube Webhook address) in the displayed dialog box. Click Create to finish.

    webhook-page-info

Step 4: Add the SonarQube configuration to ks-installer

  1. Execute the following command to edit ks-installer.

    1. kubectl edit cc -n kubesphere-system ks-installer
  2. Navigate to devops. Add the field sonarqube and specify externalSonarUrl and externalSonarToken under it.

    1. devops:
    2. enabled: true
    3. jenkinsJavaOpts_MaxRAM: 2g
    4. jenkinsJavaOpts_Xms: 512m
    5. jenkinsJavaOpts_Xmx: 512m
    6. jenkinsMemoryLim: 2Gi
    7. jenkinsMemoryReq: 1500Mi
    8. jenkinsVolumeSize: 8Gi
    9. sonarqube: # Add this field manually.
    10. externalSonarUrl: http://192.168.0.4:31434 # The SonarQube IP address.
    11. externalSonarToken: f75dc3be11fd3d58debfd4e445e3de844683ad93 # The SonarQube admin token created above.
  3. Save the file after you finish.

Step 5: Add the SonarQube server to Jenkins

  1. Execute the following command to get the address of Jenkins.

    1. export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services devops-jenkins)
    2. export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonpath="{.items[0].status.addresses[0].address}")
    3. echo http://$NODE_IP:$NODE_PORT
  2. You can get the output as below, which tells you the port number of Jenkins.

    1. http://192.168.0.4:30180
  3. Access Jenkins with the address http://<Node IP>:30180. When KubeSphere is installed, the Jenkins dashboard is also installed by default. Besides, Jenkins is configured with KubeSphere LDAP, which means you can log in to Jenkins with KubeSphere accounts (for example, admin/[[email protected]](https://kubesphere.io/cdn-cgi/l/email-protection)) directly. For more information about configuring Jenkins, see Jenkins System Settings.

    Note

    You may need to set up necessary port forwarding rules and open port 30180 to access Jenkins in your security groups depending on where your instances are deployed.

  4. Click Manage Jenkins on the left navigation pane.

  5. Scroll down to Configure System and click it.

  6. Navigate to SonarQube servers and click Add SonarQube.

  7. Enter Name and Server URL (http://<Node IP>:<NodePort>). Click Add, select Jenkins, and then create the credentials with the SonarQube admin token in the displayed dialog box as shown in the second image below. After adding the credentials, select it from the drop-down list for Server authentication token and then click Apply to finish.

    sonarqube-jenkins-settings

    add-credentials

    Note

    If the Add button is not working, which is a known bug from Jenkins, you can navigate to Manage Credentials under Manage Jenkins, click Jenkins under Stores scoped to Jenkins, click Global credentials (unrestricted), and then click Add Credentials from the left navigation bar to add the credentials with the SonarQube admin token by referencing the second image above. After you add the credentials, you can select it from the drop-down list for Server authentication token.

Step 6: Add sonarqubeURL to the KubeSphere Console

You need to specify sonarqubeURL so that you can access SonarQube directly from the KubeSphere console.

  1. Execute the following command:

    1. kubectl edit cm -n kubesphere-system ks-console-config
  2. Go to data.client.enableKubeConfig and add the field devops with sonarqubeURL specified under it.

    1. client:
    2. enableKubeConfig: true
    3. devops: # Add this field manually.
    4. sonarqubeURL: http://192.168.0.4:31434 # The SonarQube IP address.
  3. Save the file.

Step 7: Restart Services

Execute the following commands.

  1. kubectl -n kubesphere-devops-system rollout restart deploy devops-apiserver
  1. kubectl -n kubesphere-system rollout restart deploy ks-console

Create a SonarQube Token for a New Project

You need a SonarQube token so that your pipeline can communicate with SonarQube as it runs.

  1. On the SonarQube console, click Create new project.

    sonarqube-create-project

  2. Enter a project key, such as java-demo, and click Set Up.

    jenkins-projet-key

  3. Enter a project name, such as java-sample, and click Generate.

    generate-a-token

  4. After the token is created, click Continue.

    token-created

  5. Choose Java and Maven respectively. Copy the serial number within the green box in the image below, which needs to be added in the Credentials section if it is to be used in pipelines.

    sonarqube-example

View Results on the KubeSphere Console

After you create a pipeline using the graphical editing panel or create a pipeline using a Jenkinsfile, you can view the result of code quality analysis.