Benutzer-Werkzeuge

Webseiten-Werkzeuge


tachtler:gitlab_centos_7_-_ci_cd

Gitlab CentOS 7 - CI / CD

Gitlab ist ein, in der Community Edition unter einer MIT-Lizenz zur Verfügung gestelltes System, zur Verwaltung von Git-Repositorys im Browser, was den unentgeltlichen Betrieb auf einem eigenen Server ermöglicht.

Gitlab CI (Continous Integration) bedeutet vereinfacht gesagt, dass nach jeder Änderung am Source Code eines Projektes ein Script ausgeführt wird. Das Gitlab CI (Continous Integration)-Script wird in den meisten Fällen über eine Versionsverwaltung angestoßen. Dateien welche dieses Script erstellt, können später als sogenannte „Build Artifacts“ genutzt werden.

Das Konzept von Gitlab CI (Continous Integration) basiert auf so genannten „Runnern“, welche die eigentlichen „Build-Scripts“ ausführen. Gitlab CI (Continous Integration) ist nur für das Orchestrieren dieser „Runner“ und dem Zusammentragen von Resultaten und „Build-Artifacts“ zuständig. Dadurch haben die Gitlab CI (Continous Integration)-Scripte kein Zugriff auf Gitlab selbst.

Die „Runner“ melden sich alle paar Sekunden über HTTPS bei Gitlab und fragen nach, ob es für sie einen Job gibt, Gitlab CI (Continous Integration) kann die „Runner“ von sich aus nicht kontaktieren. Dies erleichtert die Einrichtung von neuen „Runnern“ erheblich, einzige Voraussetzung ist, dass sich der „Runner“ per HTTPS mit Gitlab verbinden kann.

Gitlab gibt es ebenfalls in einer kostenpflichtigen Enterprise Edition

Ab hier werden root-Rechte zur Ausführung der nachfolgenden Befehle benötigt. Um der Benutzer root zu werden, geben Sie bitte nachfolgenden Befehl ein:

$ su -
Password: 

Voraussetzungen

Als Voraussetzung für die hier, nachfolgend dargestellte Installation von Gitlab sind folgende Komponenten erforderlich:

Nachfolgende Konfigurationen sind Beispiele für den Einsatz der Gitlab-CI (Continous Integration):

CI - Tomcat: Build *.war

Nachfolgendes Beispiel zeigt den Einsatz der Gitlab-CI (Continous Integration), zur Erstellung eines Apache Tomcat *.war-Archiv, welches NUR mithilfe der

  • Sourcen des Projekts „Homepage“ „gecloned“ aus dem lokalen Gitlab-Repository
  • Einbindung der Apache Tomcat-Bibliotheken
  • Einbindung der Java OpenJDK-Bibliotheken

auskommt.

.gitlab-ci.yml - WAR-Archiv

Nachfolgende Konfigurationsdatei für die Gitlab-CI (Continous Integration) wird standardmäßig im Hauptverzeichnis der Web-Anwendung erzeugt bzw. abgelegt und soll hier nachfolgenden Inhalt haben:

stages:
 - build
 - test
.job_war_build:
  script: &war_build
  - "sh  ${CI_PROJECT_DIR}/web/WEB-INF/script/war_build_homepage.sh"
.job_war_test:
  script: &war_test
  - "sh  ${CI_PROJECT_DIR}/web/WEB-INF/script/war_test_homepage.sh"
job_war_build:
  stage: build
  script: *war_build
job_war_test:
  stage: test
  script: *war_test

war_build_homepage.sh

Nachfolgendes Skript führt die eigentliche Erzeugung eines Apache Tomcat *.war-Archiv durch.

Zur Vorbereitung der Ablage der Gitlab-CI-Ergebnisse in einem Verzeichnis im Dateisystem, kann nachfolgender Befehl verwendet werden:

# mkdir -p /var/opt/gitlab/gitlab-runner/www

Anschließend müssen noch die Datei- und Besitzrechte wie folgt angepasst werden, damit der Gitlab - Omnibus und später ggf. der nginx-Webserver Zugriff auf das Verzeichnis und die darunterliegenden Dateien hat, was mit nachfolgenden Befehlen durchgeführt werden kann:

# chmod 775 /var/opt/gitlab/gitlab-runner/www

und

# chown 775 gitlab-runner:gitlab-runner /var/opt/gitlab/gitlab-runner/www

Zur Überprüfung, ob die entsprechenden Datei- und Besitzrechte richtig gesetzt wurden, kann nachfolgender Befehl verwendet werden:

# ls -l -d /var/opt/gitlab/gitlab-runner/www
drwxrwxr-x 4 gitlab-runner gitlab-runner 53 Feb  2 07:19 /var/opt/gitlab/gitlab-runner/www

Anschließend wird das Skript mit nachfolgendem Inhalt erzeugt im Projekt, z.B. unter

  • /web/WEB_INF/script

mit einem Editor erstellt und gespeichert:

  • [Pfad zum Projektordner]/web/WEB_INF/script/war_build_homepage.sh

Nachfolgend eine Bildschirmkopie, eines Beispiels mit Eclipse (Eclipse IDE for Java EE Developers) erstellten Projektes, einer Apache Tomcat-Anwendung (Nur Ausschnitt des „Project Explorer“s)

Eclipse IDE for Java EE Developers - Project Explorer

#!/bin/bash
 
##############################################################################
# Script-Name : war_build_homepage.sh                                        #
# Description : Script to build a web application war file.                  #
#                                                                            #
#                                                                            #
#                                                                            #
#                                                                            #
# Last update : 28.01.2018                                                   #
# Version     : 1.00                                                         #
#                                                                            #
# Author      : Klaus Tachtler, <klaus@tachtler.net>                         #
# DokuWiki    : http://www.dokuwiki.tachtler.net                             #
# Homepage    : http://www.tachtler.net                                      #
#                                                                            #
#  +----------------------------------------------------------------------+  #
#  | This program is free software; you can redistribute it and/or modify |  #
#  | it under the terms of the GNU General Public License as published by |  #
#  | the Free Software Foundation; either version 2 of the License, or    |  #
#  | (at your option) any later version.                                  |  #
#  +----------------------------------------------------------------------+  #
#                                                                            #
# Copyright (c) 2018 by Klaus Tachtler.                                      #
#                                                                            #
##############################################################################
 
##############################################################################
#                                H I S T O R Y                               #
##############################################################################
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
#############################################################################
 
##############################################################################
# >>> Please edit following lines for personal settings and custom usages. ! #
##############################################################################
 
# CUSTOM - Script-Name.
SCRIPT_NAME='war_build_homepage'
 
# CUSTOM - Build-Environment settings.
CLASS_PATH=${CI_PROJECT_DIR}'/web/WEB-INF/lib/*:/usr/lib/jvm/jre/lib/*:/usr/lib/jvm/jre/lib/ext/*:/usr/share/tomcat/lib/*:.'
CUSTOM_DIR=${CI_PROJECT_DIR}
SOURCE_DIR='/src'
WEBAPP_DIR='/web'
TARGET_DIR='/var/opt/gitlab/gitlab-runner/www/homepage-build/'
TARGET_WAR='homepage.war'
 
# CUSTOM - Mail-Recipient.
MAIL_RECIPIENT='gitlab-builds-user@example.com'
 
# CUSTOM - Status-Mail [Y|N].
MAIL_STATUS='N'
 
##############################################################################
# >>> Normaly there is no need to change anything below this comment line. ! #
##############################################################################
 
# Variables.
JAVAC_COMMAND=`command -v javac`
ZIP_COMMAND=`command -v zip`
FIND_COMMAND=`command -v find`
TOUCH_COMMAND=`command -v touch`
CP_COMMAND=`command -v cp`
RM_COMMAND=`command -v rm`
CAT_COMMAND=`command -v cat`
DATE_COMMAND=`command -v date`
MKDIR_COMMAND=`command -v mkdir`
PROG_SENDMAIL=`command -v sendmail`
FILE_LOCK='/tmp/'$SCRIPT_NAME'.lock'
FILE_LOG=$TARGET_DIR$SCRIPT_NAME'.log'
FILE_LAST_LOG='/tmp/'$SCRIPT_NAME'.log'
FILE_MAIL='/tmp/'$SCRIPT_NAME'.mail'
VAR_HOSTNAME=`uname -n`
VAR_SENDER='root@'$VAR_HOSTNAME
VAR_EMAILDATE=`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%Z)'`
 
