Not sure if this is a sonarcloud or a vsts issue but I’ve noticed that the “Run code analysis” task is not using the full branch name anymore.
Take this example: the branch name is story/1234-NameOfTheStory. In sonarcloud the branch is reported as 1234-NameOfTheStory. Somehow it picks the string after the /.
2018-07-19T10:02:07.2138351Z INFO: Branch name: 1234-NameOfTheStory, type: short living
To make matters even more confusing, everything works perfectly fine If I run the definition manually. I can only see this type of behaviour only when the build runs as a result of a trigger being fired.
I noticed a similar thing with ours, I think the forward slashes cause problems.
My workaround was to just rename the branches for when passed to SonarCloud, for example in the advanced settings box of the prepare analysis task you could put:
sonar.branch.name=story-$(Build.SourceBranchName)
This will name all your branches in SonarCloud as story-1234-NameOfTheStory and you can set the appropriate regex from there.
The SonarQube Scanner for TFS/VSTS extension was updated recently (v4.3.2) to use the Build.SourceBranch variable instead of Build.SourceBranchName, the former of which preserves the full branch name while the latter only uses the last path segment in the ref.
Could it be possible that, perhaps by chance, when the builds have run because of a trigger being fired, that build agent is using an older (4.3.1 or earlier) version of the extension? Check your build logs.
You can also set in your prepare analysis task…
sonar.branch.name=${Build.SourceBranch}
But I would check what version of the extension is being used (note that is different than the version of SonarQube Scanner for MSBuild).
Adding sonar.branch.name=${Build.SourceBranch} did not do the trick. The analysis results went in the master branch. I’ll try the Peter’s solution as a workaround.
@Colin - for some reason the agent is using 4.3.1. Not sure I can update it as it’s managed by Microsoft.
2018-07-19T16:49:46.8286754Z SonarScanner for MSBuild 4.3.1
2018-07-19T16:49:46.8287981Z Using the .NET Framework version of the Scanner for MSBuild
Finally nailed it. It turns out that vsts does not like pascal case. It works just fine when doing this: sonar.branch.name=story/$(build.sourcebranchname).