Download and extract SonarQube 9.9.4 LTS Community Edition:
cd /opt
sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.9.4.87374.zip
sudo unzip sonarqube-9.9.4.87374.zip
Create a dedicated system user and assign ownership:
sudo useradd --system --home-dir /opt/sonarqube-9.9.4.87374 --shell /sbin/nologin --comment "SonarQube service account" --user-group sonarqube
sudo chown -R sonarqube:sonarqube /opt/sonarqube-9.9.4.87374
Define a systemd service unit at /etc/systemd/system/sonarqube.service:
[Unit]
Description=SonarQube service
After=network.target
[Service]
Type=forking
User=sonarqube
Group=sonarqube
WorkingDirectory=/opt/sonarqube-9.9.4.87374
ExecStart=/opt/sonarqube-9.9.4.87374/bin/linux-x86-64/sonar.sh start
ExecReload=/opt/sonarqube-9.9.4.87374/bin/linux-x86-64/sonar.sh restart
ExecStop=/opt/sonarqube-9.9.4.87374/bin/linux-x86-64/sonar.sh stop
TimeoutSec=300
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable sonarqube
sudo systemctl start sonarqube
Verify the service status:
systemctl status sonarqube
Once SonarQube is running, access http://localhost:9000, log in with admin/admin, and immediateyl change the default password. Generate a new token under User > My Account > Security, then copy it for use in Jenkins.
In Jenkins, install the SonarQube Scanner plugin. Create a new secret text credential (e.g., ID: sonarqube) using the generated token.
Configure the pipeline to perform static analysis using Maven:
pipeline {
agent any
tools {
jdk 'bellsoft-jdk17.0.10'
maven '3.6.3'
}
stages {
stage('Check tool version') {
steps {
sh '''
env | grep -E "PATH|JAVA_HOME"
java -version
mvn --version
'''
}
}
stage('Build') {
steps {
sh 'mvn clean package -DskipTests'
}
}
stage('Static Code Analysis') {
environment {
SONAR_HOST_URL = "http://127.0.0.1:9000"
}
steps {
withCredentials([string(credentialsId: 'sonarqube', variable: 'SONAR_TOKEN')]) {
sh 'mvn sonar:sonar -Dsonar.token=$SONAR_TOKEN -Dsonar.host.url=$SONAR_HOST_URL'
}
}
}
}
}