user13821287
user13821287

Reputation:

Run SonarCloud Analyze Manually From Visual Studio

I have paid account in Sonarcloud and Gitlab CI for automation, I use Visual Studio with integrated SonarLint for C#. Sometimes I want to run analyzing manually from VS and not using Gitlab runner every time. Is there a way to bypass pipeline? The reason is that I have limitations for runner minutes and I want to make commit and run pipeline only when I cleanup code warnings from SonarLint.

Upvotes: 3

Views: 9566

Answers (1)

DV82XL
DV82XL

Reputation: 6629

SonarScanner

You can trigger a SonarCloud analysis locally using SonarScanner:

  1. SonarScanner for MSBuild as a stand-alone executable.
  2. .NET Core global Tool installed with NuGet, also called as executable.
  3. Download the SonarScanner CLI binary and run it from the command line.

The MSBuild option looks something like this:

SonarScanner.MSBuild.exe begin /k:"project-key"
MSBuild.exe <path to solution.sln> /t:Rebuild
SonarScanner.MSBuild.exe end

The .NET Core global tool looks like this:

dotnet tool install --global dotnet-sonarscanner
dotnet sonarscanner begin /k:"project-key"  /d:sonar.login="myAuthenticationToken"
dotnet build <path to solution.sln>
dotnet sonarscanner end /d:sonar.login="myAuthenticationToken"

Since You need to call SonarScanner before and after a build. you can integrate the SonarScanner commands in Visual Studio using Build Events. Add the commands to PreBuildEvent.bat and PostBuildEvent.bat to run the analysis automatically every time you build. If you don't want to run SonarScanner every time you build, create a new custom build configuration (e.g. Debug, Release, Analyze).

You will need to generate a private token for your project key in SonarCloud. The final report will also be available in that project when it is ready.

SonarLint

If you don't need to update SonarCloud and just want to view the code analysis results, you can use SonarLint Visual Studio extension. You can connect SonarLint with SonarCloud to download your common configuration using a feature called Connected Mode.

Connected mode does not push issues to the server. Rather, its purpose is to configure the IDE so that it uses the same settings as the server.

A summary of instructions (see Connected Mode for details):

  1. Open the Team Explorer Home tab and click on the SonarQube icon
  2. Click on Connect... to display the connection dialogue
  3. Select the server and enter your credentials
  4. Select the Organization (SonarCloud only)
  5. Select the Sonar project to bind to

SonarLint will then fetch the required settings from the server and create local configuration files

Your code will be analyzed live (as you type), or you can run a full code analysis as follows:

  • Right click solution -> Analysis -> Run Code Analysis

Upvotes: 3

Related Questions