Multiple gitlab-ci stages with multistage dockerfile

Viewed 2204

I have a Dockerfile with two stages; the first stage builds a react app from source and the second stage copies that build and adds an NGINX server:

FROM kkarczmarczyk/node-yarn as builder
COPY . /workspace
RUN set -ex \
    yarn global add create-react-app \
    && yarn install \
    && GENERATE_SOURCEMAP=false yarn build

FROM nginx:mainline-alpine
COPY ./nginx-default.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /workspace/build /var/www/html/

I'm also using gitlab-ci, and I'd like to have multiple stages: build, test and deploy. But I don't know how to split the build and test stage since I'm using a multistage Dockerfile. The issue there is that all the JS tests (for React) need to be run before "yarn build" gets executed and the build gets copied over to the seconds docker stage.

A solution could be to just execute yarn test before yarn build gets run. But I really would like to have a separate test gitlab-ci stage.

Is this possible? Or do I need to split my Dockerfile stages into multiple Dockerfiles?

2 Answers

The reason you might want multiple stages in gitlab is fail-fast. But building with multi-stage docker or gitlab stages can give you the same result, if test fails you won't get further and will get the stacktrace. From one side docker multi-stage could be even more handy because you always can call the previous container and reproduce/test/fix the error. But overall it's just unnecessary complication to have docker-builder pattern inside of CI, it's better to separate docker images and use them by gitlab stage.

Related