SonarQube issue - Could not find branches



SonarQube allows branch level (for example, if you are using Git, you may want to scan your feature branch and fix identified issues before creating a Pull/Merge Request to the develop branch) analysis. In order to do branch level analysis, you need to specify your branch using SonarQube parameter 'sonar.branch.name' and optionally 'sonar.branch.target' (the name of the branch into which the temporary branch specified with 'sonar.branch.name' will be merged.
However, if SonarQube project does not exist for the given source code repository yet and you are scanning your source code repository for the first time and you specified your branch name using 'sonar.branch.name', you'll get the following error:
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project web-mywebapps: Could not find branches. A regular analysis is required before creating branches. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:

As you can see from the error message, it says "...could not find branches. A regular analysis is required...". So, for the first scan whatever branch you are scanning, do not use parameter 'sonar.branch.name' One way to tackle this issue is to write some conditional logic in your SonarQube job. Below is a kind of a pseudo code example. Here the -Dsonar.branch.name property and value is passed to Maven goal only if the variable 'FIRST_TIME_BUILD' is false.
Note: 'FIRST_TIME_BUILD' is not a SonarQube built in variable, I've used it as an example to specify the condition.

<goals>clean install sonar:sonar -Dsonar.host.url=\$SONAR_HOST_URL <% if(!FIRST_BASELINE_BUILD) {%> -Dsonar.branch.name=<%= sonarBranchName%> <%}%> -Dorg.xml.sax.driver=com.sun.org.apache.xerces.internal.parsers.SAXParser -U</goals>

Hope it helps. For details on SonarQube branch analysis, visit https://docs.sonarqube.org/latest/branches/overview/