Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop/fail docker build if tests fail

My aim is to force the docker build to fail if the Unittests have not all passed. I'm using the following line in my DockerFile:

RUN python3 -m unittest discover test

My unit tests are in classes in the test directory. The tests are run, but a failed test does not result in a failed build.

I know that a non-zero exit code is how to stop a build, but that does not seem to occur using this command.

like image 528
C Murphy Avatar asked Oct 21 '19 10:10

C Murphy


1 Answers

Had the same problem : Fail the docker build if unittest fails in azure pipeline I put the below code in my build.yml file.

errors=$(docker logs careplan-tests-1 2>&1 | grep "FAILED" -c) if [ $errors > 0 ]; then echo -e "unittest tests failed" docker rm apiunittest exit 1 fi

Gives the output as "##[error]Bash exited with code '1'." in docker build and fails the azure pipeline.

like image 141
sam Avatar answered Oct 12 '22 00:10

sam