diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 8eac27a6bd0aee07020838a89f60fb282be469e0..f2f1cad716d04b61cb3e2d4977bc81b91a0743fb 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,3 +1,6 @@
+include:
+  - .gitlab/ci_templates/kaniko.yml
+
 stages:
   - build
   - setup-cluster
@@ -9,20 +12,15 @@ stages:
 image: "${CI_REGISTRY_IMAGE}/openappstack-ci:${CI_COMMIT_REF_NAME}"
 
 ci_test_image:
-  stage: build
-  image:
-    # We need a shell to provide the registry credentials, so we need to use the
-    # kaniko debug image (https://github.com/GoogleContainerTools/kaniko#debug-image)
-    name: gcr.io/kaniko-project/executor:debug
-    entrypoint: [""]
-  script:
-    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
-    - /kaniko/executor --context ${CI_PROJECT_DIR}/test --dockerfile ${CI_PROJECT_DIR}/test/Dockerfile --destination $CI_REGISTRY_IMAGE/openappstack-ci:${CI_COMMIT_REF_NAME}
+  variables:
+    KANIKO_CONTEXT: "test"
+    KANIKO_IMAGENAME: "openappstack-ci"
   only:
     changes:
       - .gitlab-ci.yml
       - test/Dockerfile
       - test/requirements.txt
+  extends: .kaniko_build
 
 bootstrap:
   stage: setup-cluster
diff --git a/.gitlab/ci_templates/kaniko.yml b/.gitlab/ci_templates/kaniko.yml
new file mode 100644
index 0000000000000000000000000000000000000000..e0b91954624b47a5a1e06b94974f9c10b0b2161a
--- /dev/null
+++ b/.gitlab/ci_templates/kaniko.yml
@@ -0,0 +1,18 @@
+# Optional environment variables:
+# - KANIKO_IMAGENAME
+# - KANIKO_CONTEXT: The subdir which holds the Dockerfile, leave unset if
+#                   the Dockerfile is located at root level of the project.
+.kaniko_build:
+  stage: build
+  image:
+    # We need a shell to provide the registry credentials, so we need to use the
+    # kaniko debug image (https://github.com/GoogleContainerTools/kaniko#debug-image)
+    name: gcr.io/kaniko-project/executor:debug
+    entrypoint: [""]
+  script:
+    - echo "CI_REGISTRY: $CI_REGISTRY"
+    - echo "CI_REGISTRY_USER: $CI_REGISTRY_USER"
+    - echo "KANIKO_CONTEXT: $KANIKO_CONTEXT"
+    - echo "KANIKO_IMAGENAME: $KANIKO_IMAGENAME"
+    - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
+    - /kaniko/executor --context ${CI_PROJECT_DIR}/${KANIKO_CONTEXT:-.} --dockerfile ${CI_PROJECT_DIR}/${KANIKO_CONTEXT:-.}/Dockerfile --destination $CI_REGISTRY_IMAGE/${KANIKO_IMAGENAME/#//}:${CI_COMMIT_REF_NAME}