From d158b38fe23352a98f84fd5da9ba888c12eafcb7 Mon Sep 17 00:00:00 2001
From: Varac <varac@varac.net>
Date: Wed, 4 Sep 2019 18:51:32 +0200
Subject: [PATCH] Use kaniko build template

---
 .gitlab-ci.yml                  | 16 +++++++---------
 .gitlab/ci_templates/kaniko.yml | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+), 9 deletions(-)
 create mode 100644 .gitlab/ci_templates/kaniko.yml

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 8eac27a6b..f2f1cad71 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 000000000..e0b919546
--- /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}
-- 
GitLab