如何在多阶段构建中提取覆盖率报告?

How to extract coverage report in multistage build?

我想在多阶段构建中构建 docker 图像时提取覆盖率报告。在我使用 Jenkins Docker 插件通过 image.inside 执行测试之前,但现在我正在使用以下命令执行测试,但我无法提取覆盖率报告。

docker build -t myapp:test --cache-from registry/myapp:test --target test --build-arg BUILDKIT_INLINE_CACHE=1 .

有没有什么方法可以像下面的函数一样在没有 运行 docker 图像的情况下安装 Jenkins 工作区?有一个 --output 标志,但我不明白如果它有效我该如何使用它。或者可以通过 RUN --mount=type ...

  image.inside('-u root -v $WORKSPACE/coverage:/var/app/coverage') {
      stage("Running Tests") {
        timeout(10) {
          withEnv(["NODE_ENV=production"]) {
            sh(script: "cd /var/app; yarn run test:ci")
        }

Docker文件

FROM node:16.15.0-alpine3.15 as base
WORKDIR /var/app
RUN --mount=type=cache,target=/var/cache/apk \
    apk add --update --virtual build-dependencies build-base \
    curl \
    python3 \
    make \
    g++ \
    bash 
COPY package*.json ./
COPY yarn.lock ./
COPY .solidarity ./
RUN --mount=type=cache,target=/root/.yarn YARN_CACHE_FOLDER=/root/.yarn && \
    yarn install --no-progress --frozen-lockfile --check-files && \
    yarn cache clean
COPY . .

FROM base as test
ENV NODE_ENV=production
RUN ["yarn", "run", "format:ci"]
RUN ["yarn", "run", "lint:ci"]
RUN ["yarn", "run", "test:ci"]

FROM base as builder
RUN yarn build

FROM node:16.15.0-alpine3.15 as production
WORKDIR /var/app
COPY  --from=builder /var/app /var/app
CMD ["yarn", "start:envconsul"]

你可以用你想要提取的输出制作一个舞台:

FROM node:16.15.0-alpine3.15 as base
WORKDIR /var/app
RUN --mount=type=cache,target=/var/cache/apk \
    apk add --update --virtual build-dependencies build-base \
    curl \
    python3 \
    make \
    g++ \
    bash 
COPY package*.json ./
COPY yarn.lock ./
COPY .solidarity ./
RUN --mount=type=cache,target=/root/.yarn YARN_CACHE_FOLDER=/root/.yarn && \
    yarn install --no-progress --frozen-lockfile --check-files && \
    yarn cache clean
COPY . .

FROM base as test
ENV NODE_ENV=production
RUN ["yarn", "run", "format:ci"]
RUN ["yarn", "run", "lint:ci"]
RUN ["yarn", "run", "test:ci"]

FROM scratch as test-out
COPY --from=test /var/app/coverage/ /

FROM base as builder
RUN yarn build

FROM node:16.15.0-alpine3.15 as production
WORKDIR /var/app
COPY  --from=builder /var/app /var/app
CMD ["yarn", "start:envconsul"]

然后你可以构建:

docker build \
  --output "type=local,dest=${WORKSPACE}/coverage" \
  --target test-out .