General info about the project:
Using Bitbucket Cloud Pipeline
Languages of the repository: C# dotnet core 5 & 6
Hey there, I’m trying to integrate sonarcloud to our pipeline system on bitbucket cloud.
The project is dotnet core with C#. Our build step was in the dockerfile. Everything went well there. Here is summary of my dockerfile script:
# set all the ARG and ENVs, install required libs etc.
...
...
RUN dotnet sonarscanner begin \
/k:"$SONAR_PROJECT_KEY" \
/o:"$SONAR_OGRANIZAION_KEY" \
/d:sonar.host.url="$SONAR_HOST_URL" \
/d:sonar.login="$SONAR_TOKEN" \
/d:sonar.coverageReportPaths=/src/coverage/SonarQube.xml \
/d:sonar.qualitygate.wait=true
...
...
# end the sonarscanner step
P.S. I know in bitbucket cloud we are not supposed to use the /d:sonar.qualitygate.wait=true
and instead we should use the pipes. But they were slow, doing extra work and step, and this solution worked perfectly.
So far everything was fine.
But because we only need to run sonar scanner on PRs, there was no need to include it in the dockerfile.
We decided to move it out in the pipeline and build/test natively on the pipeline without docker.
So my script became:
pipelines:
pull-requests:
'**':
- step:
name: Sonarcloud scan
image: mcr.microsoft.com/dotnet/sdk:6.0
caches:
- dotnetcore
script:
- apt-get update && apt-get install -y openjdk-11-jre
- dotnet tool install --global dotnet-sonarscanner
- dotnet tool install --global dotnet-reportgenerator-globaltool
- export PATH="$PATH:/root/.dotnet/tools"
- cd src
- dotnet sonarscanner begin /k:"${SONAR_PROJECT_KEY}" /o:"${SONAR_OGRANIZAION_KEY}" /d:sonar.host.url="${SONAR_HOST_URL}" /d:sonar.login="${SONAR_TOKEN}" /d:sonar.coverageReportPaths=/src/coverage/SonarQube.xml /d:sonar.qualitygate.wait=true
- cp Nuget.config /root/.nuget/NuGet/NuGet.Config
- dotnet restore Api.csproj
- dotnet build --no-restore Api.csproj -c Release
- dotnet test Api.Test.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[xunit*]\*" /p:CoverletOutput="./TestResults/"
- dotnet test Api.Integration.Test.csproj /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[xunit*]\*" /p:CoverletOutput="./TestResults/"
- reportgenerator "-reports:*/TestResults/coverage.opencover.xml" "-targetdir:/src/coverage" "-reporttypes:SonarQube"
- dotnet sonarscanner end /d:sonar.login="${SONAR_TOKEN}"
Problems started as the following line didn’t trigger what it was expected to do: /d:sonar.qualitygate.wait=true
So I decided to follow your instructions from doc and added this at the end of my last line:
- pipe: sonarsource/sonarcloud-quality-gate:0.1.6
variables:
SONAR_TOKEN: $SONAR_TOKEN
It failed with the following error:
Quality Gate failed: Could not get scanner report: [Errno 2] No such file or directory: ‘/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes/sonarsource/sonarcloud-scan/sonarcloud-scan.log’
It was absurd at this point because for dotnet we can’t use the: sonarsource/sonarcloud-scan:1.3.0
But I added this pipe before the sonarcloud-quality-gate
one, and it worked.
Now my question is, how can I achieve the quality gate without using the pipes (because it is actually possible if I build in dockerfile).
And if for some reason the bitbucket VM environment can’t provide that, why do I need to run sonarcloud-scan
at all if I already run it at dotnet level.
What I want is to not use the sonarcloud-scan
or if possible the sonarcloud-quality-gate
too. What am I doing wrong here?