πŸš€ Step-by-Step Guide to Community Branch Plugin in SonarQube

πŸš€ Step-by-Step Guide to Community Branch Plugin in SonarQube

Β·

3 min read

SonarQube is a powerful tool for code quality and security. With the Community Branch Plugin, you can extend its functionality to include branch analysis, even in the Community Edition. Let's walk through setting up this tool with Jenkins in a Dockerized environment and analyze code like a pro! 🌟


πŸ› οΈ Prerequisites

Before diving into the setup, ensure you have:

  • Docker installed 🐳

  • Jenkins set up πŸ’»

  • Basic knowledge of Maven and CI/CD workflows πŸ”„


🌟 What is Community Branch Analysis?

Branch analysis lets you review code quality across different branches, ensuring consistency and standards. The Community Branch Plugin brings this feature to SonarQube's Community Edition, giving you premium capabilities for free! πŸŽ‰


πŸš€ Step 1: Install Docker & Create a Container for SonarQube with the Plugin

  1. Install Docker:

     sudo apt update  
     sudo apt install docker.io
    
  2. Create a Docker Container with the Plugin:

     docker run -d -p 9000:9000 mc1arke/sonarqube-with-community-branch-plugin
    

πŸ”§ Step 2: Install Jenkins Plugins (SonarQube Scanner & Eclipse Temurin Installer)

  1. Log into your Jenkins instance.

  2. Go to Manage Jenkins > Plugin Manager.

  3. Search for:

    • SonarQube Scanner πŸ”

    • Eclipse Temurin Installer β˜•
      Install both plugins and restart Jenkins.


βš™οΈ Step 3: Configure Tools in Jenkins

  1. JDK Configuration:

    • Go to Manage Jenkins > Global Tool Configuration.

    • Under JDK, add a new JDK and select "Install Automatically."

  2. SonarQube Scanner Configuration:

    • Add SonarQube Scanner and configure its installation.

  3. Maven Configuration:

    • Add Maven and configure its installation.


πŸ”‘ Step 4: Create a Token in SonarQube

  1. Log in to SonarQube (localhost:9000).

  2. Navigate to My Account > Security > Tokens.

  3. Generate a new token and save it securely. You’ll need it in Jenkins.


🌐 Step 5: Configure SonarQube Server in Jenkins

  1. Go to Manage Jenkins > Configure System.

  2. Scroll to SonarQube Servers and click Add SonarQube.

  3. Provide:

    • Server Name.

    • SonarQube URL (e.g., localhost:9000).

    • Add the token under Server Authentication Tokens.


πŸ“œ Step 6: Write a Jenkins Pipeline for the Job

Create a Jenkins pipeline with the following script:



pipeline {
    agent any
    tools{
        maven 'maven3'
        jdk 'jdk17'
    }
    environment{
        SCANNER_HOME=tool 'sonar-scanner'
    }
    stages {
        stage('Git Checkout') {
            steps {
                git branch: 'dev', url: 'https://github.com/Ank911007/FullStack-Blogging-App-.git'
            }
        }
        stage('Compile') {
            steps {
                sh 'mvn compile'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('SonarQube Analysis') {
            steps {
                withSonarQubeEnv('sonar-server') {
                    sh '''$SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=bloggingApp -Dsonar.projectKey=bloggingApp \
                        -Dsonar.java.binaries=target -Dsonar.branch.name=dev '''
                    echo "$SCANNER_HOME"
                }
            }
        }
        stage('Quality Gate Check') {
            steps {
                timeout(time: 1, unit: 'HOURS') {
                        waitForQualityGate abortPipeline: false
                }
            }
        }
    }
}

πŸ—οΈ Step 7: Build the Job & Analyze the Report

  1. Run the pipeline in Jenkins.

  2. Once the build completes, go to SonarQube at localhost:9000.

  3. View detailed analysis under your project’s dashboard. πŸŽ‰


🎯 Conclusion

With these steps, you've successfully integrated the Community Branch Plugin into your CI/CD workflow. You now have the power to analyze code quality across branches with ease! πŸš€

Pro Tip: Keep iterating and optimizing your pipeline for faster feedback and improved code quality. Happy coding! πŸ’»βœ¨

Β