Unlocking Code Quality: Integrating Jenkins Pipeline with SonarQube and GitHub

Zuda Pradana Putra
6 min readDec 1, 2023

In the ever-evolving of software development, continuous integration and delivery (CI/CD) have become indispensable practices. At the heart of this automation lies Jenkins, a powerful open-source automation server. Jenkins simplifies the building, testing, and deployment of code, bringing efficiency and consistency to development workflows.

Understanding Jenkins: A Catalyst for Continuous Integration

Jenkins acts as the linchpin in the CI/CD process, orchestrating seamless integration of code changes. Its significance lies not only in automating mundane tasks but also in fostering collaboration among development and operations teams. By providing a framework for building, testing, and deploying code, Jenkins accelerates the software development lifecycle, enabling faster delivery of high-quality software.

The Benefits of Jenkins:

  • Automation Efficiency: Jenkins automates repetitive tasks, reducing the chance of human error and ensuring consistent builds and deployments.
  • Flexibility and Extensibility: Its extensive plugin ecosystem allows integration with a variety of tools and technologies, making it adaptable to diverse development environments.
  • Rapid Feedback: Jenkins provides real-time feedback on code changes, facilitating early detection of issues and enabling quick resolution.

While Jenkins optimizes the integration and delivery phases, maintaining code quality is equally crucial. This brings us to SonarQube.

Elevating Code Quality with SonarQube

Why SonarQube? Code quality is more than just syntactic correctness; it extends to best practices, maintainability, and adherence to coding standards. This is where SonarQube steps in as a leading platform for continuous inspection of code quality.

Key Advantages of SonarQube:

  • Holistic Code Analysis: SonarQube provides a comprehensive analysis of code quality, covering areas such as code duplication, unit test coverage, and adherence to coding standards.
  • Early Issue Detection: By integrating into the development process, SonarQube identifies issues early in the development cycle, preventing the accumulation of technical debt.
  • Actionable Insights: Developers receive actionable feedback, allowing them to address code quality issues before they escalate, leading to more maintainable and sustainable codebases.

In the subsequent sections of this article, we’ll delve into the intricacies of integrating Jenkins pipelines with SonarQube and GitHub. By combining the power of Jenkins for CI/CD with SonarQube for code quality analysis, development teams can establish a robust and streamlined workflow, ensuring the delivery of high-quality software at speed.

Setting Up Jenkins for Seamless SonarQube Integration

Now that we understand the importance of maintaining code quality, let’s dive into the practical steps of integrating Jenkins with SonarQube. This integration not only automates code analysis but also provides actionable insights for developers to enhance the quality of their code. Follow these steps to seamlessly configure Jenkins with SonarQube:

Step 1: Install SonarQube Scanner Plugin

  1. Open your Jenkins dashboard and navigate to Manage Jenkins.
  2. Click on Manage Plugins.
  3. In the Available tab, search for “SonarQube Scanner.”
Installing Plugin Sonar Scanner for Jenkins

you can select available plugins and search for Sonar Scanner, you can also leave the restart after install checklist. you can check the plugins that have been installed in the installed plugins menu.

Step 2: Configure SonarQube Server in Jenkins

After successful installation, go to Manage Jenkins and select Configure System. Make sure you check the box for Environment variables to ensure Jenkins can access SonarQube variables.

Setup SonarQuber Servers Jenkins
  1. Fill in the field name, this will be used for the jenkins setup file later.
  2. In the Server URL field, enter the URL of your SonarQube server.

Step 3: Manage Credentials

  1. If you haven’t set up credentials yet, go to Manage Jenkins and select Manage Credentials.
  2. Create a new domain, and within that domain, add new credentials for SonarQube (Optional). If you won’t to create domain, just click global credential then fill the credential.
Create Global Credential Jenkins

Change the type option to secret text, this you can get from the sonar user token

Step 4: Generate Token Sonar and Create SonarQube Project

  1. Access your SonarQube dashboard and go to Administration.
  2. Navigate to Security and select Users.
  3. Choose your user and click on it. You will find an option to generate a token.