# Functions.
function log() {
        echo $1
        echo `$DATE_COMMAND '+%Y/%m/%d %H:%M:%S'` " INFO:" $1 >>${FILE_LAST_LOG}
}
 
function retval() {
if [ "$?" != "0" ]; then
        case "$?" in
        *)
                log "ERROR: Unknown error $?"
        ;;
        esac
fi
}
 
function movelog() {
        $CAT_COMMAND $FILE_LAST_LOG >> $FILE_LOG
        $RM_COMMAND -f $FILE_LAST_LOG   
        $RM_COMMAND -f $FILE_LOCK
}
 
function sendmail() {
        case "$1" in
        'STATUS')
                MAIL_SUBJECT='Status execution '$SCRIPT_NAME' script.'
        ;;
        *)
                MAIL_SUBJECT='ERROR while execution '$SCRIPT_NAME' script !!!'
        ;;
        esac
 
$CAT_COMMAND <<MAIL >$FILE_MAIL
Subject: $MAIL_SUBJECT
Date: $VAR_EMAILDATE
From: $VAR_SENDER
To: $MAIL_RECIPIENT
MAIL
 
$CAT_COMMAND $FILE_LAST_LOG >> $FILE_MAIL
 
$PROG_SENDMAIL -f $VAR_SENDER -t $MAIL_RECIPIENT < $FILE_MAIL
 
$RM_COMMAND -f $FILE_MAIL
 
}
 
# Main.
log ""
log "+-----------------------------------------------------------------+"
log "| Start build script to build a web application war file by hand. |"
log "+-----------------------------------------------------------------+"
log ""
log "Run script with following parameter:"
log ""
log "CLASS_PATH....: $CLASS_PATH"
log "CUSTOM_DIR....: $CUSTOM_DIR"
log "SOURCE_DIR....: $SOURCE_DIR"
log "WEBAPP_DIR....: $WEBAPP_DIR"
log "TARGET_DIR....: $TARGET_DIR"
log "TARGET_WAR....: $TARGET_WAR"
log ""
log "MAIL_RECIPIENT: $MAIL_RECIPIENT"
log "MAIL_STATUS...: $MAIL_STATUS"
log ""
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$JAVAC_COMMAND" ]; then
        log "Check if command '$JAVAC_COMMAND' was found....................[FAILED]"
        sendmail ERROR
        movelog
        exit 11
else
        log "Check if command '$JAVAC_COMMAND' was found....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$ZIP_COMMAND" ]; then
        log "Check if command '$ZIP_COMMAND' was found......................[FAILED]"
        sendmail ERROR
        movelog
        exit 12
else
        log "Check if command '$ZIP_COMMAND' was found......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$FIND_COMMAND" ]; then
        log "Check if command '$FIND_COMMAND' was found.....................[FAILED]"
        sendmail ERROR
        movelog
        exit 13
else
        log "Check if command '$FIND_COMMAND' was found.....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$TOUCH_COMMAND" ]; then
        log "Check if command '$TOUCH_COMMAND' was found....................[FAILED]"
        sendmail ERROR
        movelog
        exit 14
else
        log "Check if command '$TOUCH_COMMAND' was found....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$CP_COMMAND" ]; then
        log "Check if command '$CP_COMMAND' was found.......................[FAILED]"
        sendmail ERROR
        movelog
        exit 15
else
        log "Check if command '$CP_COMMAND' was found.......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$RM_COMMAND" ]; then
        log "Check if command '$RM_COMMAND' was found.......................[FAILED]"
        sendmail ERROR
        movelog
        exit 16
else
        log "Check if command '$RM_COMMAND' was found.......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$CAT_COMMAND" ]; then
        log "Check if command '$CAT_COMMAND' was found......................[FAILED]"
        sendmail ERROR
        movelog
        exit 17
else
        log "Check if command '$CAT_COMMAND' was found......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$DATE_COMMAND" ]; then
        log "Check if command '$DATE_COMMAND' was found.....................[FAILED]"
        sendmail ERROR
        movelog
        exit 18
else
        log "Check if command '$DATE_COMMAND' was found.....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$MKDIR_COMMAND" ]; then
        log "Check if command '$MKDIR_COMMAND' was found....................[FAILED]"
        sendmail ERROR
        movelog
        exit 19
else
        log "Check if command '$MKDIR_COMMAND' was found....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$PROG_SENDMAIL" ]; then
        log "Check if command '$PROG_SENDMAIL' was found................[FAILED]"
        sendmail ERROR
        movelog
        exit 20
else
        log "Check if command '$PROG_SENDMAIL' was found................[  OK  ]"
fi
 
# Check if LOCK file NOT exist.
if [ ! -e "$FILE_LOCK" ]; then
        log "Check if script is NOT already runnig .........................[  OK  ]"
 
        $TOUCH_COMMAND $FILE_LOCK
else
        log "Check if script is NOT already runnig .........................[FAILED]"
        log ""
        log "ERROR: The script was already running, or LOCK file already exists!"
        log ""
        sendmail ERROR
        movelog
        exit 20
fi
 
# Start build process
log ""
log "+-----------------------------------------------------------------+"
log "| Run build process ............................................. |"
log "+-----------------------------------------------------------------+"
log ""
 
# Step 01: Make target directory.
# Check if TARGET_DIR exists. If yes delete it ande make it again. If no make it.
if [ ! -d "$TARGET_DIR" ]; then
        log "Check if TARGET_DIR exists.................................[FAILED]"
        $MKDIR_COMMAND -p $TARGET_DIR
        log "TARGET_DIR was now created.................................[  OK  ]"
else
        log "Check if DIR_BACKUP exists.................................[  OK  ]"
        $RM_COMMAND -rf $TARGET_DIR
        log "TARGET_DIR was now deleted.................................[  OK  ]"
        $MKDIR_COMMAND -p $TARGET_DIR
        log "TARGET_DIR was now created.................................[  OK  ]"
fi
 
