Setting Up SonarQube with

GitHub Actions

By Siva Dondapati
10 mins Read
In the fast-paced world of software development, maintaining high code quality is crucial for ensuring reliable, maintainable, and performant software. Among the various tools and techniques available, integrating SonarQube with GitHub Actions stands out as a powerful combination. This integration automates code quality checks, provides comprehensive insights, and fosters a culture of continuous improvement. In this blog, we will explore the benefits of integrating SonarQube with GitHub Actions and how it can elevate your development process.

What is SonarQube?
SonarQube is an open-source platform that continuously inspects the quality of source code. It analyzes code for various quality metrics, including code smells, bugs, vulnerabilities, and technical debt. By providing detailed reports and actionable feedback, SonarQube helps developers maintain high standards of code quality throughout the development lifecycle.

What are GitHub Actions?
GitHub Actions is a powerful automation tool provided by GitHub. It allows developers to automate workflows directly within their repositories. From running tests and deployments to integrating with third-party tools, GitHub Actions can streamline numerous aspects of the software development process.

Setting Up SonarQube with GitHub Actions

Step 1: Setting Up SonarQube Server
First, we'll set up the SonarQube server on a Linux machine without using Docker.

1.1 Install Java
SonarQube requires Java to run. We'll install OpenJDK 17, which is the recommended version.
1.2 Install and Configure PostgreSQL
SonarQube uses a database to store its data. We'll use PostgreSQL 15 for this purpose.
1.3 Install SonarQube
Download and extract the latest version of SonarQube from the SonarQube Downloads page:
1.4 Configure SonarQube
Edit the SonarQube configuration file to set the database connection parameters:
1.5 Start SonarQube
Start SonarQube manually:
Step 2: Configure SonarQube
Now that SonarQube is running, let's configure it through the web interface.

2.1 Access SonarQube
Open a web browser and navigate to http://<your-server-ip>:9000. Log in with the default credentials (admin / admin) and change the default password.

2.2 Generate a New Sonar Token
In the SonarQube dashboard, click on “My account > Security”. Enter Token Name and generate a new project token and save it securely, as we will need this token later for GitHub Actions.
Step 3: Set Up GitHub Actions
Next, we will create a GitHub Actions workflow to integrate SonarQube scans into our CI/CD pipeline.

3.1 Set Up GitHub Secrets
In your GitHub repository, go to Settings > Secrets > Actions.

  • Add new secrets named SONAR_TOKEN and SONAR_HOST_URL.
  • SONAR_TOKEN: Paste the project token you generated earlier.
  • SONAR_HOST_URL: Paste your SonarQube public URL (e.g., http://<your-server-ip>:9000).

3.2 Create a Workflow File
In your GitHub repository, create a new file in the .github/workflows directory (e.g., sonarqube.yml). Use the following YAML configuration as an example for Node.js projects:
Note: The above YAML configuration is an example for Node.js projects. If you are using a different language or framework, the configuration steps will differ. Please refer to the SonarQube documentation for specific instructions on setting up SonarQube scans for other languages.

3.3 Trigger the Workflow
Push changes to the development branch to trigger the SonarQube scan.

3.4 Check Analysis Results
After the workflow runs, go to your SonarQube dashboard to check the analysis results. Navigate to your project in SonarQube, where you will find detailed reports on code quality, bugs, vulnerabilities, code smells, and more.
Conclusion

Integrating SonarQube with GitHub Actions brings numerous benefits to the software development process. From automated code quality checks and immediate feedback to enforcing quality gates and fostering continuous improvement, this integration enhances the overall quality of the codebase. By making code quality an integral part of the development workflow, teams can deliver reliable, secure, and maintainable software with confidence. Embrace the power of SonarQube and GitHub Actions to elevate your code quality to new heights