feat(ci): create kaniko template to build docker

This commit is contained in:
bmartins 2023-03-23 12:05:12 +01:00
parent 2bf832a8eb
commit 061e210404
2 changed files with 72 additions and 11 deletions

View File

@ -1,5 +1,7 @@
# image: weboaks/node-karma-protractor-chrome # image: weboaks/node-karma-protractor-chrome
image: node:16 # image: node:16
include:
- "kaniko-build.gitlab-ci.yml"
stages: stages:
- build - build
@ -7,10 +9,10 @@ stages:
# - test # - test
# - e2e # - e2e
cache: # cache:
paths: # paths:
- node_modules/ # - node_modules/
- .yarn # - .yarn
#pages: #pages:
# stage: pages # stage: pages
@ -37,14 +39,14 @@ cache:
# cache: # cache:
# policy: pull # policy: pull
build-docker: build-image:
stage: build stage: build
tags: tags:
- docker - docker
before_script: extends: .kaniko_build
- docker info variables:
script: REGISTRY_IMAGE_PATH: "${HARBOR_HOST}/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}"
- docker build -t framadate . PUSH_IMAGE: "true"
build: build:
stage: build stage: build
@ -54,7 +56,6 @@ build:
- yarn run build:prod - yarn run build:prod
cache: cache:
policy: pull policy: pull
#e2e: #e2e:
# stage: e2e # stage: e2e
# script: # script:

View File

@ -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}"