# Step 02: Copy webapp directorys and files to target directory.
log "Copy WEBAPP_DIR to TARGET_DIR .............................[  OK  ]"
$CP_COMMAND -ar $CUSTOM_DIR/$WEBAPP_DIR/* $TARGET_DIR
 
# Step 03: Copy source directorys and files to target directory.
log "Copy SOURCE_DIR to TARGET_DIR/WEB-INF/classes .............[  OK  ]"
$CP_COMMAND -ar $CUSTOM_DIR/$SOURCE_DIR $TARGET_DIR/WEB-INF/classes
 
# Step 04: Compile the java files to class files.
log "Compile *.java in TARGET_DIR/WEB-INF/classes to *.class ...[  OK  ]"
$FIND_COMMAND $TARGET_DIR/WEB-INF/classes -name *.java -print | xargs $JAVAC_COMMAND -cp $CLASS_PATH
 
# Step 05: Delete the java source files.
log "Delete *.java in TARGET_DIR/WEB-INF/classes ...............[  OK  ]"
$FIND_COMMAND $TARGET_DIR/WEB-INF/classes -name *.java -print | xargs $RM_COMMAND -f
 
# Step 06: Make war file.
log "Make *.war file from TARGET_DIR ...........................[  OK  ]"
cd $TARGET_DIR
$ZIP_COMMAND -9r $TARGET_WAR *
 
# Delete LOCK file.
if [ "$?" != 0 ]; then
        retval $?
        log ""
        $RM_COMMAND -f $FILE_LOCK
        sendmail ERROR
        movelog
        exit 99
else
        log ""
        log "+-----------------------------------------------------------------+"
        log "| End build process ............................................. |"
        log "+-----------------------------------------------------------------+"
        log ""
fi
 
# Finish syncing.
log "+-----------------------------------------------------------------+"
log "| Finish......................................................... |"
log "+-----------------------------------------------------------------+"
log ""
 
# Status e-mail.
if [ $MAIL_STATUS = 'Y' ]; then
        sendmail STATUS
fi
# Move temporary log to permanent log
movelog
 
exit 0

Eine Besonderheit stellt die Verwendung einer Umgebungsvariable welche von Gitlab-CI (Continous Integration) zur Verfügung gestellt wird dar.

Weitere Informationen können unter nachfolgenden externen Link angerufen werden:

war_build_homepage.sh - Pipeline Ausgabe

Eine Ausgabe in etwa wie nachfolgende sollte erscheinen, wenn der „Runner“ die Gitlab-CI (Continous Integration) erfolgreich abgearbeitet hat, was nachfolgende Bildschirmkopie zeigt:

Projekt - CI /CD - Jobs - job_war_build_homepage

war_test_homepage.sh

Nachfolgendes Skript führt spezifische Junit-Tests aus dem Apache Tomcat *.war-Archiv durch.

:!: ACHTUNG - Dies ist NUR ein Beispiel meiner JUnit-Test und ist nicht allgemein verwendbar!!!

Zur Vorbereitung der Ablage der Gitlab-CI-Ergebnisse in einem Verzeichnis im Dateisystem, kann nachfolgender Befehl verwendet werden (Falls die Anlage des Verzeichnisses nicht schon durchgeführt wurde !!!):

# mkdir -p /var/opt/gitlab/gitlab-runner/www

Anschließend müssen noch die Datei- und Besitzrechte wie folgt angepasst werden, damit der Gitlab - Omnibus und später ggf. der nginx-Webserver Zugriff auf das Verzeichnis und die darunterliegenden Dateien hat, was mit nachfolgenden Befehlen durchgeführt werden kann:

# chmod 775 /var/opt/gitlab/gitlab-runner/www

und

# chown 775 gitlab-runner:gitlab-runner /var/opt/gitlab/gitlab-runner/www

Zur Überprüfung, ob die entsprechenden Datei- und Besitzrechte richtig gesetzt wurden, kann nachfolgender Befehl verwendet werden:

# ls -l -d /var/opt/gitlab/gitlab-runner/www
drwxrwxr-x 4 gitlab-runner gitlab-runner 53 Feb  2 07:19 /var/opt/gitlab/gitlab-runner/www

Anschließend wird das Skript mit nachfolgendem Inhalt erzeugt im Projekt, z.B. unter

  • /web/WEB_INF/script

mit einem Editor erstellt und gespeichert:

  • [Pfad zum Projektordner]/web/WEB_INF/script/war_test_homepage.sh

Nachfolgend eine Bildschirmkopie, eines Beispiels mit Eclipse (Eclipse IDE for Java EE Developers) erstellten Projektes, einer Apache Tomcat-Anwendung (Nur Ausschnitt des „Project Explorer“s)

Eclipse IDE for Java EE Developers - Project Explorer

#!/bin/bash
 
##############################################################################
# Script-Name : war_test_homepage.sh                                         #
# Description : Script to test a web application java classes with.          #
#                                                                            #
#                                                                            #
#                                                                            #
#                                                                            #
# Last update : 28.01.2018                                                   #
# Version     : 1.00                                                         #
#                                                                            #
# Author      : Klaus Tachtler, <klaus@tachtler.net>                         #
# DokuWiki    : http://www.dokuwiki.tachtler.net                             #
# Homepage    : http://www.tachtler.net                                      #
#                                                                            #
#  +----------------------------------------------------------------------+  #
#  | This program is free software; you can redistribute it and/or modify |  #
#  | it under the terms of the GNU General Public License as published by |  #
#  | the Free Software Foundation; either version 2 of the License, or    |  #
#  | (at your option) any later version.                                  |  #
#  +----------------------------------------------------------------------+  #
#                                                                            #
# Copyright (c) 2018 by Klaus Tachtler.                                      #
#                                                                            #
##############################################################################
 
##############################################################################
#                                H I S T O R Y                               #
##############################################################################
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
#############################################################################
 
##############################################################################
# >>> Please edit following lines for personal settings and custom usages. ! #
##############################################################################
 
# CUSTOM - Script-Name.
SCRIPT_NAME='war_test'
 
# CUSTOM - Build-Environment settings.
CUSTOM_DIR=${CI_PROJECT_DIR}
SOURCE_DIR='/src'
WEBAPP_DIR='/web'
TARGET_DIR='/var/opt/gitlab/gitlab-runner/www/homepage-junittests/'
 
# CUSTOM - Mail-Recipient.
MAIL_RECIPIENT='root@tachtler.net'
 
# CUSTOM - Status-Mail [Y|N].
MAIL_STATUS='N'
 
##############################################################################
# >>> Normaly there is no need to change anything below this comment line. ! #
##############################################################################
 
# Variables.
JAVAC_COMMAND=`command -v javac`
JAVA_COMMAND=`command -v java`
FIND_COMMAND=`command -v find`
TOUCH_COMMAND=`command -v touch`
CP_COMMAND=`command -v cp`
RM_COMMAND=`command -v rm`
CAT_COMMAND=`command -v cat`
DATE_COMMAND=`command -v date`
MKDIR_COMMAND=`command -v mkdir`
PROG_SENDMAIL=`command -v sendmail`
FILE_LOCK='/tmp/'$SCRIPT_NAME'.lock'
FILE_LOG=$TARGET_DIR$SCRIPT_NAME'.log'
FILE_LAST_LOG='/tmp/'$SCRIPT_NAME'.log'
FILE_MAIL='/tmp/'$SCRIPT_NAME'.mail'
VAR_HOSTNAME=`uname -n`
VAR_SENDER='root@'$VAR_HOSTNAME
VAR_EMAILDATE=`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%Z)'`
 
# Functions.
function log() {
        echo $1
        echo `$DATE_COMMAND '+%Y/%m/%d %H:%M:%S'` " INFO:" $1 >>${FILE_LAST_LOG}
}
 
function retval() {
if [ "$?" != "0" ]; then
        case "$?" in
        *)
                log "ERROR: Unknown error $?"
        ;;
        esac
fi
}
 
function movelog() {
        $CAT_COMMAND $FILE_LAST_LOG >> $FILE_LOG
        $RM_COMMAND -f $FILE_LAST_LOG   
        $RM_COMMAND -f $FILE_LOCK
}
 
function sendmail() {
        case "$1" in
        'STATUS')
                MAIL_SUBJECT='Status execution '$SCRIPT_NAME' script.'
        ;;
        *)
                MAIL_SUBJECT='ERROR while execution '$SCRIPT_NAME' script !!!'
        ;;
        esac
 
$CAT_COMMAND <<MAIL >$FILE_MAIL
Subject: $MAIL_SUBJECT
Date: $VAR_EMAILDATE
From: $VAR_SENDER
To: $MAIL_RECIPIENT
MAIL
 
$CAT_COMMAND $FILE_LAST_LOG >> $FILE_MAIL
 
$PROG_SENDMAIL -f $VAR_SENDER -t $MAIL_RECIPIENT < $FILE_MAIL
 
$RM_COMMAND -f $FILE_MAIL
 
}
 
# Main.
log ""
log "+-----------------------------------------------------------------+"
log "| Start test script to run junit web tests on commandline by hand.|"
log "+-----------------------------------------------------------------+"
log ""
log "Run script with following parameter:"
log ""
log "CUSTOM_DIR....: $CUSTOM_DIR"
log "SOURCE_DIR....: $SOURCE_DIR"
log "WEBAPP_DIR....: $WEBAPP_DIR"
log "TARGET_DIR....: $TARGET_DIR"
log ""
log "MAIL_RECIPIENT: $MAIL_RECIPIENT"
log "MAIL_STATUS...: $MAIL_STATUS"
log ""
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$JAVAC_COMMAND" ]; then
        log "Check if command '$JAVAC_COMMAND' was found....................[FAILED]"
        sendmail ERROR
        movelog
        exit 11
else
        log "Check if command '$JAVAC_COMMAND' was found....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$JAVA_COMMAND" ]; then
        log "Check if command '$JAVA_COMMAND' was found.....................[FAILED]"
        sendmail ERROR
        movelog
        exit 12
else
        log "Check if command '$JAVA_COMMAND' was found.....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$FIND_COMMAND" ]; then
        log "Check if command '$FIND_COMMAND' was found.....................[FAILED]"
        sendmail ERROR
        movelog
        exit 13
else
        log "Check if command '$FIND_COMMAND' was found.....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$TOUCH_COMMAND" ]; then
        log "Check if command '$TOUCH_COMMAND' was found....................[FAILED]"
        sendmail ERROR
        movelog
        exit 14
else
        log "Check if command '$TOUCH_COMMAND' was found....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$CP_COMMAND" ]; then
        log "Check if command '$CP_COMMAND' was found.......................[FAILED]"
        sendmail ERROR
        movelog
        exit 15
else
        log "Check if command '$CP_COMMAND' was found.......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$RM_COMMAND" ]; then
        log "Check if command '$RM_COMMAND' was found.......................[FAILED]"
        sendmail ERROR
        movelog
        exit 16
else
        log "Check if command '$RM_COMMAND' was found.......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$CAT_COMMAND" ]; then
        log "Check if command '$CAT_COMMAND' was found......................[FAILED]"
        sendmail ERROR
        movelog
        exit 17
else
        log "Check if command '$CAT_COMMAND' was found......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$DATE_COMMAND" ]; then
        log "Check if command '$DATE_COMMAND' was found.....................[FAILED]"
        sendmail ERROR
        movelog
        exit 18
else
        log "Check if command '$DATE_COMMAND' was found.....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$MKDIR_COMMAND" ]; then
        log "Check if command '$MKDIR_COMMAND' was found....................[FAILED]"
        sendmail ERROR
        movelog
        exit 19
else
        log "Check if command '$MKDIR_COMMAND' was found....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$PROG_SENDMAIL" ]; then
        log "Check if command '$PROG_SENDMAIL' was found................[FAILED]"
        sendmail ERROR
        movelog
        exit 20
else
        log "Check if command '$PROG_SENDMAIL' was found................[  OK  ]"
fi
 
# Check if LOCK file NOT exist.
if [ ! -e "$FILE_LOCK" ]; then
        log "Check if script is NOT already runnig .........................[  OK  ]"
 
        $TOUCH_COMMAND $FILE_LOCK
else
        log "Check if script is NOT already runnig .........................[FAILED]"
        log ""
        log "ERROR: The script was already running, or LOCK file already exists!"
        log ""
        sendmail ERROR
        movelog
        exit 20
fi
 
# Start build process
log ""
log "+-----------------------------------------------------------------+"
log "| Run tests process ............................................. |"
log "+-----------------------------------------------------------------+"
log ""
 
# Step 01: Make target directory.
# Check if TARGET_DIR exists. If yes delete it ande make it again. If no make it.
if [ ! -d "$TARGET_DIR" ]; then
        log "Check if TARGET_DIR exists.................................[FAILED]"
        $MKDIR_COMMAND -p $TARGET_DIR
        log "TARGET_DIR was now created.................................[  OK  ]"
else
        log "Check if DIR_BACKUP exists.................................[  OK  ]"
	$RM_COMMAND -rf $TARGET_DIR
        log "TARGET_DIR was now deleted.................................[  OK  ]"
        $MKDIR_COMMAND -p $TARGET_DIR
        log "TARGET_DIR was now created.................................[  OK  ]"
fi
 
# Step 02: Change into target directory.                            
log "Change into TARGET_DIR ....................................[  OK  ]"
cd $TARGET_DIR
 
# Step 03: Copy source directorys and files to target directory.
log "Copy SOURCE_DIR to TARGET_DIR .............................[  OK  ]"
$CP_COMMAND -ar $CUSTOM_DIR/$SOURCE_DIR $TARGET_DIR
 
# Step 04: Copy webapp directorys and files to target directory.
log "Copy WEBAPP_DIR to TARGET_DIR .............................[  OK  ]"
$CP_COMMAND -ar $CUSTOM_DIR/$WEBAPP_DIR $TARGET_DIR
 
# Step 05: Copy net folder and files to target directory.
log "Copy net folder to TARGET_DIR .............................[  OK  ]"
$CP_COMMAND -ar $TARGET_DIR$SOURCE_DIR/net $TARGET_DIR
 
# Step 06: Copy lib files to target directory.
log "Copy lib files to TARGET_DIR ..............................[  OK  ]"
$CP_COMMAND -ar $TARGET_DIR$WEBAPP_DIR/WEB-INF/lib/* $TARGET_DIR
 
# Step 07: Compile all classes inside target directory.
log "Compile all classes .......................................[  OK  ]"
$JAVAC_COMMAND -g:none -target 1.8 -cp .:*:/usr/share/tomcat/lib/*  net/tachtler/*/*.java
 
