2

I have a dotnet image that is used as an agent for the Jenkins pipeline. Now I want to include sonar scanner in the image so that I can run an analysis and see if the coverage is good. If the coverage is not good then the build should fail. How to include the sonar scanner in my image.

I tried including the skilldlabs/sonar-scanner in my Dockerfile of the dotnet image. but when I run the container it directly executed the sonar cube commands and failed as the default sonarqube address is used.

Below is my current Dockerfile

FROM microsoft/dotnet:2.1-sdk
FROM skilldlabs/sonar-scanner:3.3

COPY some-ca.crt /usr/local/share/ca-certificates
COPY NuGet.Config /build/.nuget/NuGet/

VOLUME [ "/build/sources" ]

WORKDIR /build/sources

When I ran :

docker run --name sonar -it sonar

INFO: Scanner configuration file: /root/sonar-scanner-3.3.0.1492-linux/conf/sonar-scanner.properties

INFO: Project root configuration file: NONE

INFO: SonarQube Scanner 3.3.0.1492

INFO: Java 1.8.0_191 Oracle Corporation (64-bit)

INFO: Linux 4.9.125-linuxkit amd64

INFO: User cache: /root/.sonar/cache

ERROR: SonarQube server [http://sonarqube:9000] can not be reached

INFO: ------------------------------------------------------------------------

INFO: EXECUTION FAILURE

INFO: ------------------------------------------------------------------------

INFO: Total time: 5.433s

INFO: Final Memory: 3M/39M

INFO: ------------------------------------------------------------------------

ERROR: Error during SonarQube Scanner execution

ERROR: Unable to execute SonarQube

ERROR: Caused by: Fail to get bootstrap index from server

ERROR: Caused by: sonarqube: Try again

ERROR:

ERROR: Re-run SonarQube Scanner using the -X switch to enable full debug logging.

How can I tell the container to provide the config for the sonar-scanner?

1 Answers1

1

Instead of using the sonar scanner image into my image, I have installed dotnet-sonarscanner using the below command,

dotnet tool install -g dotnet-sonarscanner

I had to install a "coverlet" package to my unit test project by adding the below to my .csproj file of the unit test project.

<PackageReference Include="coverlet.msbuild" Version="2.6.1">
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
  <PrivateAssets>all</PrivateAssets>
</PackageReference>

Now, whenever I want to send sonarqube my coverage results I run below command to generate the coverage file.

dotnet test ./UnitTests/UnitTests.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=opencover

The above command will generate the coverage.opencover.xml file in the project folder.

Now use below commands to send the coverage

dotnet sonarscanner begin /k:"yourprojectkey" /d:sonar.host.url=https://yoursonarqubedomain.com /d:sonar.cs.opencover.reportsPaths="./UnitTests/coverage.opencover.xml" /d:sonar.coverage.exclusions="**Tests*.cs"
dotnet build
dotnet sonarscanner end

you can set the sonarscanner properties like report location and the URL etc., using /d: