Dagger SonarQube
We’ve been experimenting with Dagger in our previous tests, but let’s not forget to check our code quality! 😎
SonarQube
SonarQube is an open-source code quality tool that ensures our code adheres to best practices and clean code principles.
SonarQube
It has many other features, but for now, we’ll focus on integrating Sonar analysis into our CI/CD pipeline using Dagger.
While there’s a Dagger module for SonarQube, I couldn’t get it to work with a self-hosted SonarQube URL:
Daggerverse Sonar
I might open a Pull Request or an Issue with the module maintainer to resolve this problem (or maybe it’s a skill issue 🫣).
In the meantime, we’ll do it ourselves! It’s just a SonarCLI container, so it shouldn’t be too difficult.
Integration
Let’s revisit our project lib-acl-json
.
We already have the following functions to install packages and run unit tests:
Sonar Scanner CLI
Since Dagger natively supports working with containers, we’ll use the Sonar Scanner CLI image:
Sonar Scanner
Here’s the scan function:
.stdout()
returns the result as a string. If you prefer, you can omit it to return aPromise<Container>
..withUser("root")
ensures the container has the necessary permissions for analysis.
Testing the Function
First, set your token as an environment variable:
Now, run the function with dagger call
:
Result:
The analysis was successful! 🎉
Adding Code Coverage
Currently, we don’t see code coverage in Sonar because we haven’t executed the tests or captured the results.
Let’s create a function that executes multiple steps:
preBuild
returns the directory afterbun install
, allowing us to build the project later.unitTest
stores the directory after running tests, ensuring coverage data is available for Sonar analysis.
Run the minimal
function:
Result:
We see the steps bun install
, test
, and Sonar analysis
completed successfully.
The project’s coverage percentage is now visible in the Sonar interface:
Why Use a Minimal Function?
The minimal
function lets developers run Sonar analysis without building the entire project.
If they want to build later, Dagger’s cache makes the process faster.
Full index.ts
File
Here’s the complete index.ts
:
Demonstrating Cache Efficiency
Run the build function:
Result: 4.4 seconds!
With caching, only the build step needs to run.
Conclusion
As you’ve seen, integration is straightforward thanks to containerized tools like Sonar Scanner.
Dagger’s use of containers makes local pipelines closely resemble production environments, streamlining CI/CD processes.
GitHub
Project link: lib-acl-json