# Step 08: Run Tests.                                     
log "Run Tests .................................................[  OK  ]"
$JAVA_COMMAND -cp .:$TARGET_DIR/junit-4.12.jar:$TARGET_DIR/hamcrest-core-1.3.jar:$TARGET_DIR/* org.junit.runner.JUnitCore net.tachtler.tests.AllTests | tee $TARGET_DIR/junittest.result
 
# Delete LOCK file.
if [ "$?" != 0 ]; then
        retval $?
        log ""
        $RM_COMMAND -f $FILE_LOCK
        sendmail ERROR
        movelog
        exit 99
else
        log ""
        log "+-----------------------------------------------------------------+"
        log "| End tests process ............................................. |"
        log "+-----------------------------------------------------------------+"
        log ""
fi
 
# Finish syncing.
log "+-----------------------------------------------------------------+"
log "| Finish......................................................... |"
log "+-----------------------------------------------------------------+"
log ""
 
# Status e-mail.
if [ $MAIL_STATUS = 'Y' ]; then
        sendmail STATUS
fi
 
# Check the result of Failures.                  
if grep -Fxqi Failure $TARGET_DIR/junittest.result
then
	log "Check the result of failures ..............................[FOUND!]"
	log ""
	# Move temporary log to permanent log
	movelog
	exit 66
else
	log "Check the result of failures ..............................[  OK  ]"
	log ""
	movelog
fi
 
exit 0

Eine Besonderheit stellt die Verwendung einer Umgebungsvariable welche von Gitlab-CI (Continous Integration) zur Verfügung gestellt wird dar.

Weitere Informationen können unter nachfolgenden externen Link angerufen werden:

war_test_homepage.sh - Pipeline Ausgabe

Eine Ausgabe in etwa wie nachfolgende sollte erscheinen, wenn der „Runner“ die Gitlab-CI (Continous Integration) erfolgreich abgearbeitet hat, was nachfolgende Bildschirmkopie zeigt:

Projekt - CI /CD - Jobs - job_war_test_homepage

Directory Listing: Builds - WAR-Archiv

Um die durch die Skripte erzeugten Verzeichnisse und Dateien bequem in einem „Browser“ zur Anzeige zu bringen, oder herunterladen zu können, sind nachfolgende Anpassungen in der Gitlab - Omnibus - nginx-Konfiguration notwendig.

Siehe hierzu auch nachfolgende Dokumentation:

Beschreibung Externer Link
Inserting custom NGINX settings into the GitLab server block https://docs.gitlab.com/omnibus/settings/nginx.html#inserting-custom-nginx-settings-into-the-gitlab-server-block

Nachfolgende Ergänzungen sind in der Konfigurationsdatei der Gitlab - Omnibus Installation:

  • /etc/gitlab/gitlab.rb

durchzuführen.

:!: HINWEIS - Um die richtige Stelle innerhalb der Konfigurationsdatei zu finden, kann nach custom_gitlab_server_config gesucht werden, da hier eine Beispielkonfiguration vorhanden ist.

(Nur relevanter Ausschnitt)

# Tachtler
nginx['custom_gitlab_server_config'] = "location ^~ /www {\n    root /var/opt/gitlab/gitlab-runner;\n    autoindex on;\n    autoindex_localtime on;\n  }\n"

Diese Konfiguration erzeugt nach einer Neukonfiguration von Gitlab - Omnibus - nginx nachfolgende Zeilen in der Konfigurationsdatei

  • /var/opt/gitlab/nginx/conf/gitlab-http.conf

welche mit nachfolgendem Befehl ausgegeben werden können:

# tail -n 6 /var/opt/gitlab/nginx/conf/gitlab-http.conf | head -4
    root /var/opt/gitlab/gitlab-runner;
    autoindex on;
    autoindex_localtime on;
  }

Abschließend ist eine Neukonfiguration der Gitlab - Omnibus Installation erforderlich, was mit nachfolgendem Befehl durchgeführt werden kann:

# gitlab-ctl reconfigure
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: ["gitlab"]
Synchronizing Cookbooks:
  - mattermost (0.1.0)
  - postgresql (0.1.0)
  - gitaly (0.1.0)
  - package (0.1.0)
  - consul (0.0.0)
  - registry (0.1.0)
  - gitlab (0.0.1)
  - runit (0.14.2)
Installing Cookbook Gems:
Compiling Cookbooks...
...
...
...
Chef Client finished, 2/505 resources updated in 19 seconds
gitlab Reconfigured!

Ein Aufruf, hier als Beispiel mit nachfolgender URL:

sollte nun in etwa nachfolgendes Ergebnis zurück liefern:

Directory Listing: Builds

CI - JAR: Build *.jar

Nachfolgendes Beispiel zeigt den Einsatz der Gitlab-CI (Continous Integration), zur Erstellung eines Runnable *.jar-Archiv, welches NUR mithilfe der

  • Sourcen des Projekts „FooterMilter“ „gecloned“ aus dem lokalen Gitlab-Repository
  • Einbindung der Java OpenJDK-Bibliotheken

auskommt.

.gitlab-ci.yml - JAR-Archiv

Nachfolgende Konfigurationsdatei für die Gitlab-CI (Continous Integration) wird standardmäßig im Hauptverzeichnis im Projektverzeichnis hier: unter /jmilter-Footermilter erzeugt bzw. abgelegt und soll hier nachfolgenden Inhalt haben:

stages:
 - build
 - test
.job_jar_build:
  script: &jar_build
  - "sh  ${CI_PROJECT_DIR}/jmilter-FooterMilter/src/script/jar_build_footermilter.sh"
.job_jar_test:
  script: &jar_test
  - "sh  ${CI_PROJECT_DIR}/jmilter-FooterMilter/src/script/jar_test_footermilter.sh"
job_jar_build:
  stage: build
  script: *jar_build
job_jar_test:
  stage: test
  script: *jar_test

jar_build_footermilter.sh

Nachfolgendes Skript führt die eigentliche Erzeugung eines Runnable *.jar-Archivs durch.

Zur Vorbereitung der Ablage der Gitlab-CI-Ergebnisse in einem Verzeichnis im Dateisystem, kann nachfolgender Befehl verwendet werden:

# mkdir -p /var/opt/gitlab/gitlab-runner/www

Anschließend müssen noch die Datei- und Besitzrechte wie folgt angepasst werden, damit der Gitlab - Omnibus und später ggf. der nginx-Webserver Zugriff auf das Verzeichnis und die darunterliegenden Dateien hat, was mit nachfolgenden Befehlen durchgeführt werden kann:

# chmod 775 /var/opt/gitlab/gitlab-runner/www

und

# chown 775 gitlab-runner:gitlab-runner /var/opt/gitlab/gitlab-runner/www

Zur Überprüfung, ob die entsprechenden Datei- und Besitzrechte richtig gesetzt wurden, kann nachfolgender Befehl verwendet werden:

# ls -l -d /var/opt/gitlab/gitlab-runner/www
drwxrwxr-x 4 gitlab-runner gitlab-runner 53 Feb  2 07:19 /var/opt/gitlab/gitlab-runner/www

Anschließend wird das Skript mit nachfolgendem Inhalt erzeugt im Projekt, z.B. unter

  • /src/script

mit einem Editor erstellt und gespeichert:

  • [Pfad zum Projektordner]/src/script/jar_build_footermilter.sh

Nachfolgend eine Bildschirmkopie, eines Beispiels mit Eclipse (Eclipse IDE for Java EE Developers) erstellten Projektes, einer Runnable *.jar-Archivs (Nur Ausschnitt des „Project Explorer“s)

Eclipse IDE for Java EE Developers - Project Explorer - FooterMilter

#!/bin/bash
 
##############################################################################
# Script-Name : jar_build_footermilter.sh                                    #
# Description : Script to build a runnable jar application file.             #
#                                                                            #
#                                                                            #
#                                                                            #
#                                                                            #
# Last update : 07.12.2018                                                   #
# Version     : 1.00                                                         #
#                                                                            #
# Author      : Klaus Tachtler, <klaus@tachtler.net>                         #
# DokuWiki    : http://www.dokuwiki.tachtler.net                             #
# Homepage    : http://www.tachtler.net                                      #
#                                                                            #
#  +----------------------------------------------------------------------+  #
#  | This program is free software; you can redistribute it and/or modify |  #
#  | it under the terms of the GNU General Public License as published by |  #
#  | the Free Software Foundation; either version 2 of the License, or    |  #
#  | (at your option) any later version.                                  |  #
#  +----------------------------------------------------------------------+  #
#                                                                            #
# Copyright (c) 2018 by Klaus Tachtler.                                      #
#                                                                            #
##############################################################################
 
##############################################################################
#                                H I S T O R Y                               #
##############################################################################
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
#############################################################################
 
##############################################################################
# >>> Please edit following lines for personal settings and custom usages. ! #
##############################################################################
 
# CUSTOM - Script-Name.
SCRIPT_NAME='jar_build_footermilter'
 
# CUSTOM - Build-Environment settings.
JAVAC_PATH='./lib/*:* ./net/tachtler/jmilter/FooterMilter/*.java'
BUILD_PATH='META-INF/MANIFEST.MF net/tachtler/jmilter/FooterMilter/*.class'
CUSTOM_DIR=${CI_PROJECT_DIR}
SOURCE_DIR='/jmilter-FooterMilter/src'
LIBAPP_DIR='/jmilter-FooterMilter/lib'
TARGET_DIR='/var/opt/gitlab/gitlab-runner/www/footermilter-build/'
TARGET_JAR='FooterMilter.jar'
 
# CUSTOM - Mail-Recipient.
MAIL_RECIPIENT='user@example.com'
 
# CUSTOM - Status-Mail [Y|N].
MAIL_STATUS='N'
 
##############################################################################
# >>> Normaly there is no need to change anything below this comment line. ! #
##############################################################################
 
# Variables.
JAVAC_COMMAND=`command -v javac`
JAR_COMMAND=`command -v jar`
FIND_COMMAND=`command -v find`
TOUCH_COMMAND=`command -v touch`
CP_COMMAND=`command -v cp`
RM_COMMAND=`command -v rm`
CAT_COMMAND=`command -v cat`
DATE_COMMAND=`command -v date`
MKDIR_COMMAND=`command -v mkdir`
PROG_SENDMAIL=`command -v sendmail`
FILE_LOCK='/tmp/'$SCRIPT_NAME'.lock'
FILE_LOG=$TARGET_DIR$SCRIPT_NAME'.log'
FILE_LAST_LOG='/tmp/'$SCRIPT_NAME'.log'
FILE_MAIL='/tmp/'$SCRIPT_NAME'.mail'
VAR_HOSTNAME=`uname -n`
VAR_SENDER='root@'$VAR_HOSTNAME
VAR_EMAILDATE=`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%Z)'`
 
# Functions.
function log() {
        echo $1
        echo `$DATE_COMMAND '+%Y/%m/%d %H:%M:%S'` " INFO:" $1 >>${FILE_LAST_LOG}
}
 
function retval() {
if [ "$?" != "0" ]; then
        case "$?" in
        *)
                log "ERROR: Unknown error $?"
        ;;
        esac
fi
}
 
function movelog() {
        $CAT_COMMAND $FILE_LAST_LOG >> $FILE_LOG
        $RM_COMMAND -f $FILE_LAST_LOG   
        $RM_COMMAND -f $FILE_LOCK
}
 
function sendmail() {
        case "$1" in
        'STATUS')
                MAIL_SUBJECT='Status execution '$SCRIPT_NAME' script.'
        ;;
        *)
                MAIL_SUBJECT='ERROR while execution '$SCRIPT_NAME' script !!!'
        ;;
        esac
 
$CAT_COMMAND <<MAIL >$FILE_MAIL
Subject: $MAIL_SUBJECT
Date: $VAR_EMAILDATE
From: $VAR_SENDER
To: $MAIL_RECIPIENT
MAIL
 
$CAT_COMMAND $FILE_LAST_LOG >> $FILE_MAIL
 
$PROG_SENDMAIL -f $VAR_SENDER -t $MAIL_RECIPIENT < $FILE_MAIL
 
$RM_COMMAND -f $FILE_MAIL
 
}
 
# Main.
log ""
log "+-----------------------------------------------------------------+"
log "| Start build script to build a web application war file by hand. |"
log "+-----------------------------------------------------------------+"
log ""
log "Run script with following parameter:"
log ""
log "JAVAC_PATH....: $JAVAC_PATH"
log "BUILD_PATH....: $BUILD_PATH"
log "CUSTOM_DIR....: $CUSTOM_DIR"
log "SOURCE_DIR....: $SOURCE_DIR"
log "LIBAPP_DIR....: $LIBAPP_DIR"
log "TARGET_DIR....: $TARGET_DIR"
log "TARGET_JAR....: $TARGET_JAR"
log ""
log "MAIL_RECIPIENT: $MAIL_RECIPIENT"
log "MAIL_STATUS...: $MAIL_STATUS"
log ""
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$JAVAC_COMMAND" ]; then
        log "Check if command '$JAVAC_COMMAND' was found....................[FAILED]"
        sendmail ERROR
        movelog
        exit 11
else
        log "Check if command '$JAVAC_COMMAND' was found....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$JAR_COMMAND" ]; then
        log "Check if command '$JAR_COMMAND' was found......................[FAILED]"
        sendmail ERROR
        movelog
        exit 12
else
        log "Check if command '$JAR_COMMAND' was found......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$FIND_COMMAND" ]; then
        log "Check if command '$FIND_COMMAND' was found.....................[FAILED]"
        sendmail ERROR
        movelog
        exit 13
else
        log "Check if command '$FIND_COMMAND' was found.....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$TOUCH_COMMAND" ]; then
        log "Check if command '$TOUCH_COMMAND' was found....................[FAILED]"
        sendmail ERROR
        movelog
        exit 14
else
        log "Check if command '$TOUCH_COMMAND' was found....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$CP_COMMAND" ]; then
        log "Check if command '$CP_COMMAND' was found.......................[FAILED]"
        sendmail ERROR
        movelog
        exit 15
else
        log "Check if command '$CP_COMMAND' was found.......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$RM_COMMAND" ]; then
        log "Check if command '$RM_COMMAND' was found.......................[FAILED]"
        sendmail ERROR
        movelog
        exit 16
else
        log "Check if command '$RM_COMMAND' was found.......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$CAT_COMMAND" ]; then
        log "Check if command '$CAT_COMMAND' was found......................[FAILED]"
        sendmail ERROR
        movelog
        exit 17
else
        log "Check if command '$CAT_COMMAND' was found......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$DATE_COMMAND" ]; then
        log "Check if command '$DATE_COMMAND' was found.....................[FAILED]"
        sendmail ERROR
        movelog
        exit 18
else
        log "Check if command '$DATE_COMMAND' was found.....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$MKDIR_COMMAND" ]; then
        log "Check if command '$MKDIR_COMMAND' was found....................[FAILED]"
        sendmail ERROR
        movelog
        exit 19
else
        log "Check if command '$MKDIR_COMMAND' was found....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$PROG_SENDMAIL" ]; then
        log "Check if command '$PROG_SENDMAIL' was found................[FAILED]"
        sendmail ERROR
        movelog
        exit 20
else
        log "Check if command '$PROG_SENDMAIL' was found................[  OK  ]"
fi
 
# Check if LOCK file NOT exist.
if [ ! -e "$FILE_LOCK" ]; then
        log "Check if script is NOT already runnig .........................[  OK  ]"
 
        $TOUCH_COMMAND $FILE_LOCK
else
        log "Check if script is NOT already runnig .........................[FAILED]"
        log ""
        log "ERROR: The script was already running, or LOCK file already exists!"
        log ""
        sendmail ERROR
        movelog
        exit 20
fi
 
# Start build process
log ""
log "+-----------------------------------------------------------------+"
log "| Run build process ............................................. |"
log "+-----------------------------------------------------------------+"
log ""
 
# Step 01: Make target directory.
# Check if TARGET_DIR exists. If yes delete it ande make it again. If no make it.
if [ ! -d "$TARGET_DIR" ]; then
        log "Check if TARGET_DIR exists.................................[FAILED]"
        $MKDIR_COMMAND -p $TARGET_DIR
        log "TARGET_DIR was now created.................................[  OK  ]"
else
        log "Check if TARGET_DIR exists.................................[  OK  ]"
        $RM_COMMAND -rf $TARGET_DIR
        log "TARGET_DIR was now deleted.................................[  OK  ]"
        $MKDIR_COMMAND -p $TARGET_DIR
        log "TARGET_DIR was now created.................................[  OK  ]"
fi
 
# Step 02: Change into target directory.                            
log "Change into TARGET_DIR ....................................[  OK  ]"
cd $TARGET_DIR
 
# Step 03: Copy source directorys and files to target directory.
log "Copy SOURCE_DIR to TARGET_DIR .............................[  OK  ]"
$CP_COMMAND -ar $CUSTOM_DIR/$SOURCE_DIR/* $TARGET_DIR
 
# Step 04: Copy libapp directorys and files to target directory.
log "Copy LIBAPP_DIR to TARGET_DIR .............................[  OK  ]"
$CP_COMMAND -ar $CUSTOM_DIR/$LIBAPP_DIR $TARGET_DIR
 
# Step 05: Compile the java files to class files.
log "Compile *.java in TARGET_DIR to *.class ...................[  OK  ]"
$JAVAC_COMMAND -g:none -target 1.8 -cp $JAVAC_PATH
 
# Step 06: Make jar file.
log "Make *.jar file from TARGET_DIR ...........................[  OK  ]"
cd $TARGET_DIR
$JAR_COMMAND cvfm $TARGET_JAR $BUILD_PATH
 
# Delete LOCK file.
if [ "$?" != 0 ]; then
        retval $?
        log ""
        $RM_COMMAND -f $FILE_LOCK
        sendmail ERROR
        movelog
        exit 99
else
        log ""
        log "+-----------------------------------------------------------------+"
        log "| End build process ............................................. |"
        log "+-----------------------------------------------------------------+"
        log ""
fi
 
# Finish syncing.
log "+-----------------------------------------------------------------+"
log "| Finish......................................................... |"
log "+-----------------------------------------------------------------+"
log ""
 
# Status e-mail.
if [ $MAIL_STATUS = 'Y' ]; then
        sendmail STATUS
fi
# Move temporary log to permanent log
movelog
 
exit 0

Eine Besonderheit stellt die Verwendung einer Umgebungsvariable welche von Gitlab-CI (Continous Integration) zur Verfügung gestellt wird dar.

Weitere Informationen können unter nachfolgenden externen Link angerufen werden:

jar_build_footermilter.sh - Pipeline Ausgabe

Eine Ausgabe in etwa wie nachfolgende sollte erscheinen, wenn der „Runner“ die Gitlab-CI (Continous Integration) erfolgreich abgearbeitet hat, was nachfolgende Bildschirmkopie zeigt:

Projekt - CI /CD - Jobs - job_jar_build_footermilter

jar_test_footermilter.sh

Nachfolgendes Skript führt einen Aufruf-Tests mit dem *.jar-Archiv durch.

Zur Vorbereitung der Ablage der Gitlab-CI-Ergebnisse in einem Verzeichnis im Dateisystem, kann nachfolgender Befehl verwendet werden (Falls die Anlage des Verzeichnisses nicht schon durchgeführt wurde !!!):

# mkdir -p /var/opt/gitlab/gitlab-runner/www

Anschließend müssen noch die Datei- und Besitzrechte wie folgt angepasst werden, damit der Gitlab - Omnibus und später ggf. der nginx-Webserver Zugriff auf das Verzeichnis und die darunterliegenden Dateien hat, was mit nachfolgenden Befehlen durchgeführt werden kann:

# chmod 775 /var/opt/gitlab/gitlab-runner/www

und

# chown 775 gitlab-runner:gitlab-runner /var/opt/gitlab/gitlab-runner/www

Zur Überprüfung, ob die entsprechenden Datei- und Besitzrechte richtig gesetzt wurden, kann nachfolgender Befehl verwendet werden:

# ls -l -d /var/opt/gitlab/gitlab-runner/www
drwxrwxr-x 4 gitlab-runner gitlab-runner 53 Feb  2 07:19 /var/opt/gitlab/gitlab-runner/www

Anschließend wird das Skript mit nachfolgendem Inhalt erzeugt im Projekt, z.B. unter

  • /src/script

mit einem Editor erstellt und gespeichert:

  • [Pfad zum Projektordner]/src/script/jar_test_footermilter.sh

Nachfolgend eine Bildschirmkopie, eines Beispiels mit Eclipse (Eclipse IDE for Java EE Developers) erstellten Projektes, einer Apache Tomcat-Anwendung (Nur Ausschnitt des „Project Explorer“s)

Eclipse IDE for Java EE Developers - Project Explorer - FooterMilter

#!/bin/bash
 
##############################################################################
# Script-Name : jar_test_footermilter.sh                                     #
# Description : Script to test a web application java classes with.          #
#                                                                            #
#                                                                            #
#                                                                            #
#                                                                            #
# Last update : 07.12.2018                                                   #
# Version     : 1.00                                                         #
#                                                                            #
# Author      : Klaus Tachtler, <klaus@tachtler.net>                         #
# DokuWiki    : http://www.dokuwiki.tachtler.net                             #
# Homepage    : http://www.tachtler.net                                      #
#                                                                            #
#  +----------------------------------------------------------------------+  #
#  | This program is free software; you can redistribute it and/or modify |  #
#  | it under the terms of the GNU General Public License as published by |  #
#  | the Free Software Foundation; either version 2 of the License, or    |  #
#  | (at your option) any later version.                                  |  #
#  +----------------------------------------------------------------------+  #
#                                                                            #
# Copyright (c) 2018 by Klaus Tachtler.                                      #
#                                                                            #
##############################################################################
 
##############################################################################
#                                H I S T O R Y                               #
##############################################################################
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
# Version     : x.xx                                                         #
# Description : <Description>                                                #
# -------------------------------------------------------------------------- #
#############################################################################
 
##############################################################################
# >>> Please edit following lines for personal settings and custom usages. ! #
##############################################################################
 
# CUSTOM - Script-Name.
SCRIPT_NAME='jar_test_footermilter'
 
# CUSTOM - Build-Environment settings.
CUSTOM_DIR=${CI_PROJECT_DIR}
JARAPP_DIR='/var/opt/gitlab/gitlab-runner/www/footermilter-build'
TARGET_DIR='/var/opt/gitlab/gitlab-runner/www/footermilter-test'
TARGET_JAR='FooterMilter.jar'
 
# CUSTOM - Mail-Recipient.
MAIL_RECIPIENT='user@example.com'
 
# CUSTOM - Status-Mail [Y|N].
MAIL_STATUS='N'
 
##############################################################################
# >>> Normaly there is no need to change anything below this comment line. ! #
##############################################################################
 
# Variables.
JAVAC_COMMAND=`command -v javac`
JAVA_COMMAND=`command -v java`
FIND_COMMAND=`command -v find`
TOUCH_COMMAND=`command -v touch`
CP_COMMAND=`command -v cp`
RM_COMMAND=`command -v rm`
CAT_COMMAND=`command -v cat`
DATE_COMMAND=`command -v date`
MKDIR_COMMAND=`command -v mkdir`
PROG_SENDMAIL=`command -v sendmail`
FILE_LOCK='/tmp/'$SCRIPT_NAME'.lock'
FILE_LOG=$TARGET_DIR$SCRIPT_NAME'.log'
FILE_LAST_LOG='/tmp/'$SCRIPT_NAME'.log'
FILE_MAIL='/tmp/'$SCRIPT_NAME'.mail'
VAR_HOSTNAME=`uname -n`
VAR_SENDER='root@'$VAR_HOSTNAME
VAR_EMAILDATE=`$DATE_COMMAND '+%a, %d %b %Y %H:%M:%S (%Z)'`
 
# Functions.
function log() {
        echo $1
        echo `$DATE_COMMAND '+%Y/%m/%d %H:%M:%S'` " INFO:" $1 >>${FILE_LAST_LOG}
}
 
function retval() {
if [ "$?" != "0" ]; then
        case "$?" in
        *)
                log "ERROR: Unknown error $?"
        ;;
        esac
fi
}
 
function movelog() {
        $CAT_COMMAND $FILE_LAST_LOG >> $FILE_LOG
        $RM_COMMAND -f $FILE_LAST_LOG   
        $RM_COMMAND -f $FILE_LOCK
}
 
function sendmail() {
        case "$1" in
        'STATUS')
                MAIL_SUBJECT='Status execution '$SCRIPT_NAME' script.'
        ;;
        *)
                MAIL_SUBJECT='ERROR while execution '$SCRIPT_NAME' script !!!'
        ;;
        esac
 
$CAT_COMMAND <<MAIL >$FILE_MAIL
Subject: $MAIL_SUBJECT
Date: $VAR_EMAILDATE
From: $VAR_SENDER
To: $MAIL_RECIPIENT
MAIL
 
$CAT_COMMAND $FILE_LAST_LOG >> $FILE_MAIL
 
$PROG_SENDMAIL -f $VAR_SENDER -t $MAIL_RECIPIENT < $FILE_MAIL
 
$RM_COMMAND -f $FILE_MAIL
 
}
 
# Main.
log ""
log "+-----------------------------------------------------------------+"
log "| Start test script to run junit web tests on commandline by hand.|"
log "+-----------------------------------------------------------------+"
log ""
log "Run script with following parameter:"
log ""
log "CUSTOM_DIR....: $CUSTOM_DIR"
log "JARAPP_DIR....: $JARAPP_DIR"
log "TARGET_DIR....: $TARGET_DIR"
log "TARGET_JAR....: $TARGET_JAR"
log ""
log "MAIL_RECIPIENT: $MAIL_RECIPIENT"
log "MAIL_STATUS...: $MAIL_STATUS"
log ""
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$JAVAC_COMMAND" ]; then
        log "Check if command '$JAVAC_COMMAND' was found....................[FAILED]"
        sendmail ERROR
        movelog
        exit 11
else
        log "Check if command '$JAVAC_COMMAND' was found....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$JAVA_COMMAND" ]; then
        log "Check if command '$JAVA_COMMAND' was found.....................[FAILED]"
        sendmail ERROR
        movelog
        exit 12
else
        log "Check if command '$JAVA_COMMAND' was found.....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$FIND_COMMAND" ]; then
        log "Check if command '$FIND_COMMAND' was found.....................[FAILED]"
        sendmail ERROR
        movelog
        exit 13
else
        log "Check if command '$FIND_COMMAND' was found.....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$TOUCH_COMMAND" ]; then
        log "Check if command '$TOUCH_COMMAND' was found....................[FAILED]"
        sendmail ERROR
        movelog
        exit 14
else
        log "Check if command '$TOUCH_COMMAND' was found....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$CP_COMMAND" ]; then
        log "Check if command '$CP_COMMAND' was found.......................[FAILED]"
        sendmail ERROR
        movelog
        exit 15
else
        log "Check if command '$CP_COMMAND' was found.......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$RM_COMMAND" ]; then
        log "Check if command '$RM_COMMAND' was found.......................[FAILED]"
        sendmail ERROR
        movelog
        exit 16
else
        log "Check if command '$RM_COMMAND' was found.......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$CAT_COMMAND" ]; then
        log "Check if command '$CAT_COMMAND' was found......................[FAILED]"
        sendmail ERROR
        movelog
        exit 17
else
        log "Check if command '$CAT_COMMAND' was found......................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$DATE_COMMAND" ]; then
        log "Check if command '$DATE_COMMAND' was found.....................[FAILED]"
        sendmail ERROR
        movelog
        exit 18
else
        log "Check if command '$DATE_COMMAND' was found.....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$MKDIR_COMMAND" ]; then
        log "Check if command '$MKDIR_COMMAND' was found....................[FAILED]"
        sendmail ERROR
        movelog
        exit 19
else
        log "Check if command '$MKDIR_COMMAND' was found....................[  OK  ]"
fi
 
# Check if command (file) NOT exist OR IS empty.
if [ ! -s "$PROG_SENDMAIL" ]; then
        log "Check if command '$PROG_SENDMAIL' was found................[FAILED]"
        sendmail ERROR
        movelog
        exit 20
else
        log "Check if command '$PROG_SENDMAIL' was found................[  OK  ]"
fi
 
# Check if LOCK file NOT exist.
if [ ! -e "$FILE_LOCK" ]; then
        log "Check if script is NOT already runnig .........................[  OK  ]"
 
        $TOUCH_COMMAND $FILE_LOCK
else
        log "Check if script is NOT already runnig .........................[FAILED]"
        log ""
        log "ERROR: The script was already running, or LOCK file already exists!"
        log ""
        sendmail ERROR
        movelog
        exit 20
fi
 
# Start build process
log ""
log "+-----------------------------------------------------------------+"
log "| Run tests process ............................................. |"
log "+-----------------------------------------------------------------+"
log ""
 
# Step 01: Make target directory.
# Check if TARGET_DIR exists. If yes delete it ande make it again. If no make it.
if [ ! -d "$TARGET_DIR" ]; then
        log "Check if TARGET_DIR exists.................................[FAILED]"
        $MKDIR_COMMAND -p $TARGET_DIR
        log "TARGET_DIR was now created.................................[  OK  ]"
else
        log "Check if TARGET_DIR exists.................................[  OK  ]"
        $RM_COMMAND -rf $TARGET_DIR
        log "TARGET_DIR was now deleted.................................[  OK  ]"
        $MKDIR_COMMAND -p $TARGET_DIR
        log "TARGET_DIR was now created.................................[  OK  ]"
fi
 
# Step 02: Change into target directory.                            
log "Change into TARGET_DIR ....................................[  OK  ]"
cd $TARGET_DIR
 
# Step 03: Copy jarapp directorys and files to target directory.
log "Copy JARAPP_DIR to TARGET_DIR .............................[  OK  ]"
$CP_COMMAND -ar $JARAPP_DIR/* $TARGET_DIR
 
# Step 04: Test the TARGET_JAR.
log "Test the TARGET_JAR .......................................[  OK  ]"
$JAVA_COMMAND -jar $TARGET_JAR -h > jartest.result
 
# Delete LOCK file.
if [ "$?" != 0 ]; then
        retval $?
        log ""
        $RM_COMMAND -f $FILE_LOCK
        sendmail ERROR
        movelog
        exit 99
else
        log ""
        log "+-----------------------------------------------------------------+"
        log "| End tests process ............................................. |"
        log "+-----------------------------------------------------------------+"
        log ""
fi
 
# Finish syncing.
log "+-----------------------------------------------------------------+"
log "| Finish......................................................... |"
log "+-----------------------------------------------------------------+"
log ""
 
# Status e-mail.
if [ $MAIL_STATUS = 'Y' ]; then
        sendmail STATUS
fi
 
# Check the result of Failures.
$CAT_COMMAND $TARGET_DIR/jartest.result                  
if grep -Fxqi Exception $TARGET_DIR/jartest.result
then
        log "Check the result of failures ..............................[FOUND!]"
        log ""
        # Move temporary log to permanent log
        movelog
        exit 66
else
        log "Check the result of failures ..............................[  OK  ]"
        log ""
        movelog
fi
 
exit 0

Eine Besonderheit stellt die Verwendung einer Umgebungsvariable welche von Gitlab-CI (Continous Integration) zur Verfügung gestellt wird dar.

Weitere Informationen können unter nachfolgenden externen Link angerufen werden:

jar_test_footermilter.sh - Pipeline Ausgabe

Eine Ausgabe in etwa wie nachfolgende sollte erscheinen, wenn der „Runner“ die Gitlab-CI (Continous Integration) erfolgreich abgearbeitet hat, was nachfolgende Bildschirmkopie zeigt: Projekt - CI /CD - Jobs - job_jar_test_footermilter

Directory Listing: Builds - JAR-Archiv

Um die durch die Skripte erzeugten Verzeichnisse und Dateien bequem in einem „Browser“ zur Anzeige zu bringen, oder herunterladen zu können, sind nachfolgende Anpassungen in der Gitlab - Omnibus - nginx-Konfiguration notwendig.

Siehe hierzu auch nachfolgende Dokumentation:

Beschreibung Externer Link
Inserting custom NGINX settings into the GitLab server block https://docs.gitlab.com/omnibus/settings/nginx.html#inserting-custom-nginx-settings-into-the-gitlab-server-block

Nachfolgende Ergänzungen sind in der Konfigurationsdatei der Gitlab - Omnibus Installation:

  • /etc/gitlab/gitlab.rb

durchzuführen.

:!: HINWEIS - Um die richtige Stelle innerhalb der Konfigurationsdatei zu finden, kann nach custom_gitlab_server_config gesucht werden, da hier eine Beispielkonfiguration vorhanden ist.

(Nur relevanter Ausschnitt)

# Tachtler
nginx['custom_gitlab_server_config'] = "location ^~ /www {\n    root /var/opt/gitlab/gitlab-runner;\n    autoindex on;\n    autoindex_localtime on;\n  }\n"

Diese Konfiguration erzeugt nach einer Neukonfiguration von Gitlab - Omnibus - nginx nachfolgende Zeilen in der Konfigurationsdatei

  • /var/opt/gitlab/nginx/conf/gitlab-http.conf

welche mit nachfolgendem Befehl ausgegeben werden können:

# tail -n 6 /var/opt/gitlab/nginx/conf/gitlab-http.conf | head -4
    root /var/opt/gitlab/gitlab-runner;
    autoindex on;
    autoindex_localtime on;
  }

Abschließend ist eine Neukonfiguration der Gitlab - Omnibus Installation erforderlich, was mit nachfolgendem Befehl durchgeführt werden kann:

# gitlab-ctl reconfigure
Starting Chef Client, version 12.12.15
resolving cookbooks for run list: ["gitlab"]
Synchronizing Cookbooks:
  - mattermost (0.1.0)
  - postgresql (0.1.0)
  - gitaly (0.1.0)
  - package (0.1.0)
  - consul (0.0.0)
  - registry (0.1.0)
  - gitlab (0.0.1)
  - runit (0.14.2)
Installing Cookbook Gems:
Compiling Cookbooks...
...
...
...
Chef Client finished, 2/505 resources updated in 19 seconds
gitlab Reconfigured!

Ein Aufruf, hier als Beispiel mit nachfolgender URL:

sollte nun in etwa nachfolgendes Ergebnis zurück liefern:

Directory Listing: Builds - FooterMilter

Diese Website verwendet Cookies. Durch die Nutzung der Website stimmen Sie dem Speichern von Cookies auf Ihrem Computer zu. Außerdem bestätigen Sie, dass Sie unsere Datenschutzbestimmungen gelesen und verstanden haben. Wenn Sie nicht einverstanden sind, verlassen Sie die Website.Weitere Information
tachtler/gitlab_centos_7_-_ci_cd.txt · Zuletzt geändert: 2018/12/08 07:48 von klaus