Generate Token Sonar

Generate a token and save it securely. Go back to Jenkins, add a new credential (Secret text) in the SonarQube server configuration. Paste the generated token in the Secret field. With these configurations, Jenkins is now seamlessly connected to your SonarQube server. The environment variables and credentials are set up, ensuring secure and efficient communication between the two platforms.

After generate toke you can create a new project sonarqube, this key and project name will be used to initiate the project when verifying in the Jenkins script.

Create Project Sonar

Step 5: Manage Tools Jenkins

Before entering the stage of creating a pipeline, it is important to setup and organize the installation. you can enter manage and then go to tools. there you can install JDK can be automatic or search for jdk files on your local computer, then automatically install maven, sonarqube scanner each choose the latest version.

Step 7: Creating a Jenkins Pipeline for SonarQube Integration

Now that we’ve configured Jenkins and SonarQube, let’s proceed with setting up a new Jenkins pipeline. This pipeline will automate the integration of SonarQube into your code analysis process. Follow these steps to create an efficient pipeline:

  1. Create a New Jenkins Pipeline In your Jenkins dashboard, click on New Item. Give your project a name (e.g., “CodeAnalysisPipeline”) and select Pipeline. Click OK to create the new pipeline.
  2. Step 2: Configure Pipeline from Source Code Management (SCM)
    In the pipeline configuration, scroll down to the Pipeline section. In the Definition dropdown, select Pipeline script from SCM. Choose your preferred SCM (Source Code Management), which in this case is Git.
  3. Enter the URL of your GitHub repository.
  4. Specify the branch you want to build by entering it in the Branch Specifier field.
Build Pipeline and Connect to Git

In your script file, enter the name of your Jenkins file in the git directory.

pipeline {
agent any
tools {
maven 'Maven'
}

stages {
stage('Git Checkout') {
steps {
checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[url: 'https://github.com/ZudaPradana/sonar']])
echo 'Git Checkout Completed'
}
}

stage('SonarQube Analysis') {
steps {
withSonarQubeEnv('ServerNameSonar') {
bat '''mvn clean verify sonar:sonar -Dsonar.projectKey=ProjectNameSonar -Dsonar.projectName='ProjectNameSonar' -Dsonar.host.url=http://localhost:9000''' //port 9000 is default for sonar
echo 'SonarQube Analysis Completed'
}
}
}
}
}

mvn clean verify: This is a Maven command that does two things:

  • clean: It cleans up all the files created by the previous build.
  • verify: It runs all the build phases up to the verify phase, which includes compiling, testing, and packing your code.

Dsonar.projectKey=ProjectNameSonar: Ini menetapkan kunci proyek SonarQube. Kunci proyek adalah pengenal unik untuk proyek Anda dalam SonarQube.

Dsonar.projectName=’ProjectNameSonar’: Ini menetapkan nama proyek SonarQube.

Dsonar.host.url=http://localhost:9000: Ini menetapkan URL server SonarQube. Dalam hal ini, server berjalan pada localhost di port 9000.

Last Step: Let’s Build!

Save your pipeline configuration. Click on Build Now to run your pipeline. Now, Jenkins will automatically trigger the pipeline, which includes building your code and running SonarQube analysis. The results will be visible in your SonarQube dashboard, providing insights into the code quality of your project.

Console Output Success Build Sonar
Overview Quality Code with Sonar

After running your Jenkins pipeline and SonarQube analysis, you’ve observed a code coverage of 74%, falling below the configured quality gate threshold of 80%. Understanding that code coverage serves as a critical metric encompassing the entirety of your codebase, it’s imperative to refine this threshold to ensure comprehensive testing.

Congratulations! You’ve successfully set up a Jenkins pipeline that integrates with SonarQube to enhance your code quality. Adjustments can be made based on your project’s specific needs and configurations. Happy coding! Cheerssss!

--

--