From 061e2104047ca15eb144cfbf3a6c83667462aeef Mon Sep 17 00:00:00 2001 From: bmartins Date: Thu, 23 Mar 2023 12:05:12 +0100 Subject: [PATCH] feat(ci): create kaniko template to build docker --- .gitlab-ci.yml | 23 ++++++++------- kaniko-build.gitlab-ci.yml | 60 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 kaniko-build.gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ee65837f..a69fbb96 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,7 @@ # image: weboaks/node-karma-protractor-chrome -image: node:16 +# image: node:16 +include: + - "kaniko-build.gitlab-ci.yml" stages: - build @@ -7,10 +9,10 @@ stages: # - test # - e2e -cache: - paths: - - node_modules/ - - .yarn +# cache: +# paths: +# - node_modules/ +# - .yarn #pages: # stage: pages @@ -37,14 +39,14 @@ cache: # cache: # policy: pull -build-docker: +build-image: stage: build tags: - docker - before_script: - - docker info - script: - - docker build -t framadate . + extends: .kaniko_build + variables: + REGISTRY_IMAGE_PATH: "${HARBOR_HOST}/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}" + PUSH_IMAGE: "true" build: stage: build @@ -54,7 +56,6 @@ build: - yarn run build:prod cache: policy: pull - #e2e: # stage: e2e # script: diff --git a/kaniko-build.gitlab-ci.yml b/kaniko-build.gitlab-ci.yml new file mode 100644 index 00000000..356cef21 --- /dev/null +++ b/kaniko-build.gitlab-ci.yml @@ -0,0 +1,60 @@ +# FROM https://gitlab.cern.ch/ci-tools/container-image-ci-templates/ + +# Defining the variables which can be overridden while using the template file in .gitlab-ci.yml +# REGISTRY_IMAGE_PATH: Defines where the image should be pushed to. +# CONTEXT_DIR: Defines the context dir for the image build +# DOCKER_FILE_NAME: Defines the name of the Dockerfile +# PUSH_IMAGE: If the gitlab step should push the built image +# BUILD_ARGS: The build-time variables + +variables: + CONTEXT_DIR: "" + DOCKER_FILE_NAME: "Dockerfile" + GIT_SUBMODULE_STRATEGY: recursive + PUSH_IMAGE: "false" + BUILD_ARGS: "" + +.kaniko_build: + stage: build + tags: + - docker + image: + # Using the official kaniko v1.6 image for the builds + name: gcr.io/kaniko-project/executor:v1.6.0-debug + entrypoint: [""] + script: + - | + if [ -z "${REGISTRY_IMAGE_PATH}" ]; then + echo "ERROR: CI variable REGISTRY_IMAGE_PATH is mandatory." + exit 1 + fi + - REGISTRY=$(echo ${REGISTRY_IMAGE_PATH} | cut -d / -f 1) + - > + if [ -z "${CONTEXT_DIR}" ]; then + KANIKO_CONTEXT_DIR=${CI_PROJECT_DIR} + else + KANIKO_CONTEXT_DIR=${CI_PROJECT_DIR}/${CONTEXT_DIR} + fi + - mkdir -p /kaniko/.docker + - | + if [ -z "$DOCKER_AUTH_CONFIG" ]; then + # build the DOCKER_AUTH_CONFIG from user parameters as per https://docs.gitlab.com/ee/ci/docker/using_kaniko.html and internal docs + echo "{\"auths\":{\"${HARBOR_HOST}\":{\"auth\":\"$(printf "%s:%s" "${HARBOR_USERNAME}" "${HARBOR_PASSWORD}" | base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json + else + echo "${DOCKER_AUTH_CONFIG}" > /kaniko/.docker/config.json + fi + # build and push the image to the path set in $REGISTRY_IMAGE_PATH variable. + - | + if [ "$(echo ${PUSH_IMAGE} | tr '[:upper:]' '[:lower:]')" = "true" ]; then + PUSH_IMAGE="" + else + echo "Info: defer pushing image to remote as PUSH_IMAGE is false" + PUSH_IMAGE="--no-push" + fi + - | + if [ -n "$BUILD_ARGS" ]; then + for arg in $BUILD_ARGS; do + KANIKO_BUILD_ARGS="${KANIKO_BUILD_ARGS} --build-arg ${arg}" + done + fi + - /kaniko/executor --context "${KANIKO_CONTEXT_DIR}" --dockerfile "${KANIKO_CONTEXT_DIR}/${DOCKER_FILE_NAME}" ${KANIKO_BUILD_ARGS} --destination "${REGISTRY_IMAGE_PATH}" "${PUSH_IMAGE}"