From the gitlab documentation this is how to create a docker image using kaniko:
build:stage: buildimage:name: gcr.io/kaniko-project/executor:debugentrypoint: [""]script:- mkdir -p /kaniko/.docker- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_TAGonly:- tags
but I want to run the test first(pytest) before pushing it to the container registry. Any help is greatly appreciated. Thanks!
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: buildimage:name: gcr.io/kaniko-project/executor:debugentrypoint: [""]before_script:- mkdir -p /kaniko/.docker- >-echo "{\"auths...}" > /kaniko/.docker/config.jsonscript:- >-/kaniko/executor--context $KANIKO_BUILD_CONTEXT--dockerfile $DOCKERFILE_PATH--destination $IMAGE_TAG--target image
Dockerfile
FROM ubuntu as imageRUN apt update -y && \apt upgrade -yRUN apt install -y gitFROM 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.