I would like to generate test coverage for Sonar, but there is something missing from my Dockerfile below. Can you see what I am missing?
First here is the Azure Devops YAML file:
# Charles Teague November 22, 2023
variables:
- name: DOCKER_BUILDKIT
value: 1
trigger:
branches:
include:
- refs/heads/QA
pool:
vmImage: 'ubuntu-latest'
steps:
- checkout: self
persistCredentials: True
- task: Docker@2
displayName: Build an image
inputs:
command: build
repository: [Redacted]
Dockerfile: Dockerfile
arguments: '--build-arg PROJECT=$(System.TeamProject) --build-arg REPOSITORY=$(Build.Repository.Name) --build-arg INSTANCEURL=$(System.CollectionUri) --build-arg PULLREQUEST_ID=$(System.PullRequest.PullRequestId) --build-arg TARGET_BRANCH_NAME=$(System.PullRequest.targetBranchName) --build-arg SOURCE_BRANCH_NAME=$(System.PullRequest.SourceBranch)'
Here is the Docker file
#######################################################
# Step 1: Build the application in a container #
#######################################################
# Download the official ASP.NET Core SDK image
# to build the project while creating the docker image
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
ARG SONAR_OGRANIZAION_KEY=[Redacted]
ARG SONAR_PROJECT_KEY=[Redacted]
ARG SONAR_HOST_URL=https://sonarcloud.io
ARG SONAR_TOKEN=[Redacted]
ARG PULLREQUEST_ID
ARG SOURCE_BRANCH_NAME
ARG TARGET_BRANCH_NAME
ARG PROJECT
ARG REPOSITORY
ARG INSTANCEURL
WORKDIR /app
RUN mkdir /app/out
RUN mkdir /app/out/coverage
RUN echo ${PROJECT}
RUN echo ${REPOSITORY}
RUN echo ${INSTANCEURL}
RUN echo ${PULLREQUEST_ID}
RUN echo ${SOURCE_BRANCH_NAME}
RUN echo ${TARGET_BRANCH_NAME}
RUN echo ${SONAR_PROJECT_KEY}
EXPOSE 80
# Install Sonar Scanner, Coverlet and Java (required for Sonar Scanner)
RUN apt-get update && apt-get install -y openjdk-17-jdk-headless
RUN dotnet tool install --global dotnet-sonarscanner
RUN dotnet tool install --global coverlet.console
ENV PATH="$PATH:/root/.dotnet/tools"
# Restore NuGet packages
COPY . .
RUN dotnet restore
# Start Sonar Scanner
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.cs.opencover.reportsPaths=/app/out/coverage/coverage.opencover.xml \
/d:sonar.pullrequest.key=${PULLREQUEST_ID} \
/d:sonar.pullrequest.branch=${SOURCE_BRANCH_NAME} \
/d:sonar.pullrequest.base=${TARGET_BRANCH_NAME} \
/d:sonar.pullrequest.vsts.project=${PROJECT} \
/d:sonar.pullrequest.vsts.repository=${REPOSITORY} \
/d:sonar.pullrequest.vsts.instanceUrl=${INSTANCEURL} \
/n:${PROJECT}
# Build and test the application
RUN dotnet publish --output /app/out/
RUN dotnet test --logger trx --no-build \
/p:CollectCoverage=true \
/p:CoverletOutputFormat=opencover \
/p:CoverletOutput=/app/out/coverage/ \
./layoutboards/layoutboards.csproj
RUN echo "$(ls /app/out/coverage)"
# End Sonar Scanner
RUN dotnet sonarscanner end /d:sonar.login="${SONAR_TOKEN}"
# Copy the rest of the files over
COPY . .
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build /app/out .
ENTRYPOINT ["dotnet", "./layoutboards.dll"]
- ALM used: Azure DevOps
- CI system used: Azure DevOps
- Scanner command used:
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.cs.opencover.reportsPaths=/app/out/coverage/coverage.opencover.xml \
/d:sonar.pullrequest.key=${PULLREQUEST_ID} \
/d:sonar.pullrequest.branch=${SOURCE_BRANCH_NAME} \
/d:sonar.pullrequest.base=${TARGET_BRANCH_NAME} \
/d:sonar.pullrequest.vsts.project=${PROJECT} \
/d:sonar.pullrequest.vsts.repository=${REPOSITORY} \
/d:sonar.pullrequest.vsts.instanceUrl=${INSTANCEURL} \
/n:${PROJECT}
- Languages of the repository: C#
- Error observed: The test results are not generated
- Steps to reproduce: Commit a change to the code, which automatically triggers the above YML
- Potential workaround: None Found