Only analyse Master Branch

Hi there

Just trying to figure out how to setup SonarQube to only run when building from the Master branch.

I found this from another post:

"We are using Azure DevOps so all I had to do was add a condition so that the Sonar tasks only run on the master branch.

condition: and(succeeded(), eq(variables[‘Build.SourceBranch’], ‘refs/heads/master’))"

I am just not sure were to add this condition? Is it in the “Prepare Analysis” step under “Custom Conditions”? Or someplace else?

I also found this in the SonatQube documentation:

Limiting analysis to relevant branches

You need to add a condition to your pipeline script to ensure only relevant branches are analyzed. For example, you wouldn’t want to run analysis on feature branches that won’t need analysis until they have pull requests .

In the following example, analysis would be limited to branches named master or release/* .

if [[ "$CI_BRANCH_NAME" == master ]] || [[ "$CI_BRANCH_NAME" == release/* ]]; then
  ./gradlew sonarqube

Just not sure were I would add this either - as part of the Prepare Analysis task or as a separate task (as a script)?

Any advice really appreciated.


Hi Julio,

Welcome to the Community!

You are heading into the right direction, however here we are completely under Azure DevOps’ domain and not SonarQube’s, so I would strongly suggest for you to knock on Azure’s door/documentation. For example, I could find their documentation on conditional pipeline steps here. If you don’t want to analyse other branches, you won’t need the SonarQube related tasks to execute in your job (neither Prepare Analysis nor Run Analysis nor Quality Gate…) when you detect you are not on the Master branch.

I think Azure’s documentation is pretty clear and even includes nice examples for your case :smiley:


Thanks Daniel

Yep I figured it out!