Branches are not created on sonarcloud, using bitbucket

Hello, I am running sonar-scanner for .net core in a Bitbucket pipeline.
I’ve also installed the SonarCloud for Bitbucket cloud and configured the right sonar project in Bitbucket under “Repository->SONARCLOUD->Settings” in the drop down.

Whenever such a build is completed successfully, I can see the new analysis in the right project at sonarcloud.io, but it is always under master branch, even if the change was done in a forked branch and also the pipeline runs it in this forked branch. I guess this is also the reason why my PRs are not decorated. In other words, no other branch than master, has ever been created on SonarCloud.io

Can you please help? Do you need the pipeline build log for that?

Thanks,

Itzik

Hi,

can you share:

  • how you trigger the analysis in your Pipelines YML file
  • the logs

Hello Fabrice,
Attached is the log file and the YML file run by bitbucket pipline.
pipelineLog-108.txt (52.2 KB)

Bitbucket-pipelines. .t (881 Bytes)

BTW, lately I added this parameter: /d:sonar.branch.name=$BITBUCKET_BRANCH and it does create a branch in sonarcloud, and when I create a pull request in bitbucket, from a new branch to master, I do see the Code Quality section in the pull request being filled

(see image attached), but when I click on the “See all issues on Sonar Cloud”, I’m being redirected to SonarCloud.io and I get the message “Component ‘MyHDP’ of pull request ‘hdp_test2’ not found”

Hi,

it looks like some environment variables might be missing from the Docker image you are using - which would prevent the auto-detection to work properly. Can you add those 2 following lines to your YML file (right after the last “sudo dotnet” command) and send us the logs:

env | grep BITBUCKET_
env | grep CI
1 Like

Hi Fabrice, much appreciated.:slightly_smiling_face:

Attached is the log as you asked for
pipelineLog-111 (1).txt (52.6 KB)

Hi,

I think the problem come from the use of sudo to run the scanner. The original environment variables are not available in the sudo context:

Maybe try to run sudo --preserve-env instead, or don’t use sudo if you can.

Thanks Julien, you nailed it! Adding -E to sudo did the job. (sudo -E dotnet…)

:mage:

Julien, as I told you in the previous reply, your solution works. Nonetheless, the behavior I see is weird: When I create a pull request in Bitbucket, a corresponding PR is not being created on SonarCloud. Only after I do another commit and push to that PR, a corresponding PR is being created on SonarCloud.

This is a limitation of Bitbucket Pipelines. They don’t trigger a build when PR is created.
Feel free to watch/vote for https://bitbucket.org/site/master/issues/13438/add-specific-pipelines-configuration-for

Basically, let say you have an analysis of your master branch on SonarCloud. Locally you create a feature branch feature/foo and add a few commits. Then you push the branch on Bitbucket Cloud. It will trigger a pipelines build of the branch, and our integration will configure a branch analysis.

Then you open a new PR in BBC UI. Nothing happens (no build is triggered).

Then you push more commits to your feature branch. A pipelines build is triggered. Our integration will detect that there is an open PR for this branch, and so configure a PR analysis. As a consequence your branch analysis will stop being updated.

:disappointed_relieved:

Does it work well on github?

GitHub alone doesn’t have any build feature. But if you add travis-ci (using our integration) it works better indeed.
travis-ci will triger one build as soon as you create a PR. And each time you push to your branch, it will trigger two builds (one of the branch, and one of the PR “virtual” merge commit).