Recently I had the opportunity to build a CI pipeline for various microservices using GitLab. The issue comes when it’s time to run the unit test, as some of the projects does not use a mocker and would want to connect to a test DB. Thankfully there is a way for us to generate a temporary database for testing purposes.

In this scenario, I will be using GitLab dindDocker-In-Docker (dind) to build, test and deploy the image.

Let’s start with an example Dockerfile, for this I will assume we are using a simple NodeJs project

FROM node:14-alpine AS node

## Builder stage
FROM node AS builder
ENV APP_PORT 8080
WORKDIR /app
COPY . .
## Copy .env configuration
COPY .env.test ./.env
## Install dependencies
RUN npm ci
## Port public untuk akses
EXPOSE 8080
## Run development server
CMD [ "npm", "run", "dev" ]

#unit-testing #docker #ci-cd-pipeline #gitlab

Using A Database for Unit Testing With Docker in GitLab Pipeline
1.40 GEEK