Reputation: 19968
SonarQube has the concept of a main branch irrespective of its actual name. According to the documentation, the key is not to pass the sonar.branch.name
property to the analysis.
So far, so good. However, when my Gradle build in Github does not set the property, then automatic branch detection kicks in and I get:
Detected branch/PR in 'GitHub Action'
Auto-configuring branch 'develop'
Well, that’s unfortunate, because now sonar.branch.name
is set to develop
, and SonarQube will not accept it as the main branch. This develop
is set as the default branch in Github, but the scanner seems not able to detect this. Automatic detection will not happen when I set the property explicitly, but then of course it is still set to something.
The question is now: Can I skip branch detection in the Sonarscanner without setting sonar.branch.name
? Can I influence the branch detection so it will correctly detect it is on the default branch?
We are using Github actions in a GitHub Enterprise Server 3.5.5, for reference.
Upvotes: 3
Views: 10733
Reputation: 1
SonarQube has different edition (community, developer, enterprise, ...), which contain different scopes of analysis tools. The community edition as an example is limited to accumulate all analysis data within one branch. This is called main branch for analysis, not matter which name it actually has within your scm. If you run SonarQube - Community Edition and run the analysis within your CI for different branches, they will all result in different runs for the main branch.
The developer edition allows to run the analysis for separate branches of repository, which is then compated the the main branch. The name of main branch is configured within the project settings of the SonarQube server for the corresponding project.
The properties parameter sonar.branch.name
tells the sonar-scanner/sonarQube plugin, for which branch the current analysis run should be stored. This is be default the currently checked out branch, for which the analysis has been run.
If you want to set the "main" branch or let's call it reference branch you need to open your project within the SonarQube server. Open the "Project Settings" -> "Branches & Pull Requests". Here you will find all branches, which have analysis data. One is marked with "MAIN BRANCH". If you click on actions, you can rename it and set the name to your develop/main/master/trunk whatever the name is.
Upvotes: 0
Reputation: 1214
well i faced the same issue, while doing CICD with github action.
i use, this code to get the current branch
property "sonar.branch.name", System.getenv("BRANCH")
currentBranch is variable set in github actions
env:
BRANCH: ${{github.event.pull_request.head.ref}}
Upvotes: 1
Reputation: 19968
I have an answer that feels more like a work-around, but seems to work. Within SonarQube, I can rename the main branch. This is more than just a cosmetic change. I set the value to develop
, and the next SonarQube analysis with sonar.branch.name=develop
was accepted as the main branch. This works – for now – but does not seem to be the documented and preferred way.
Upvotes: 3