Incorrect default branch set in SonarCloud

  • ALM used BitBucket
  • CI system used BitBucket
  • Languages of the repository - PHP

Hello. I am encountering a problem with my SonarCloud scanner. It seems to be picking up the wrong branch as a default one. In “Main Branch” I get a message saying ““DCSB-691-improvments-in-update-itemised-sale-functionality” branch has not been analyzed yet and you have multiple branches already. It looks like it is not your Main Branch, check your configuration.”

We are using BitBucket for our CI. The main branch in BitBucket is selected as develop. I can see “develop” branch in “Branches” in SonarCloud and I can see the pull requests, but I can’t see it as a default overview of the project.


How can I change it so that develop becomes the Main Branch? If I remove DCSB-691-improvments-in-update-itemised-sale-functionality branch from BitBucket, would it automatically switch to develop as being the main branch?

Hey there.

The default branch should get set correctly when your project is bound – is that the case for this project?

(You can delete the develop branch and then rename your current main branch develop, but the best case scenario is to have a bound project).

Hi Colin!

Thanks for your reply. I just checked there and the project is bound to BitBucket but the default branch is still showing up incoorrectly.
image
The main branch in BitBucket is set as “develop” as can be seen in the screenshot.

Thanks.

That does seem out of the ordinary. I can’t reproduce it on my side.

Do you mind sharing your Bitbucket Pipelines YML file?

Of course, that’s no problem! I’ve just removed the path from the command

clone:
  depth: full

definitions:
  caches:
    sonar: ~/.sonar/cache
  services:
    docker:
      memory: 3072
  steps:
  - step: &build-test-sonarcloud
      name: Build, test and analyze on SonarCloud
      image: webdevops/php-apache-dev:8.1
      size: 2x
      runs-on:
        - self.hosted
        - linux
      caches:
        - composer
        - node
        - sonar
      script:
        - curl -L https://deb.nodesource.com/setup_18.x | bash && apt-get install -yq nodejs
        - composer install && php artisan migrate:fresh --seed && php artisan optimize:clear && npm install && npm run dev
        - pipe: sonarsource/sonarcloud-scan:1.4.0
          variables:
            SONAR_TOKEN: ${SONAR_TOKEN}
            SONAR_SCANNER_OPTS: -Xmx2048m
  - step: &check-quality-gate-sonarcloud
      name: Check the Quality Gate on SonarCloud
      runs-on:
        - self.hosted
        - linux
      script:
        - pipe: sonarsource/sonarcloud-quality-gate:0.1.6
  - step: &develop-deployment
      name: Deployment on dev server
      runs-on:
        - self.hosted
        - linux
      deployment: develop
      script:
        - pipe: atlassian/ssh-run:0.4.1
          variables:
            SSH_USER: ${DEVELOP_SSH_USER}
            SERVER: ${DEVELOP_SSH_SERVER}
            MODE: 'command'
            COMMAND: 'cd ****/public_html && git checkout develop && git pull origin develop && composer install && php artisan optimize:clear && npm install && npm run dev'
  - step: &develop-deployment-seed
      name: Seed test database on dev server
      runs-on:
        - self.hosted
        - linux
      trigger: manual
      script:
        - pipe: atlassian/ssh-run:0.4.1
          variables:
            SSH_USER: ${DEVELOP_SSH_USER}
            SERVER: ${DEVELOP_SSH_SERVER}
            MODE: 'command'
            COMMAND: 'cd ****/public_html && php artisan migrate:fresh --seed && php artisan optimize:clear && npm install && npm run dev'

pipelines:
  branches:
    develop:
      - step: *develop-deployment
      - step: *build-test-sonarcloud
      - step: *develop-deployment-seed
      #- step: *check-quality-gate-sonarcloud

Let me know if you’ll need anything else

Hey there.

Thanks for sharing. Nothing looks amiss.

Just to understand – separate from whether or not the project was created with the correct main branch, were you able to delete the develop short-lived branch and delete the original main branch? Just want to make sure you’re not still blocked there.

Hey Colin,

When you are saying to delete the branches, do you mean to delete them in BitBucket or in Sonarcloud? If it’s in Sonarcloud, I have an option to delete the “develop” branch, but not the [DCSB-691-improvments-in-update-itemised-sale-functionality]MAIN BRANCH. The only option I have for the main branch is Rename?

You would need to delete the develop branch, and rename the main branch to develop.

Or, delete the project and recreate it. If the main branch name is still not correct by default, make sure develop is the first branch analyzed.

Here is a project I created after changing the main branch to develop where the branch is accurately reflected. Which is why I’m puzzled it’s not the case for you.