Skip to content

Commit

Permalink
Cleaning/testing scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Nov 16, 2024
1 parent 47f550b commit 33b798b
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 121 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ docker:

test:
docker run -it --entrypoint /bin/sh npo-poms/kaniko

source:
export KANIKO_SCRIPTS=$(pwd)/scripts;
29 changes: 2 additions & 27 deletions kaniko.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,14 @@
# 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.

#echo "Registry: ${REGISTRY}"
#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} \
Expand All @@ -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


6 changes: 6 additions & 0 deletions local-maven.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

. $(dirname "${BASH_SOURCE[0]}")/local-setup.sh

KANIKO_SCRIPTS=$(dirname ${BASH_SOURCE[0]})/scripts/
. "$KANIKO_SCRIPTS"/script.sh
25 changes: 25 additions & 0 deletions local-setup.sh
Original file line number Diff line number Diff line change
@@ -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"
54 changes: 8 additions & 46 deletions scripts/docker-build-setup.sh → scripts/dockerfile-functions.sh
Original file line number Diff line number Diff line change
@@ -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



Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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=<previous job>',
# 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
}

43 changes: 16 additions & 27 deletions scripts/kaniko-gitlab.sh → scripts/kaniko-functions.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 \
Expand Down
71 changes: 71 additions & 0 deletions scripts/kaniko-gitlab-functions.sh
Original file line number Diff line number Diff line change
@@ -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=<previous job>',
# 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"
}
38 changes: 27 additions & 11 deletions scripts/kaniko-maven.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"
}
Loading

0 comments on commit 33b798b

Please sign in to comment.