🧩 What is SonarQube?
SonarQube is an open-source platform developed by SonarSource that is used to inspect and continuously analyze the quality of source code. It helps developers identify bugs, code smells, vulnerabilities, and security issues in their code across multiple programming languages.
It integrates with CI/CD pipelines (like Jenkins, GitLab CI, Azure DevOps, etc.) to automatically analyze code every time a change is made.
⚙️ Main Features
-
Static Code Analysis: Detects bugs, code smells, and security vulnerabilities.
-
Multi-Language Support: Supports 25+ languages including Java, C#, Python, JavaScript, Go, PHP, etc.
-
Quality Gates: Enforces minimum quality standards before merging code.
-
Security Reports: Checks for OWASP Top 10 vulnerabilities.
-
Integration: Works with Jenkins, GitHub, Bitbucket, GitLab, and more.
-
Dashboards: Visual reports and metrics for maintainability, reliability, and security.
🌟 Benefits of SonarQube
Benefit | Description |
---|---|
1. Improved Code Quality | Detects bad practices and enforces coding standards automatically. |
2. Early Bug Detection | Finds bugs during development rather than after deployment. |
3. Enhanced Security | Identifies vulnerabilities like SQL injection, XSS, etc. |
4. Continuous Integration Support | Seamlessly integrates with CI/CD tools for automated checks. |
5. Multi-Language Support | Works across many programming languages in the same project. |
6. Technical Debt Measurement | Calculates how much effort is needed to fix issues. |
7. Custom Quality Gates | You can define thresholds to block builds if quality criteria fail. |
8. Easy Reporting | Provides clear dashboards and reports for developers and managers. |
- Root or sudo access on the Rocky Linux 8 host.
- Internet access to download packages.
- Recommended memory: 4 GB minimum for test, 8+ GB for production.
- Recommended disk: 10+ GB free.
- Pick SonarQube version (example uses 9.9 LTS). Ensure matching Java version (Java 17 for SonarQube 9.9+).
- Use a secure password and secure Sonar token in production (examples below use placeholders).
- SonarQube server installed under /opt/sonarqube (run by user sonar).
- PostgreSQL as backing DB (database sonarqube, user sonar).
- Elasticsearch is bundled; kernel parameter vm.max_map_count must be tuned.
- Systemd used to run SonarQube as a service.
# dnf install -y java-17-openjdk-devel
# java -version
# dnf install -y postgresql-server postgresql-contrib
# postgresql-setup --initdb --unit postgresql
# systemctl enable --now postgresql
# sudo -u postgres psql -c "CREATE USER sonar WITH ENCRYPTED PASSWORD 'redhat';"
# sudo -u postgres psql -c "CREATE DATABASE sonarqube OWNER sonar ENCODING 'UTF8' LC_COLLATE='en_US.utf8' LC_CTYPE='en_US.utf8' TEMPLATE template0;"
# sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonar;"
find active file
Reset sonar password to match sonar.properties
Download and install SonarQube to /opt
# cd /opt
# dnf install -y unzip curl
# curl -L -O https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.9.0.65466.zip
# unzip sonarqube-9.9.0.65466.zip
# mv sonarqube-9.9.0.65466 sonarqube
# groupadd sonar
# useradd -r -s /sbin/nologin -g sonar sonar
# chown -R sonar:sonar /opt/sonarqube
# chmod -R u+rwX,go-rwx /opt/sonarqube
sonar.jdbc.username=sonar
sonar.jdbc.password=redhat
sonar.jdbc.url=jdbc:postgresql://127.0.0.1:5432/sonarqube
sonar.web.host=0.0.0.0
sonar.web.port=9000
Make systemd use Java 17 for SonarQube (drop-in)
# mkdir -p /etc/systemd/system/sonarqube.service.d
[Service]
Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk"
Environment="PATH=/usr/lib/jvm/java-17-openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
EOF
Description=SonarQube service
After=syslog.target network.target postgresql.service
[Service]
Type=forking
ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
User=sonar
Group=sonar
LimitNOFILE=65536
LimitNPROC=4096
TimeoutStartSec=300
Restart=on-failure
[Install]
WantedBy=multi-user.target
Elasticsearch kernel tuning
# sysctl -w vm.max_map_count=262144
vm.max_map_count=262144
EOF
<artifactId>sonar-maven-plugin</artifactId>
<version>3.9.1.2184</version>
</plugin>
root@tomd01:/git/tomcat-war# mvn clean verify sonar:sonar \
-Dsonar.host.url=http://son01.darole.org:9000 \
-Dsonar.login=sqa_361fedc9dc911e16f5cc6dd1f4a3b3145318c97f
Tips for Maven/Sonar usage
- Use a Sonar token (generated in SonarQube user account > Security) instead of a username/password. Keep the token secret.
- You can store sonar.host.url and sonar.login in ~/.m2/settings.xml or CI pipeline environment variables to avoid exposing tokens on the command line.
- For multi-module projects, run the mvn sonar:sonar from the project root. Consider setting sonar.projectKey and sonar.projectName via properties or in the root pom.
Troubleshooting checklist
- If SonarQube fails to start: check /opt/sonarqube/logs/{sonar.log,web.log,es.log,ce.log,nohup.log}.
- Java wrong version: UnsupportedClassVersionError → install Java 17 and set JAVA_HOME.
- Elasticsearch bootstrap failure: vm.max_map_count too low → set to 262144.
- DB connection errors: check pg_hba.conf ordering (first match wins), ensure md5/scram for localhost and that sonar.jdbc.* in sonar.properties matches DB user/password. Test with:
# PGPASSWORD=<your_password> psql -h 127.0.0.1 -U sonar -d sonarqube -c "SELECT 1;"
- Port 9000 not responding: verify Sonar web server started successfully in web.log and that firewall allows access.
- SonarQube provides code-quality and security analysis for many languages. After installation and Maven integration, tune quality profiles, configure projects, and integrate the scanner into CI for automated code analysis.
- If you want, I can generate a single runnable shell script to perform the full install and basic configuration for Rocky Linux 8, or show an example CI pipeline step for GitLab/GitHub Actions to run Sonar scans securely.
No comments:
Post a Comment