Hello, I’m still relatively new to SonarQube so forgive me if there are any mistakes/misinterpretations.
Whenever the scan job from the GitLab CI/CD pipeline runs for a pull request, I want the sonar scanner to find the common ancestor between the PR and master and use that to find the new or changed code to scan for SonarQube. However, it sometimes doesn’t find this merge base, resulting in the new code on the PR analysis report being completely wrong because the PR branch is behind the master branch analysis report on the SonarQube portal (the new code is set to “Previous Version”).
I first run git checkout branch_name_here
and make dummy changes, then I push onto GitLab as a PR (Merge Request in Gitlab’s case) and run the scan job (as it is manual).
To run sonar scanner, it is unzipped using the Dockerfile so that it can be run in the docker build environment and then is run in the Gitlab CI/CD pipeline as part of a scan job after the build wrapper is finished observing the clean build.
What I tried so far in the .gitlab-ci.yml
file to try to fix the issue:
- Make the
GIT_DEPTH
variable set to 0 - Run the
git fetch --all
command within the scan job - Run
git fetch origin
- Make dummy changes on one file and push onto the pipeline (Sometimes works, sometimes not. For example, we made a dummy PR with this change, and it did find the merge base, the second commit and run on the pipeline it did not find a merge base)
- Run the pipeline on various runners (sometimes they find the merge base, most of the time they do not, regardless of runner)
Job log before actual analysis:
INFO: Load project repositories
INFO: Load project repositories (done) | time=44ms
INFO: SCM collecting changed files in the branch
WARN: No merge base found between HEAD and refs/heads/master
INFO: SCM collecting changed files in the branch (done) | time=7159ms
Job log at the near end of the scan job:
INFO: SCM writing changed lines
WARN: No merge base found between HEAD and refs/heads/master
INFO: SCM writing changed lines (done) | time=8703ms
INFO: Analysis report generated in 8914ms, dir size=11.3 MB
The version of SonarQube I’m using is 9.9.1 developer edition. Java version is v17.
If there is any information I missed sharing please let me know, thank you!