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"]
CodePudding user response:
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 .