简体   繁体   English

如何从 docker 容器访问 gradle 测试报告

[英]How to access gradle test reports from a docker container

I'm using a docker container to build and run my java application and I want to see the test results that would usually be available from build/reports/tests/test/index.html .我正在使用 docker 容器来构建和运行我的 java 应用程序,我想查看通常可以从build/reports/tests/test/index.html获得的测试结果。

Here's my Dockerfile :这是我的Dockerfile

FROM alpine:latest

RUN apk add gradle openjdk17

WORKDIR /home/proj
COPY . .

RUN gradle build

ENTRYPOINT [ "java", "-jar", "./app/build/libs/app.jar" ]

Here's my docker-compose.yml :这是我的docker-compose.yml

version: "3.8"
services:
  app:
    build: .

I typically build my container with docker-compose build and run it with docker-compose up and would like this to stay as it is.我通常使用docker-compose build构建我的容器并使用docker-compose up运行它,并希望它保持原样。

EDIT编辑

I've tried changing my docker-compose.yml to:我尝试将我的docker-compose.yml更改为:

version: "3.8"

services:
  app:
    build: .
    volumes:
      - ./tests:/home/proj/app/build/reports/tests/test

But this just creates an empty directory called tests in my project's root directory.但这只是在我的项目的根目录中创建了一个名为tests的空目录。 I am 100% sure the right path is /home/proj/app/build/reports/tests/test as you can see here:我 100% 确定正确的路径是/home/proj/app/build/reports/tests/test ,正如您在此处看到的:

小路

I've hit this same issue.我遇到了同样的问题。 Unfortunately volumes are only availible to docker run, they aren't part of the docker build portion of the docker compose process.不幸的是,卷仅适用于 docker 运行,它们不是 docker 组合过程的 docker 构建部分的一部分。

I even looked into the new RUN --mount=type=bind,source=.,target=./build/reports,rw gradle build and while in the container I can see (with a RUN ls./build/reports) the reports being generated, but that mount (even as RW) only puts files in the container as a layer and they never appear on the host machine.我什至查看了新的RUN --mount=type=bind,source=.,target=./build/reports,rw gradle build ,在容器中我可以看到(使用 RUN ls./build/reports)正在生成报告,但该挂载(即使是 RW)仅将文件作为层放入容器中,并且它们永远不会出现在主机上。

There is a hacky way to recover those test results, in the docker output, just above the failure you'll see this line ---> Running in f98e14dd1ee4 .在 docker output 中,有一种破解方法可以恢复这些测试结果,就在故障上方,您会看到这一行---> Running in f98e14dd1ee4 This is the layer ID, with that value you can copy from the failed layer to the local machine using这是层 ID,您可以使用该值从故障层复制到本地计算机

$ docker cp f98e14dd1ee4:/tmp/build/reports ./

$ ls reports/
checkstyle/ jacoco/     tests/

It shouldn't be to difficult to automate this kind of recover but it feels like it would be fragile automated.自动化这种恢复应该不难,但感觉自动化会很脆弱。

I'm very interested if anyone has a better solution to this, even with an alternative to docker.如果有人对此有更好的解决方案,我很感兴趣,即使是 docker 的替代品。 I know I can build the container with gradle but I'd rather everything happens inside the container build process to keep the build environment defined as code.我知道我可以使用 gradle 构建容器,但我宁愿一切都发生在容器构建过程中,以将构建环境定义为代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM