I am working on implementing a solution for a sonar scanner inside a docker container for the .net core application in azure DevOps.
Currently, the sonarqube installed was on a sonarqube namespace in the AKS cluster and our dot net application is containerized in a different namespace in the same cluster. I’m struggling with the following error: The temporary analysis directory (usually .sonarqube) doesn’t exist. The "begin" step was probably not executed. Is there any sample or working Docker file which implements this type of logic? I tried to follow the steps described in this blog post but the result is the same.
Here is my Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
RUN apt-get update && apt-get install -y openjdk-11-jdk
RUN dotnet tool install --global dotnet-sonarscanner
RUN dotnet tool install --global dotnet-reportgenerator-globaltool
RUN dotnet tool install --global coverlet.console
## Set the dotnet tools folder in the PATH env variable
ENV PATH="$PATH:/root/.dotnet/tools"
## Start scanner
RUN dotnet sonarscanner begin
/k:"my-token"
/o:"my-org"
/d:sonar.host.url="sonar-host"
/d:sonar.login="sonar-token"
/d:sonar.cs.opencover.reportsPaths=/coverage.opencover.xml
COPY my-project/*.csproj ./my-project/
COPY . .
WORKDIR /src/my-project
RUN dotnet restore
RUN dotnet build -c Release -o /app/build --no-restore
RUN dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:CoverletOutput="/coverage"
## Stop scanner
RUN dotnet sonarscanner end /d:sonar.login="sonar-token"
FROM build AS publish
WORKDIR /src/my-project
RUN dotnet publish -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "my-project.dll"]
2
Answers
I believe your issue may be running
dotnet restore
after thesonarscanner begin
. If you re-arrange those steps, you should be OK.Found this: https://community.sonarsource.com/t/sonarqube-is-not-showing-msbuild-analysis/44455/7
This is what did the trick for me:
Be careful with the WORKDIR statements in your Dockerfile and ensure all those 3 commands are executed within the same directory.