I assume you want to run the tests inside the Docker container you are building the image for.
The best solution I came up with so far is
- add the tests as another stage in a multi-stage
Dockerfile
- in your
test-image
job, run Kaniko without pushing the image at the end (this will run your tests during the build of the image)
- in the
build-image
job, run Kaniko with pushing the image and specify the stage/layer of the image you want to push using the --target
directive
Here is an example:
.gitlab-ci.yml
build:
stage: build
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
before_script:
- mkdir -p /kaniko/.docker
- >-
echo "{\"auths...}" > /kaniko/.docker/config.json
script:
- >-
/kaniko/executor
--context $KANIKO_BUILD_CONTEXT
--dockerfile $DOCKERFILE_PATH
--destination $IMAGE_TAG
--target image
Dockerfile
FROM ubuntu as image
RUN apt update -y && \
apt upgrade -y
RUN apt install -y git
FROM devimage as test
# smoke test to see whether git was installed as expected
RUN git --version
# you can add further tests here...
This will run the tests in a second stage within the Docker build. This would be the place where you can also install test frameworks and other test-only resources that shouldn't make it into the image pushed to the container registry.
Kaniko won't push the image, if the tests fail.
test
job before the build, which would use the correct python image, install and run pytest, and even optionally store the coverage. Some good examples in this blog post or the official doc. If the tests fail, thebuild
stage won't be run and your image not created. – Tremann