how to run test against the built image before pushing to containers registry?

2024/10/5 1:14:26

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!

Answer

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

  1. add the tests as another stage in a multi-stage Dockerfile
  2. 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)
  3. 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.

https://en.xdnf.cn/q/70541.html

Related Q&A

Adding a colorbar to a pcolormesh with polar projection

I am trying to add a colorbar to a pcolormesh plot with polar projection. The code works fine if I dont specify a polar projection. With polar projection specified, a tiny plot results, and the colorba…

GridSearch for Multi-label classification in Scikit-learn

I am trying to do GridSearch for best hyper-parameters in every individual one of ten folds cross validation, it worked fine with my previous multi-class classification work, but not the case this time…

Visualize tree in bash, like the output of unix tree

Given input:apple: banana eggplant banana: cantaloupe durian eggplant: fig:I would like to concatenate it into the format:├─ apple │ ├─ banana │ │ ├─ cantaloupe │ │ └─ durian │ └…

pygame.error: Failed loading libmpg123.dll: Attempt to access invalid address

music = pygame.mixer.music.load(not.mp3) pygame.mixer.music.play(loops=-1)when executing the code I got this error: Traceback (most recent call last):File "C:\Users\Admin\AppData\Local\Programs\Py…

Plot Red Channel from 3D Numpy Array

Suppose that we have an RGB image that we have converted it to a Numpy array with the following code:import numpy as np from PIL import Imageimg = Image.open(Peppers.tif) arr = np.array(img) # 256x256x…

How to remove image noise using opencv - python?

I am working with skin images, in recognition of skin blemishes, and due to the presence of noises, mainly by the presence of hairs, this work becomes more complicated.I have an image example in which …

Django groups and permissions

I would like to create 2 groups (Professors, Students). And I would like to restrict students from creating and deleting Courses.views.py:def is_professor(function=None):def _is_professor(u):if user.gr…

How to (properly) use external credentials in an AWS Lambda function?

I have a (extremely basic but perfectly working) AWS lambda function written in Python that however has embedded credentials to connect to: 1) an external web service 2) a DynamoDB table. What the fu…

How to set environment variable TF_Keras = 1 for onnx conversion?

Recently updated to tensorflow 2.0 and am having trouble getting my .h5 models into .onnx . Used to be a very simple procedure but now I am having an issue. When I run the following code:# onnx testing…

Django App Engine: AttributeError: AnonymousUser object has no attribute backend

I am using djangoappengine. When I try create a new user, authenticate that user, and log them in, I get the following error AttributeError: AnonymousUser object has no attribute backend.My code is sim…