diff --git a/kaniko.sh b/kaniko.sh index 0fbffab..2f9b929 100755 --- a/kaniko.sh +++ b/kaniko.sh @@ -5,9 +5,6 @@ $(dirname "${BASH_SOURCE[0]}")/local-setup.sh cat job.env -# REGISTRY: openshift-image-registry.apps.cluster.chp4.io. - -#echo "Registry: ${REGISTRY}" #KANIKO_IMAGE=npo-poms/kaniko KANIKO_IMAGE=ghcr.io/npo-poms/kaniko:5 diff --git a/local-maven.sh b/local-maven.sh index 56beeb7..52dd7d3 100755 --- a/local-maven.sh +++ b/local-maven.sh @@ -1,7 +1,10 @@ #!/bin/bash -$(dirname "${BASH_SOURCE[0]}")/local-setup.sh +DIR=$(dirname "${BASH_SOURCE[0]}") +KANIKO_SCRIPTS=$DIR/scripts/ + +. $DIR/local-setup.sh + cat job.env -KANIKO_SCRIPTS=$(dirname ${BASH_SOURCE[0]})/scripts/ . "$KANIKO_SCRIPTS"/script.sh \ No newline at end of file diff --git a/scripts/dockerfile-functions.sh b/scripts/dockerfile-functions.sh index b8c81b9..c09665a 100644 --- a/scripts/dockerfile-functions.sh +++ b/scripts/dockerfile-functions.sh @@ -40,10 +40,10 @@ os_app_name() { echo "defining get_artifact_versions" # exports PROJECT_VERSION, IMAGE_TAG, IMAGE, IMAGE_NAME -# first argument: directory containing the docker file -# second argument: version (exported as PROJECT_VERSION) +# $1: directory containing the docker file +# $2: version (exported as PROJECT_VERSION) # the name of the image is determined with os_app_name -get_artifact_versions() { +get_docker_image_name() { DIR=$1 export PROJECT_VERSION=$2 # gets name from docker file diff --git a/scripts/kaniko-functions.sh b/scripts/kaniko-functions.sh index 8774241..1b98b87 100644 --- a/scripts/kaniko-functions.sh +++ b/scripts/kaniko-functions.sh @@ -9,6 +9,39 @@ if ! type os_app_name &> /dev/null; then . "$KANIKO_SCRIPTS"dockerfile-functions.sh fi +# For a certain directory, calculate the docker image name, and run kaniko +# $1: the diretory to run for +package_docker() { + app_dir=$1 + echo -e "----------\nPackaging ${TXT_HI}'$app_dir'${TXT_CLEAR} (pom version: $PROJECT_VERSION)" + get_docker_image_name $app_dir $PROJECT_VERSION # defined in docker.yml + kaniko_execute $app_dir +} + + +package_all_docker() { + if [ ! -z "$OS_APPLICATIONS" ] ; then + for app_dir in $(echo $OS_APPLICATIONS | sed "s/,/ /g"); do + package_docker $app_dir + done + echo Finished packaging $OS_APPLICATIONS + elif [ -f Dockerfile ]; then + echo "Packaging the root directory only" + package_docker . + else + + echo "No Dockerfile and no OS_APPLICATIONS variable found" + OS_APPLICATIONS=$(find . -maxdepth 3 -mindepth 3 -name "*${PROJECT_VERSION}.war" -exec sh -c 'f=$(dirname $1); (cd $f/..; basename $PWD) ;' shell {} \; | tr '\n' ',' | sed 's/,$//') + if [ ! -z "$OS_APPLICATIONS" ] ; then + echo "Guessed OS_APPLICATIONS=$OS_APPLICATIONS" + package_wars + else + echo "Could not guess either for ${PROJECT_VERSION}" + fi + fi + +} + # sets up kaniko, executes it in a dir, and stores some variables # $1: directory to execute for run_kaniko() { @@ -19,6 +52,13 @@ run_kaniko() { store_image_name } +run_kaniko_all() { + echo "Using build args $DOCKER_BUILD_ARGS" + setup_kaniko "$DOCKER_AUTH_CONFIG_FILE" + package_all_docker + store_variables +} + # 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=', diff --git a/scripts/kaniko-gitlab-functions.sh b/scripts/kaniko-gitlab-functions.sh deleted file mode 100644 index 116cd8a..0000000 --- a/scripts/kaniko-gitlab-functions.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/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" - - -if [ -f job.env ] ; then - echo "Found job.env!" - cat job.env - . ./job.env -else - echo "No job.env in $(pwd)" -fi - -# shellcheck source=${KANIKO_SCRIPTS}kaniko-functions.sh -if ! type os_app_name &> /dev/null ; then -. "$KANIKO_SCRIPTS"kaniko-functions.sh -fi - - - - diff --git a/scripts/kaniko-maven.sh b/scripts/kaniko-maven.sh deleted file mode 100644 index 4470758..0000000 --- a/scripts/kaniko-maven.sh +++ /dev/null @@ -1,53 +0,0 @@ -##!/bin/sh -# package found war file (or files using OS_APPLICATIONS) - -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 -e "----------\nPackaging ${TXT_HI}'$app_dir'${TXT_CLEAR} (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_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_war . - else - - echo "No Dockerfile and no OS_APPLICATIONS variable found" - OS_APPLICATIONS=$(find . -maxdepth 3 -mindepth 3 -name "*${PROJECT_VERSION}.war" -exec sh -c 'f=$(dirname $1); (cd $f/..; basename $PWD) ;' shell {} \; | tr '\n' ',' | sed 's/,$//') - if [ ! -z "$OS_APPLICATIONS" ] ; then - echo "Guessed OS_APPLICATIONS=$OS_APPLICATIONS" - package_wars - else - echo "Could not guess either for ${PROJECT_VERSION}" - fi - fi - -} - -package_applications() { - package_wars "$@" -} - -run_kaniko_maven() { - echo "Using build args $DOCKER_BUILD_ARGS" - setup_kaniko "$DOCKER_AUTH_CONFIG_FILE" - package_wars - store_variables -} \ No newline at end of file diff --git a/scripts/script.sh b/scripts/script.sh index c02dae5..1617d47 100644 --- a/scripts/script.sh +++ b/scripts/script.sh @@ -11,18 +11,27 @@ if [ "$TRACE" = 'true' ]; then set -x fi -. "$KANIKO_SCRIPTS"kaniko-gitlab-functions.sh -. "$KANIKO_SCRIPTS"kaniko-maven.sh +if [ -f job.env ] ; then + echo "Found job.env!" + cat job.env + . ./job.env +else + echo "No job.env in $(pwd)" +fi + + +. "$KANIKO_SCRIPTS"kaniko-functions.sh + fun=$1 if [ "$fun" = "help" ] ;then echo usage - echo $0 Run 'run_kaniko_maven' to deploy the war project of the current directory + echo $0 Default 'run_kaniko_all' to deploy all found docker deployables echo or if OS_APPLICATIONS is defined in all the subdirectories it exit fi if [ -z "$fun" ] ; then - fun="run_kaniko_maven" + fun="run_kaniko_all" fi $fun .