2019-12-26 18:23:25 +03:00
# Multi-stage Docker build, test and package
# Based on https://github.com/dotnet/dotnet-docker/blob/master/samples/dotnetapp/dotnet-docker-unit-testing.md
2021-11-17 13:59:33 +03:00
ARG DOTNET_VERSION = 6 .0
2019-12-26 18:23:25 +03:00
2020-01-19 11:27:46 +03:00
# STAGE: Base build and test
2021-11-08 13:04:17 +03:00
FROM mcr.microsoft.com/dotnet/sdk:$DOTNET_VERSION AS build
2019-10-28 12:49:16 +03:00
WORKDIR /app
2020-01-21 18:32:14 +03:00
ARG VersionPrefix
2019-10-28 12:49:16 +03:00
2019-12-26 18:23:25 +03:00
# Copy csproj and restore as distinct layers. This caches a Docker layer with downloaded
# dependencies, making the next build faster if only code files have been changed.
2019-10-28 12:49:16 +03:00
COPY K2Bridge/*.csproj ./
RUN dotnet restore
# Copy everything else and build
2019-12-26 18:23:25 +03:00
COPY . ./
2020-01-21 18:32:14 +03:00
RUN dotnet publish K2Bridge -c Release -o out /p:VersionPrefix= ${ VersionPrefix }
2019-12-26 18:23:25 +03:00
2020-01-19 11:27:46 +03:00
# Test for lint issues, UnitTests project causes the main to build too
RUN dotnet build K2Bridge.Tests.UnitTests -p:TreatWarningsAsErrors= true -warnaserror
2020-01-14 14:58:39 +03:00
# Run unit tests. "exit 0" prevents failing build on test failures.
# (dotnet test returns a non-zero exit code on test failures, which would stop the docker build otherwise)
2020-01-19 11:27:46 +03:00
RUN dotnet test K2Bridge.Tests.UnitTests --no-build --logger "trx;LogFileName=/app/TestResult.xml" /p:CollectCoverage= true /p:CoverletOutputFormat= cobertura ; exit 0
2020-01-14 14:58:39 +03:00
# Fail build if no test result file produced (indicates an issue with the test runner itself)
RUN test -s /app/TestResult.xml
2019-10-28 12:49:16 +03:00
2020-01-12 12:44:21 +03:00
# Build end2end tests
2020-01-21 18:32:14 +03:00
RUN dotnet build K2Bridge.Tests.End2End /p:TreatWarningsAsErrors= true -warnaserror /p:VersionPrefix= ${ VersionPrefix }
2020-01-12 12:44:21 +03:00
2020-01-19 11:27:46 +03:00
# STAGE: Build image for executing End2End tests in Kubernetes
2021-11-08 13:04:17 +03:00
FROM mcr.microsoft.com/dotnet/sdk:$DOTNET_VERSION AS end2endtest
2020-01-12 12:44:21 +03:00
COPY --from= build /app/K2Bridge ./K2Bridge
COPY --from= build /app/K2Bridge.Tests.End2End ./K2Bridge.Tests.End2End
# Create a FIFO pipe allowing to fetch test outputs before container terminates
RUN mkfifo /test-result-pipe
# Run the created image to execute End2End tests
CMD [ "bash" , "-x" , "-c" , "dotnet test K2Bridge.Tests.End2End '--logger:trx;LogFileName=/app/TestResult.xml' ; cat /app/TestResult.xml > /test-result-pipe ; sleep 5" ]
2019-12-26 18:23:25 +03:00
2020-01-19 11:27:46 +03:00
# STAGE: Build runtime image
2021-11-08 13:04:17 +03:00
FROM mcr.microsoft.com/dotnet/aspnet:$DOTNET_VERSION AS runtime
2019-10-28 12:49:16 +03:00
WORKDIR /app
2019-12-26 18:23:25 +03:00
COPY --from= build /app/out .
2019-10-28 12:49:16 +03:00
ENTRYPOINT [ "dotnet" , "K2Bridge.dll" ]