From 33b798b4e32bf0d0a43fbdb17b0de9f7378766e5 Mon Sep 17 00:00:00 2001 From: Michiel Meeuwissen Date: Sat, 16 Nov 2024 13:14:21 +0100 Subject: [PATCH] Cleaning/testing scripts. --- Dockerfile | 6 +- Makefile | 3 + kaniko.sh | 29 +------- local-maven.sh | 6 ++ local-setup.sh | 25 +++++++ ...build-setup.sh => dockerfile-functions.sh} | 54 +++----------- .../{kaniko-gitlab.sh => kaniko-functions.sh} | 43 +++++------ scripts/kaniko-gitlab-functions.sh | 71 +++++++++++++++++++ scripts/kaniko-maven.sh | 38 +++++++--- scripts/script.sh | 14 ++-- 10 files changed, 168 insertions(+), 121 deletions(-) create mode 100755 local-maven.sh create mode 100644 local-setup.sh rename scripts/{docker-build-setup.sh => dockerfile-functions.sh} (50%) rename scripts/{kaniko-gitlab.sh => kaniko-functions.sh} (58%) create mode 100644 scripts/kaniko-gitlab-functions.sh diff --git a/Dockerfile b/Dockerfile index f4f5931..c2ec044 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,9 @@ LABEL maintainer=poms@mmprogrami.nl LABEL org.opencontainers.image.description='An extension of kaniko-project/executor that contains some script for deploying maven projects to CHP5 @ NPO' +ENV KANIKO_SCRIPTS=/ +COPY scripts/* $KANIKO_SCRIPTS -COPY scripts/* / - -RUN chmod +x /script.sh +RUN chmod +x ${KANIKO_SCRIPTS}script.sh ENTRYPOINT ["/script.sh"] \ No newline at end of file diff --git a/Makefile b/Makefile index c049bca..0ba3ae1 100644 --- a/Makefile +++ b/Makefile @@ -7,3 +7,6 @@ docker: test: docker run -it --entrypoint /bin/sh npo-poms/kaniko + +source: + export KANIKO_SCRIPTS=$(pwd)/scripts; \ No newline at end of file diff --git a/kaniko.sh b/kaniko.sh index e576ed3..3e53ebb 100755 --- a/kaniko.sh +++ b/kaniko.sh @@ -2,10 +2,7 @@ # This script calls kaniko (in docker) for the current directory. You can put it in your path # It's actually calling the scripts/script.sh in ghcr.io/npo-poms/kaniko -REGISTRY=${REGISTRY:-registry.npohosting.nl} -NAMESPACE=${NAMESPACE:-poms} - - +source $(dirname "${BASH_SOURCE[0]}")/local-setup.sh # REGISTRY: openshift-image-registry.apps.cluster.chp4.io. @@ -13,29 +10,6 @@ NAMESPACE=${NAMESPACE:-poms} #KANIKO_IMAGE=npo-poms/kaniko KANIKO_IMAGE=ghcr.io/npo-poms/kaniko:main -PROJECT_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) -echo "Found project version ${PROJECT_VERSION}" - -CI_COMMIT_REF_NAME="$(git symbolic-ref -q --short HEAD || git describe --tags --exact-match)" - - -if [ "$IMAGE_TAG" = "" ] ; then # you can specify the image-tag by: IMAGE_TAG=.. kaniko.sh - case $CI_COMMIT_REF_NAME in - "main") - IMAGE_TAG=dev - ;; - *SNAPSHOT*) - IMAGE_TAG=$(echo "${PROJECT_VERSION}-dev" | tr '[:upper:]' '[:lower:]') - ;; - *) - IMAGE_TAG=$(echo "${PROJECT_VERSION}" | tr '[:upper:]' '[:lower:]') - ;; - esac -fi - -echo "image tag $IMAGE_TAG" - - docker run -v ~/conf:/root/conf -v ~/.docker:/root/.docker -v "$(pwd)":/workspace \ -e PROJECT_VERSION="$PROJECT_VERSION" \ -e IMAGE_TAG=${IMAGE_TAG} \ @@ -47,3 +21,4 @@ docker run -v ~/conf:/root/conf -v ~/.docker:/root/.docker -v "$(pwd)":/workspac -e CI_COMMIT_TITLE="$(git show --format="%s" --no-patch)" \ $KANIKO_IMAGE + diff --git a/local-maven.sh b/local-maven.sh new file mode 100755 index 0000000..4786d4c --- /dev/null +++ b/local-maven.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +. $(dirname "${BASH_SOURCE[0]}")/local-setup.sh + +KANIKO_SCRIPTS=$(dirname ${BASH_SOURCE[0]})/scripts/ +. "$KANIKO_SCRIPTS"/script.sh \ No newline at end of file diff --git a/local-setup.sh b/local-setup.sh new file mode 100644 index 0000000..2ed31db --- /dev/null +++ b/local-setup.sh @@ -0,0 +1,25 @@ +# Sets up some environment as gitlab would do. + +REGISTRY=${REGISTRY:-registry.npohosting.nl} +NAMESPACE=${NAMESPACE:-poms} + +CI_COMMIT_REF_NAME="$(git symbolic-ref -q --short HEAD || git describe --tags --exact-match)" +PROJECT_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout) +echo "Found project version ${PROJECT_VERSION}" + + +if [ "$IMAGE_TAG" = "" ] ; then # you can specify the image-tag by: IMAGE_TAG=.. kaniko.sh + case $CI_COMMIT_REF_NAME in + "main") + IMAGE_TAG=dev + ;; + *SNAPSHOT*) + IMAGE_TAG=$(echo "${PROJECT_VERSION}-dev" | tr '[:upper:]' '[:lower:]') + ;; + *) + IMAGE_TAG=$(echo "${PROJECT_VERSION}" | tr '[:upper:]' '[:lower:]') + ;; + esac +fi + +echo "image tag $IMAGE_TAG" diff --git a/scripts/docker-build-setup.sh b/scripts/dockerfile-functions.sh similarity index 50% rename from scripts/docker-build-setup.sh rename to scripts/dockerfile-functions.sh index 91c94f6..b078165 100644 --- a/scripts/docker-build-setup.sh +++ b/scripts/dockerfile-functions.sh @@ -1,37 +1,10 @@ ##!/bin/sh echo "docker build setup $0" -if [ "$TRACE" = "true" ] ; then - echo "Tracing" - set -xv - env -fi - -echo "Define determine_image_version" - -# If store_image_version was called earlier in the pipeline, the the results of this are in job.env -determine_image_version() { - - # used by plain docker builds - if [ "$AS_LATEST" = 'true' ] ; then - export LATEST="--destination $REGISTRY/$IMAGE_NAME" - else - export LATEST= - fi - - if [ "$IMAGE_TAG" = '' ] ; then - echo "No IMAGE_TAG defined. Breaking build. This must be defined in job rule!" - exit 1 - fi - if [ "$IMAGE_NAME" = '' ] ; then - echo "No IMAGE_NAME defined. Taking from os_app_name" - IMAGE_NAME=$(os_app_name) - export IMAGE_NAME - fi - export IMAGE=$REGISTRY/$IMAGE_NAME:$IMAGE_TAG - echo "IMAGE: $IMAGE" -} +EMP='\033[0;36m' +NC='\033[0m' # No Color +# scripts around the 'os_app_name' function, that determin the artifact name using information from Dockerfil @@ -42,7 +15,8 @@ os_app_name() { DIR=$1 appname=$(awk -F= '$1 == "ARG NAME"{ print $2}' $DIR/Dockerfile) if [ -z "$appname" ] ; then - >&2 echo "Could not determine application name from Dockerfile (ARG NAME=). Getting from IMAGE_NAME=$IMAGE_NAME" + >&2 echo "Could not determine application name from $DIR/Dockerfile (ARG NAME=). Getting from IMAGE_NAME=$IMAGE_NAME" + appname=$IMAGE_NAME fi if [ -z "$appname" ] ; then @@ -71,15 +45,15 @@ get_artifact_versions() { exit_code=$? if [ $exit_code != '0' ] ; then echo "Error with os_app_name function $exit_code" - exit $exit_code + return $exit_code fi if [ -z "$IMAGE_TAG" ] ; then echo "No IMAGE_TAG defined. Should have been in rules" - exit 1 + return 1 fi if [ -z "$NAMESPACE" ] ; then echo "No docker NAMESPACE defined" - exit 1 + return 1 fi export IMAGE_NAME=$OS_APPLICATION export FULL_IMAGE_NAME=$NAMESPACE/$IMAGE_NAME:$IMAGE_TAG @@ -89,15 +63,3 @@ get_artifact_versions() { } -# Stores relevant variables determined by get_artifact_versions in job.env -# I'm not sure this is very useful. You can just as wel call get_articaft_versions again in the next job -# which will have the same effect, but I think this is robust, because no need for fiddling with 'need=', -# which is confusing and error-prone. -store_image_version() { - echo "IMAGE_TAG=$IMAGE_TAG" | tee job.env - echo "IMAGE=$IMAGE" | tee -a job.env - echo "IMAGE_NAME=$IMAGE_NAME" | tee -a job.env - echo "FULL_IMAGE_NAME=$FULL_IMAGE_NAME" | tee -a job.env - echo "PROJECT_VERSION=$PROJECT_VERSION" | tee -a job.env -} - diff --git a/scripts/kaniko-gitlab.sh b/scripts/kaniko-functions.sh similarity index 58% rename from scripts/kaniko-gitlab.sh rename to scripts/kaniko-functions.sh index beced6d..e0db59c 100644 --- a/scripts/kaniko-gitlab.sh +++ b/scripts/kaniko-functions.sh @@ -1,36 +1,21 @@ ##!/bin/sh -# This is the script can be used to build and push (via kaniko) an openshift statefull set. -# This script used to be present in gitlab templates, but that's unmaintainble and unreusable -# This can be used locally via run-in-docker.sh in a directory of interest - -. /docker-build-setup.sh - -echo "kaniko build setup"; -if [ "$TRACE" = "true" ] ; then - echo "Tracing" - set -xv - env +if ! type get_artifact_versions &> /dev/null; then + . "$KANIKO_SCRIPTS"dockerfile-functions.sh fi -echo "Defining function setup_kaniko" -# Just arranges authentication by copying the config.json file to right spot -setup_kaniko() { - mkdir -p /kaniko/.docker - incoming="$1" - if [ -z "$incoming" ] ; then - echo "No incoming kaniko config file. Using $DOCKER_AUTH_CONFIG" - incoming="$DOCKER_AUTH_CONFIG" - fi - if [ -e "$incoming" ] ; then - echo "Copying $incoming to /kaniko/.docker/config.json" - echo "lines: $(wc -l $incoming)" - cp $incoming /kaniko/.docker/config.json - else - echo "No incoming docker configuration file '$incoming'" - fi +# sets up kaniko, executes it, and stores some variables +# param: directory to execute for +run_kaniko() { + echo "Using build args $DOCKER_BUILD_ARGS" + setup_kaniko "$DOCKER_AUTH_CONFIG_FILE" + kaniko_execute "$@" + store_image_version } + echo "Defining function kaniko_execute" +# $1: is the directory to run for, defaults to DOCKER_DIR +# $2: is a version to build defaults to PROJECT_VERSION kaniko_execute() { dir="$1" if [ -z "$dir" ] ; then @@ -58,6 +43,10 @@ kaniko_execute() { else echo "Building and pushing image: \"$image\" ($LATEST), (project) version: $version" fi + if [ ! -f "/kaniko/executor" ] ; then + echo "kaniko/executor not found" + return 1 + fi /kaniko/executor $KANIKO_ARGS \ --context $dir \ --dockerfile $dir/Dockerfile \ diff --git a/scripts/kaniko-gitlab-functions.sh b/scripts/kaniko-gitlab-functions.sh new file mode 100644 index 0000000..4cfbf9c --- /dev/null +++ b/scripts/kaniko-gitlab-functions.sh @@ -0,0 +1,71 @@ +##!/bin/sh +# This is the script can be used to build and push (via kaniko) an openshift statefull set. +# This script used to be present in gitlab templates, but that's unmaintainble and unreusable +# This can be used locally via run-in-docker.sh in a directory of interest + + +# as /kaniko.sh but add some functions related to running in gitlab + +echo "kaniko gitlab functions" + +# shellcheck source=${KANIKO_SCRIPTS}kaniko-functions.sh +if ! type os_app_name &> /dev/null ; then +. "$KANIKO_SCRIPTS"kaniko-functions.sh +fi + +echo "Defining function setup_kaniko" +# Just arranges authentication by copying the config.json file to right spot +# $1 ~/.docker/config.json file. Defaults to DOCKER_AUTH_CONFIG +setup_kaniko() { + mkdir -p /kaniko/.docker + incoming="$1" + if [ -z "$incoming" ] ; then + echo "No incoming kaniko config file. Using $DOCKER_AUTH_CONFIG" + incoming="$DOCKER_AUTH_CONFIG" + fi + if [ -e "$incoming" ] ; then + echo "Copying $incoming to /kaniko/.docker/config.json" + echo "lines: $(wc -l $incoming)" + cp $incoming /kaniko/.docker/config.json + else + echo "No incoming docker configuration file '$incoming'" + fi +} + + + +# Stores relevant variables determined by get_artifact_versions in job.env +# I'm not sure this is very useful. You can just as wel call get_articaft_versions again in the next job +# which will have the same effect, but I think this is robust, because no need for fiddling with 'need=', +# which is confusing and error-prone. +store_image_version() { + echo "IMAGE_TAG=$IMAGE_TAG" | tee job.env + echo "IMAGE=$IMAGE" | tee -a job.env + echo "IMAGE_NAME=$IMAGE_NAME" | tee -a job.env + echo "FULL_IMAGE_NAME=$FULL_IMAGE_NAME" | tee -a job.env + echo "PROJECT_VERSION=$PROJECT_VERSION" | tee -a job.env +} + +echo "Define determine_image_version" +# If store_image_version was called earlier in the pipeline, the the results of this are in job.env +determine_image_version() { + + # used by plain docker builds + if [ "$AS_LATEST" = 'true' ] ; then + export LATEST="--destination $REGISTRY/$IMAGE_NAME" + else + export LATEST= + fi + + if [ "$IMAGE_TAG" = '' ] ; then + echo "No IMAGE_TAG defined. Breaking build. This must be defined in job rule!" + exit 1 + fi + if [ "$IMAGE_NAME" = '' ] ; then + echo "No IMAGE_NAME defined. Taking from os_app_name" + IMAGE_NAME=$(os_app_name) + export IMAGE_NAME + fi + export IMAGE=$REGISTRY/$IMAGE_NAME:$IMAGE_TAG + echo "IMAGE: $IMAGE" +} \ No newline at end of file diff --git a/scripts/kaniko-maven.sh b/scripts/kaniko-maven.sh index 1f4661e..8cd8083 100644 --- a/scripts/kaniko-maven.sh +++ b/scripts/kaniko-maven.sh @@ -1,22 +1,38 @@ ##!/bin/sh +# package found war file (or files using OS_APPLICATIONS) -. /kaniko-gitlab.sh -echo "Defining function package_applications" -package_application() { +if ! type kaniko_execute &> /dev/null ; then + . "$KANIKO_SCRIPTS"kaniko-functions.sh +fi + +echo "Defining function package_war(s)" + +package_war() { app_dir=$1 - echo "Packaging '$app_dir' (pom version: $PROJECT_VERSION)" + echo -e "----------\nPackaging ${EMP}'$app_dir'${NC} (pom version: $PROJECT_VERSION)" get_artifact_versions $app_dir $PROJECT_VERSION # defined in docker.yml ls -l $app_dir/target/*.war kaniko_execute $app_dir } +package_application() { + package_war "$@" +} -package_applications() { - for app_dir in $(echo $OS_APPLICATIONS | sed "s/,/ /g"); do - package_application $app_dir - done - if [ -z "$OS_APPLICATIONS" ]; then +package_wars() { + if [ ! -z "$OS_APPLICATIONS" ] ; then + for app_dir in $(echo $OS_APPLICATIONS | sed "s/,/ /g"); do + package_war $app_dir + done + echo Finished packaging $OS_APPLICATIONS + elif [ ! -f Dockerfile ]; then echo "Packaging the root directory only" - package_application . + package_war . + else + echo "NOTHING to do. No Dockerfile and not OS_APPLICATIONS find" fi - echo Finished packaging $OS_APPLICATIONS + +} + +package_applications() { + package_wars "$@" } \ No newline at end of file diff --git a/scripts/script.sh b/scripts/script.sh index 3d5e027..262034c 100644 --- a/scripts/script.sh +++ b/scripts/script.sh @@ -1,9 +1,9 @@ #!/bin/sh echo "--------" -echo "Executing $0 in " -pwd + export DOCKER_DIR DOCKER_DIR="$(pwd)" +echo "Executing in ${DOCKER_DIR}" export DOCKER_BUILD_ARGS= # Uses eval, when overriding escape whitespace: '--build-arg\ "FOO=BAR"' export REGISTRY="${REGISTRY:-registry.npohosting.nl}" export DOCKER_AUTH_CONFIG_FILE=$HOME/.docker/config-gitlab.json @@ -13,9 +13,9 @@ export TRACE='false' echo Using registry ${REGISTRY} -. /kaniko-gitlab.sh +. "$KANIKO_SCRIPTS"kaniko-gitlab-functions.sh + +. "$KANIKO_SCRIPTS"kaniko-maven.sh + +package_wars -echo "Using build args $DOCKER_BUILD_ARGS" -setup_kaniko "$DOCKER_AUTH_CONFIG_FILE" -kaniko_execute -store_image_version \ No newline at end of file