Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make docker build fail if tests fail

Dockerfile

FROM node:carbon

WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install
RUN npm install gulp -g

COPY . .

run gulp build --build
run npm test

EXPOSE 80
CMD [ "npm", "start" ]

Tests are ran using mocha --recursive

build.sh

docker build -t my-app .
echo $?

How can I detect that one mocha test fails, thus npm test should not be ok, and neither docker build?

I may have missed something in here.

like image 608
Francois Avatar asked Dec 22 '17 21:12

Francois


2 Answers

RUN in a Dockerfile will fail if the exit code of the command is non-zero. If that happens, docker build will also fail with a non-zero exit code.

Your npm test script needs to return a non-zero exit code when the tests fail.

For reference, you can check the exit code like this:

$ npm test
$ echo $?
like image 160
mkasberg Avatar answered Sep 19 '22 00:09

mkasberg


You may change line run npm test to run npm test || exit 1

Ran a quick test and confirmed build is failing. Here is a screen shot.

enter image description here

like image 26
Robert Ranjan Avatar answered Sep 20 '22 00:09

Robert Ranjan