Back to All Posts

Complete Guide: Jenkins + SonarQube CI/CD Pipeline with Docker

Jenkins CI/CD

Setting up a complete CI/CD pipeline with Jenkins and SonarQube is essential for maintaining code quality and automating your build process. In this comprehensive guide, we'll walk through the entire setup using Docker Compose, from installation to automated GitHub webhooks.

Step 1: Docker Compose Setup

First, we'll set up Jenkins, SonarQube, and PostgreSQL using Docker Compose. This configuration ensures all services work together seamlessly.

docker-compose.yml
version: '3.8'

services:
  jenkins:
    image: jenkins/jenkins:lts-jdk17
    container_name: jenkins
    user: root
    ports:
      - "8080:8080"
      - "50000:50000"
    volumes:
      - jenkins_home:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock  
    environment:
      JAVA_OPTS: "-Djenkins.install.runSetupWizard=false"
    depends_on:
      - sonarqube

  sonarqube:
    image: sonarqube:community
    container_name: sonarqube
    ports:
      - "9000:9000"
    environment:
      SONAR_ES_BOOTSTRAP_CHECKS_DISABLE: "true"
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: sonar
    depends_on:
      - db

  db:
    image: postgres:15
    container_name: sonarqube_db
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
      POSTGRES_DB: sonar
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  jenkins_home:
  db_data:

Start the services with:

Terminal
docker-compose up -d

Access Jenkins at http://localhost:8080 and SonarQube at http://localhost:9000

Step 2: Install Jenkins Plugins

Navigate to Settings → Plugins in Jenkins and install the following essential plugins:

  • Maven Integration Plugin
  • Pipeline
  • Pipeline: Groovy
  • Pipeline: GitHub
  • SonarQube Plugin
  • SonarQube Scanner
Jenkins Plugins Installation

Jenkins plugins installation screen

Step 3: Configure Tools in Jenkins

Maven Configuration

Go to Settings → Tools and add Maven:

  • Name: Maven
  • Version: 3.9.2 (automatic installation)
Maven Configuration

Maven tool configuration

SonarQube Scanner Configuration

In the same Tools section, add SonarQube Scanner with automatic installation.

SonarQube Scanner Configuration

SonarQube Scanner setup

Step 4: Configure SonarQube Token

First, generate a token in SonarQube (My Account → Security → Generate Token). Then add it to Jenkins:

  1. Go to Settings → Credentials → Global
  2. Click Add Credentials
  3. Select Secret text as type
  4. Paste your SonarQube token
  5. ID: sonar
SonarQube Token Configuration

Adding SonarQube credentials

Step 5: Configure SonarQube Server

Navigate to Settings → System → SonarQube servers and add:

  • Name: SonarQube
  • Server URL: http://localhost:9000
  • Authentication token: Select the credential you created (sonar)
SonarQube Server Configuration

SonarQube server setup in Jenkins

Step 6: Configure GitHub Credentials

To allow Jenkins to clone your repository, add GitHub credentials:

  1. Go to Settings → Credentials → Global
  2. Click Add Credentials
  3. Type: Username with password
  4. Username: Your GitHub username
  5. Password: Personal Access Token (generate from GitHub Settings → Developer settings → Personal access tokens)
  6. ID: github-credentials

Step 7: Create Pipeline Job

Create New Pipeline

  1. Click New Item
  2. Enter a name for your pipeline
  3. Select Pipeline
  4. Click OK

Configure Pipeline from SCM

In the pipeline configuration:

  • Definition: Pipeline script from SCM
  • SCM: Git
  • Repository URL: Your GitHub repository URL
  • Credentials: Select your GitHub credentials
  • Branch: */main (or your branch name)
  • Script Path: Jenkinsfile
Pipeline Configuration

Pipeline SCM configuration

Step 8: Configure GitHub Webhook (Optional)

For automatic pipeline triggers on every push:

Expose Jenkins (if on localhost)

Use ngrok or Cloudflare Tunnel to expose Jenkins:

Using ngrok
ngrok http 8080

Add Webhook in GitHub

  1. Go to your GitHub repository
  2. Settings → Webhooks → Add webhook
  3. Payload URL: http://your-jenkins-url/github-webhook/
  4. Content type: application/json
  5. Events: Just the push event
  6. Click Add webhook
Manual Alternative: If you don't want webhooks, you can trigger the pipeline manually from Jenkins after each commit.

Step 9: Create Jenkinsfile

Create a Jenkinsfile in your repository root with the following pipeline definition:

Jenkinsfile
pipeline {
    agent any

    environment {
        SONAR_HOST_URL = 'http://localhost:9000'
        SONAR_TOKEN = credentials('sonar')
    }

    stages {

        stage('Stage 1: Build') {
            steps {
                echo 'Building the project...'
                sh "${tool 'Maven'}/bin/mvn clean compile"
            }
        }

        stage('Stage 2: SonarQube Analysis') {
            steps {
                script {
                    withSonarQubeEnv('SonarQube') {
                        sh """
                            ${tool 'Maven'}/bin/mvn sonar:sonar \
                            -Dsonar.projectKey=sonarQube \
                            -Dsonar.host.url=$SONAR_HOST_URL \
                            -Dsonar.login=$SONAR_TOKEN
                        """
                    }
                }
            }
        }
    }

    post {
        always {
            echo "Pipeline finished!"
        }
        success {
            echo "Pipeline executed successfully!"
        }
        failure {
            echo "Pipeline failed!"
        }
    }
}

Pipeline Stages Explained

  • Stage 1: Build - Compiles the project using Maven
  • Stage 2: SonarQube Analysis - Runs code quality analysis
  • Post actions - Executes after pipeline completion

Step 10: Run the Pipeline

Now you can trigger the pipeline:

  • Automatic: Push a commit to your repository
  • Manual: Click "Build Now" in Jenkins
Pipeline Execution

Pipeline execution in Jenkins

Step 11: View Results in SonarQube

After the pipeline completes, view the analysis results in SonarQube at http://localhost:9000

SonarQube Results

Code quality analysis results in SonarQube

Troubleshooting Common Issues

1. Maven not found

Make sure Maven is configured in Settings → Tools with the exact name "Maven".

2. SonarQube connection failed

Verify that:

  • SonarQube container is running
  • The URL in Jenkins matches your setup
  • The token is valid and correctly stored

3. GitHub webhook not triggering

Check:

  • Webhook URL is correct and accessible
  • Recent deliveries in GitHub webhook settings
  • Jenkins is properly exposed (if using ngrok/cloudflare)

Best Practices

  • ✓ Use environment variables for sensitive data
  • ✓ Set up quality gates in SonarQube
  • ✓ Configure proper branch protection in GitHub
  • ✓ Add email notifications on pipeline failure
  • ✓ Regular backup of Jenkins configuration
  • ✓ Use declarative pipeline syntax

Conclusion

You now have a fully functional CI/CD pipeline with Jenkins and SonarQube! This setup provides:

  • ✓ Automated builds on every commit
  • ✓ Continuous code quality analysis
  • ✓ Early detection of code issues
  • ✓ Improved team productivity
  • ✓ Consistent code standards

Next Steps

  • Add automated testing stages
  • Configure deployment stages
  • Set up quality gates
  • Integrate with Slack/Email notifications
  • Add Docker build and push stages