从多阶段 Docker 构建(.NET Core 2.0)中提取单元测试结果 [英] Extract unit test results from multi-stage Docker build (.NET Core 2.0)
问题描述
我正在构建一个 .NET Core 2.0 Web API 并且我正在创建一个 Docker 映像.我对 Docker 很陌生,如果之前已经回答过这个问题,我深表歉意.
I am building a .NET Core 2.0 web API and I am creating a Docker image. I am quite new to Docker so apologies if the question has been answered before.
我有以下用于创建映像的 Docker 文件.特别是,我在构建过程中运行单元测试并将结果输出到 ./test/test_results.xml
(我猜是在构建期间创建的临时容器中).我的问题是,构建完成后如何访问这些测试结果?
I have the following Docker file for creating the image. In particular, I run the unit tests during the build process and the results are output to ./test/test_results.xml
(in a temporary container created during the build, I guess). My question is, how do I access these test results after the build has finished?
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
# Copy main csproj file for DataService
COPY src/DataService.csproj ./src/
RUN dotnet restore ./src/DataService.csproj
# Copy test csproj file for DataService
COPY test/DataService.Tests.csproj ./test/
RUN dotnet restore ./test/DataService.Tests.csproj
# Copy everything else (excluding elements in dockerignore)
COPY . ./
# Run the unit tests
RUN dotnet test --results-directory ./ --logger "trx;LogFileName=test_results.xml" ./test/DataService.Tests.csproj
# Publish the app to the out directory
RUN dotnet publish ./src/DataService.csproj -c Release -o out
# Build the runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
EXPOSE 5001
COPY --from=build-env /app/src/out .
# Copy test results to the final image as well??
# COPY --from=build-env /app/test/test_results.xml .
ENTRYPOINT ["dotnet", "DataService.dll"]
我采用的一种方法是在 # COPY --from=build-env/app/test/test_results.xml .
行中添加注释.这会将 test_results.xml
放入我的最终图像中.然后我可以提取这些结果并使用以下 powershell 脚本从最终图像中删除 test_results.xml
.
One approach that I have taken is to comment in the line # COPY --from=build-env /app/test/test_results.xml .
. This puts the test_results.xml
in my final image. I can then extract these results and remove the test_results.xml
from the final image using the following powershell script.
$id=$(docker create dataservice)
docker cp ${id}:app/test_results.xml ./test/test_results.xml
docker start $id
docker exec $id rm -rf /app/test_results.xml
docker commit $id dataservice
docker rm -vf $id
但这看起来很丑,我想知道有没有更清洁的方法.
This however seems ugly and I am wondering is there a cleaner way to do it.
我希望有一种方法可以在 docker build
期间挂载卷,但官方 Docker 似乎不支持这种方法.
I was hoping that there was a way to mount a volume during docker build
but it does not appear that this is going to be supported in the official Docker.
我现在正在考虑创建一个单独的图像,仅用于单元测试.
I am looking now at creating a separate image, solely for the unit tests.
不确定是否有推荐的方法来实现我想要的.
Not sure if there is a recommended way of achieving what I want.
推荐答案
感谢您的问题 - 我需要解决同样的问题.
Thanks for your question - I needed to solve the same thing.
我根据构建结果添加了一个单独的容器阶段.测试及其输出都在那里处理,因此它们永远不会到达最终容器.因此 build-env 用于构建,然后中间测试容器基于该 build-env 映像,最终基于运行时容器,并复制了 build-env 的结果.
I added a separate container stage based on results of the build. The tests and its output are all handled in there so they never reach the final container. So build-env is used to build and then an intermediate test container is based on that build-env image and final is based on runtime container with results of build-env copied in.
# ---- Test ----
# run tests and capture results for later use. This use the results of the build stage
FROM build AS test
#Use label so we can later obtain this container from the multi-stage build
LABEL test=true
WORKDIR /
#Store test results in a file that we will later extract
RUN dotnet test --results-directory ../../TestResults/ --logger "trx;LogFileName=test_results.xml" "./src/ProjectNameTests/ProjectNameTests.csproj"
我添加了一个 shell 脚本作为下一步,然后将图像标记为 project-test.
I added a shell script as a next step that then tags the image as project-test.
#!bin/bash
id=`docker images --filter "label=test=true" -q`
docker tag $id projectname-test:latest
在那之后,我基本上会做你所做的,即使用 docker cp 并将文件取出.不同之处在于我的测试结果从未出现在最终图像中,因此我不会触摸最终图像.
After that, I basically do what you do which is use docker cp and get the file out. The difference is my test results were never in the final image so I don't touch the final image.
总的来说,我认为处理测试的正确方法可能是创建一个测试映像(基于构建映像)并使用已安装的卷运行它以获得测试结果,并让它在该容器启动时运行单元测试.拥有适当的映像/容器还可以让您运行集成测试等.这篇文章较旧,但细节类似 https://blogs.infosupport.com/build-deploy-test-aspnetcore-docker-linux-tfs2015/
Overall I think the correct way to handle tests is probably create a test image (based on the build image) and run it with a mounted volume for test results and have it run the unit tests when that container starts. Having a proper image/container would also allow you to run integration tests etc. This article is older but details similar https://blogs.infosupport.com/build-deploy-test-aspnetcore-docker-linux-tfs2015/
这篇关于从多阶段 Docker 构建(.NET Core 2.0)中提取单元测试结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!