ALM used
GitHub
CI system used
Jenkins
Languages of the repository
Java 21, Maven 3.8.7
We cannot get the PR incremental cache to work reliably on the CI, whatever we do. We have a project of several thousand files, so I will illustrate the bad cache hit with the logs of only one module.
I manage to reproduce the issue in local, but also to actually make it work in one specific case.
Working scenario
- Checkout of main in the directory /home/project/app
- Run a full analysis (to update the remote cache in SonarQube Cloud) :
mvn -Dsonar.projectKey=xxx -DskipTests=true -T 1C --batch-mode -Dsonar.organization=xxxx clean package sonar:sonar - Create a PR from the main branch. Change only one line of one file.
- Run a PR analysis
mvn -Dsonar.pullrequest.key=21691 -Dsonar.pullrequest.branch=feature/PrThatShouldWork -Dsonar.pullrequest.base=main -Dsonar.projectKey=xxx -DskipTests=true -T 1C --batch-mode -Dsonar.organization=xxxx clean package sonar:sonar -X
Here, and only here do I get a good cache hit :
Server-side caching is enabled. The Java analyzer was able to leverage cached data from previous analyses for 3430 out of 3951 files
Failing scenario
This is done right after the previous scenario.
- Checkout of main in the directory /home/project/app2, at the exact same commit
- Do not run a full analysis. The cache is already up to date and should be used
- Create a PR from the main branch. Change only one line of one file (the same change as previously)
- Run a PR analysis
mvn -Dsonar.pullrequest.key=21692 -Dsonar.pullrequest.branch=feature/PrThatDoesNotWork -Dsonar.pullrequest.base=main -Dsonar.projectKey=xxx -DskipTests=true -T 1C --batch-mode -Dsonar.organization=xxxx clean package sonar:sonar -X
Now, the cache hit is terrible
Server-side caching is enabled. The Java analyzer was able to leverage cached data from previous analyses for 1539 out of 3951 files. These files will not be parsed.
We get the exact same result on every PR on our CI. This means that the PR builds last about 10 minutes instead of 4~5 minutes.
In the described scenario, we use the same :
- Project
- JDK
- Base branch with same commit
- OS
The only difference that I can think of would be the absolute path of the files. Is this how the Sonar remote cache work ? Are we supposed to run our CI build always on the same path ? It is possible that I am on the wrong path. I have the full debug run of the 3 executions described above, and I can share any part that could help identify the issue.
Edit : Additional tests that seem to confirm the cache depends on the base directory
First test:
- Deleted the /home/project/app which moved it to /home/xxx/.local/share/Trash/files/app
- Ran the sonar pr analysis from there
- Bad cache hit
Second test:
- Created a new /home/project/app (same name as first step of working scenario) from zero and checked out my app
- Ran the sonar pr analysis from there
- Good cache hit