0

I want to extract the coverage report while building a docker image in a multistage build. Before I was executing the tests via image.inside using the Jenkins Docker plugin but now I am executing the tests using the following command where I could not extract the coverage report.

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

Is there any way to mount the Jenkins workspace like the below function is doing without running the docker image? There is a --output flag but I could not understand how can I use this if it works. Or can it be possible via 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")
        }

Dockerfile

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"]
semural
  • 3,583
  • 8
  • 37
  • 67
  • Is there a file or files you are trying to extract (I'm looking for the directory and full filenames)? Where would you like to place them on the host? – BMitch Jun 04 '22 at 10:55
  • @BMitch, coverage reports generated under `/var/app/coverage`.. Somehow I need to mount `$WORKSPACE/coverage`. – semural Jun 04 '22 at 10:57

1 Answers1

4

You can make a stage with the output you want to extract:

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"]

Then you can build with:

docker build \
  --output "type=local,dest=${WORKSPACE}/coverage" \
  --target test-out .
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • Do I have to build the test stage separately to execute the tests? I mean to execute the tests I have to build a test stage as well like `docker build --target test .`, right? – semural Jun 04 '22 at 11:13
  • @semural since test is a dependency of test-out, there's no need to build it separately. Docker will do that automatically as part of creating test-out. – BMitch Jun 04 '22 at 11:15
  • can I use multiple outputs for different paths? E.g `--output type=local,dest=${WORKSPACE}/coverage --output type=local,dest=${WORKSPACE}/test-results` – semural Jun 04 '22 at 11:56
  • @semural I'm not sure, but there's a way you can find out. – BMitch Jun 04 '22 at 12:00