Skip to content

Instantly share code, notes, and snippets.

@karlospn
Created April 3, 2023 19:58
Show Gist options
  • Save karlospn/42719369968b32b5c9bfad7a6a7dd062 to your computer and use it in GitHub Desktop.
Save karlospn/42719369968b32b5c9bfad7a6a7dd062 to your computer and use it in GitHub Desktop.
A Dockerfile using sonarscanner with Pull Request data
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build-env
WORKDIR /app
# Agument for PR
ARG IS_PR
# Argument for the PAT
ARG AZDO_PAT
# Arguments for setting the Sonarqube Token and the Project Key
ARG SONAR_TOKEN
ARG SONAR_BRANCH_NAME
ARG SONAR_PRJ_KEY
ARG SONAR_PR_KEY
ARG SONAR_PR_INSTANCE
ARG SONAR_PR_PROJECT
ARG SONAR_PR_REPOSITORY
ARG SONAR_PR_BRANCH
ARG SONAR_PR_BASE
# Setting the Sonarqube Organization and Uri
ENV SONAR_ORG "karlospn"
ENV SONAR_HOST "https://sonarcloud.io/"
## Install Java, because the sonarscanner needs it.
RUN mkdir /usr/share/man/man1/
RUN apt-get update && apt-get dist-upgrade -y && apt-get install -y openjdk-11-jre
## Install sonarscanner
RUN dotnet tool install --global dotnet-sonarscanner --version 5.3.1
## Install report generator
RUN dotnet tool install --global dotnet-reportgenerator-globaltool --version 4.8.12
## Set the dotnet tools folder in the PATH env variable
ENV PATH="${PATH}:/root/.dotnet/tools"
# Start scanner
RUN if [ "$IS_PR" = "true" ] ; then \
dotnet sonarscanner begin \
/o:"$SONAR_ORG" \
/k:"$SONAR_PRJ_KEY" \
/d:sonar.host.url="$SONAR_HOST" \
/d:sonar.login="$SONAR_TOKEN" \
/d:sonar.coverageReportPaths="coverage/SonarQube.xml" \
/d:sonar.pullrequest.provider="vsts" \
/d:sonar.pullrequest.key="$SONAR_PR_KEY" \
/d:sonar.pullrequest.vsts.instanceUrl="$SONAR_PR_INSTANCE" \
/d:sonar.pullrequest.vsts.project="$SONAR_PR_PROJECT" \
/d:sonar.pullrequest.vsts.repository="$SONAR_PR_REPOSITORY" \
/d:sonar.pullrequest.base="$SONAR_PR_BASE" \
/d:sonar.pullrequest.branch="$SONAR_PR_BRANCH" \
/d:sonar.verbose=true \
/d:sonar.qualitygate.wait=true; \
else \
dotnet sonarscanner begin \
/o:"$SONAR_ORG" \
/k:"$SONAR_PRJ_KEY" \
/d:sonar.host.url="$SONAR_HOST" \
/d:sonar.login="$SONAR_TOKEN" \
/d:sonar.coverageReportPaths="coverage/SonarQube.xml" \
/d:sonar.branch.name="$SONAR_BRANCH_NAME" \
/d:sonar.qualitygate.wait=true; \
fi
# Copy everything and restore sln
COPY . ./
RUN dotnet restore -s "https://api.nuget.org/v3/index.json" \
--runtime linux-x64
# Build
RUN dotnet build "./src/CompanyPrefix.ApplicationName.WebApi/CompanyPrefix.ApplicationName.WebApi.csproj" \
-c Release \
--runtime linux-x64 \
--no-restore \
/p:PublishSingleFile=true
# Test
RUN dotnet test "./test/CompanyPrefix.ApplicationName.Library.Impl.UnitTest/CompanyPrefix.ApplicationName.Library.Impl.UnitTest.csproj" \
--collect:"XPlat Code Coverage" \
--results-directory ./coverage
# Create the code coverage file in sonarqube format using the cobertura file generated from the dotnet test command
RUN reportgenerator "-reports:./coverage/*/coverage.cobertura.xml" "-targetdir:coverage" "-reporttypes:SonarQube"
# Publish the app
RUN dotnet publish "./src/CompanyPrefix.ApplicationName.WebApi/CompanyPrefix.ApplicationName.WebApi.csproj" \
-c Release \
-o /app/publish \
--no-restore \
--no-build \
--self-contained true \
--runtime linux-x64 \
/p:PublishSingleFile=true \
/p:PublishTrimmed=true
## Stop scanner
RUN dotnet sonarscanner end /d:sonar.login="$SONAR_TOKEN"
# Build runtime image
mcr.microsoft.com/dotnet/runtime-deps:5.0-buster-slim
WORKDIR /app
COPY --from=build-env /app/publish .
ENTRYPOINT ["./CompanyPrefix.ApplicationName.WebApi"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment