SonarQube + Jenkins pull request analysis

which versions are you using (SonarQube, Scanner, Plugin, and any relevant extension)

  • Sonarqube version: 9.4
  • SonarScanner

what are you trying to achieve

Trying to set up a jenkins pipeline to analyze pull requests.

  • what have you tried so far to achieve this

I’ve achieved the analysis but so far every time the pipeline runs, it analyzes the target branch and then the pull request, so the scanner runs two times and this takes a long time.
So, I’m trying to optimize this because if I commit more than one time on the pull request branch, the analysis of the target is done multiple times (unnecessarily). If I only analyze the pull request, then sonar gives me an error because the target has never been analyzed.
¿Which would be the best way of doing this?

Also, I could trigger separated jobs for branch and pull request, but in this case, if these executes in a brief period of time, there’s the possibility that when the pull request analysis is being executed, the branch analysis hasn’t finished yet or maybe it is on an outdated version.

Another idea is to analyze the target branch only on the first time that the pull request analysis job executes. In this case, ¿what would happen if after the first analysis, the target changes? I guess the next analysis of the pull would have an incorrect comparison, right?

Hey there.

Ideally, the target branch is configured to be analyzed everytime (and only when) there’s a commit to it.

When triggering a pull request, only a pull request analysis should be performed.

What would happen if the pull request is analyzed while the target branch is being analyzed for the first time? Would the pull request analysis throw an error?