Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

SonarQube .NET Demo

This project demonstrates integrating a simple .NET Console application with SonarQube for static code analysis and quality checks.

Prerequisites

  • Docker for SonarQube
  • .NET SDK for building and testing
  • SonarQube instance (local or cloud)

Set Up SonarQube (Docker)

  1. Clone the repo and run SonarQube with Docker Compose:

    cd container-labs/labs/sonar/sonar-basic/
    docker-compose up -d

    sonar-up

    NOTE: The docker compose file using in this project is for Docker Desktop, if you use linux based OS, adjust the volume mount command appropriately

  2. Access SonarQube at http://localhost:9000 (default credentials: admin/admin).

  3. Create a project in SonarQube and get the project key and token. project-create

Run the Project

  1. We will run a sample .NET code scan in this repository, located at sonar-basic/dotnet-demo

  2. Navigate to the samples you want and restore dependencies (e.g: core/getting-started/unit-testing-using-dotnet-test):

    dotnet restore
  3. Run the application:

    dotnet run

Run Unit Tests

dotnet test

SonarQube Analysis

  1. Install SonarScanner:

    dotnet tool install --global dotnet-sonarscanner
  2. Start analysis:

    dotnet tool install --global dotnet-sonarscanner
    dotnet sonarscanner begin /k:"project-key" /d:sonar.token="<token>" /d:sonar.host.url="http://localhost:9000"
    dotnet build
    # or 'dotnet build <path to project file or .sln file> --no-incremental'
    dotnet sonarscanner end /d:sonar.token="<token>"

    NOTE: If we cannot execute dotnet sonarscanner, try to open new terminal or reload the cmd. Or make sure the dotnet Path is in environment variables

  3. View results at http://localhost:9000. Once completed you can get the result like this: project-scan-view project-scan-view1

References

License

MIT License.