1
0
mirror of https://github.com/avatao-content/test-tutorial-framework synced 2024-07-05 14:38:47 +00:00
test-tutorial-framework/hack/tfw.sh
2018-05-24 14:43:15 +02:00

190 lines
5.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eu
set -o pipefail
set -o errtrace
shopt -s expand_aliases
[ "$(uname)" == "Darwin" ] && alias readlink="greadlink" || :
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
TAO_PATH="${TAO_PATH:-$SCRIPT_DIR/../..}"
BASEIMAGE_REPO="${BASEIMAGE_REPO:-baseimage-tutorial-framework}"
TEST_REPO="${TEST_REPO:-"$(basename "$(realpath "${SCRIPT_DIR}/..")")"}"
FRONTEND_REPO="${FRONTEND_REPO:-frontend-tutorial-framework}"
BASEIMAGE_PATH="${TAO_PATH}/${BASEIMAGE_REPO}"
TEST_PATH="${TAO_PATH}/${TEST_REPO}"
FRONTEND_PATH="${TAO_PATH}/${FRONTEND_REPO}"
BASEIMAGE_NAME="${BASEIMAGE_NAME:-eu.gcr.io/avatao-challengestore/tutorial-framework}"
IMAGE_NAME="${IMAGE_NAME:-test-tutorial-framework}"
TEST_PORT="${TEST_PORT:-8888}"
AVATAO_SECRET="${AVATAO_SECRET:-secret}"
BUILD_CONTEXT="${BUILD_CONTEXT:-solvable}"
build_baseimage()
{
[ ! -d "$BASEIMAGE_PATH" ] && return || :
cd $BASEIMAGE_PATH
docker build -t "${BASEIMAGE_NAME}:${TFWTAG:-$(baseimage_releasename)}" \
-t "${BASEIMAGE_NAME}:latest" \
.
}
release_baseimage()
{
[ ! -d "$BASEIMAGE_PATH" ] && return || :
cd $BASEIMAGE_PATH
check_drone_releasename
TAG="$(baseimage_releasename)"
read -p "Tag and push new TFW version \"${TAG}\"? [y/N]" -r && echo
if [[ $REPLY =~ ^(y|Y|yes|Yes|YES)$ ]]
then
description=""
prompt_for_tag_description # This command overwrites $description
TAG="$TAG" DESCRIPTION="$description" force_push_tag
fi
}
check_drone_releasename()
{
DRONEYML_RELEASENAME="$(grep -oP '(?<=branch:\srefs/tags/).+(?=-.+)' .drone.yml)"
ACTUAL_RELEASENAME="$(baseimage_releasename | grep -oP ".+(?=-\d{8})")"
if [ "$DRONEYML_RELEASENAME" != "$ACTUAL_RELEASENAME" ]; then
echo "Release name '${DRONEYML_RELEASENAME}' in .drone.yml does not match actual release name '${ACTUAL_RELEASENAME}' in VERSION."
echo "Please make them match before releasing!"
exit 1
fi
}
prompt_for_tag_description()
{
editor="$(git config --global core.editor)"
tempfile="$(mktemp)"
printf "\n\n" >> "$tempfile"
echo "# Please enter the description for this release." >> "$tempfile"
echo "# Lines starting with '#' will be ignored." >> "$tempfile"
echo "# An empty description aborts the release." >> "$tempfile"
$editor "$tempfile"
sed -i -e 's/#.*$//' -e '/^$/d' "$tempfile"
sed -i 's/\n/ /g' "$tempfile"
if [[ ! -s "$tempfile" ]]; then
echo "Aborting release due to empty description."
exit 1
fi
description="$(tr '\n' ' ' < "$tempfile")"
rm "$tempfile"
}
force_push_tag()
{
git tag -d "$TAG" > /dev/null 2>&1 || :
git push --delete origin "$TAG" > /dev/null 2>&1 || :
git tag -s -m "$DESCRIPTION" "$TAG"
git push origin "$TAG"
}
baseimage_releasename()
{
VERSION="$(cat VERSION | head -n 1)"
DATE="$(date +%Y%m%d)"
printf "${VERSION}-${DATE}"
}
build_test_internal()
{
build_baseimage
cd $TEST_PATH
docker build -t $IMAGE_NAME \
-f ${BUILD_CONTEXT}/Dockerfile \
--build-arg BUILD_CONTEXT=$BUILD_CONTEXT \
${ARGS} .
}
build_test()
{
ARGS="--build-arg NOFRONTEND=1" build_test_internal
}
build_test_withfrontend()
{
ARGS="--no-cache" build_test_internal
}
run_test()
{
cd "$TAO_PATH"
if [ "${HOTRELOAD:-0}" == "1" ]; then
[ -d "$BASEIMAGE_PATH" ] && mount_baseimage="-v ${BASEIMAGE_PATH}/lib/tfw:/usr/local/lib/tfw"
mount_test="-v ${TEST_PATH}/solvable/src:/srv/.tfw"
mount_volumes="${mount_baseimage:-} ${mount_test}"
fi
docker run --rm \
-p $TEST_PORT:$TEST_PORT \
${mount_volumes:-} \
${@:-} \
-e AVATAO_SECRET=$AVATAO_SECRET $IMAGE_NAME
}
run_frontend()
{
FRONTEND_IN_TEST="${TEST_PATH}/solvable/frontend"
if [ "$(find "$FRONTEND_IN_TEST" | wc -l)" -gt 2 ]
then
cd $FRONTEND_IN_TEST
else
cd $FRONTEND_PATH
fi
yarn start
}
start_test()
{
trap 'exit' INT TERM
trap 'kill 0' EXIT
[[ "${RUN_FRONTEND:-1}" == "1" ]] && run_frontend &
[[ "${BUILD:-1}" == "1" ]] && build_test
run_test $@
wait
}
case ${1:-} in
start)
RUN_FRONTEND=1 start_test ${@:2}
;;
run)
BUILD=0 RUN_FRONTEND=1 start_test ${@:2}
;;
startcontainer)
RUN_FRONTEND=0 start_test ${@:2}
;;
runcontainer)
BUILD=0 RUN_FRONTEND=0 start_test ${@:2}
;;
buildtfw)
build_baseimage
;;
build)
build_test
;;
buildwithfrontend)
build_test_withfrontend
;;
releasetfw)
release_baseimage
;;
*)
echo "Usage: tfw.sh [start|buildtfw|build|buildwithfrontend|releasetfw]"
echo " |--- start: build & run TFW test and Angular frontend"
echo " |--- run: run TFW test and Angular frontend"
echo " |--- startcontainer: build & run TFW test, container only"
echo " |--- runcontainer: run TFW test, container only"
echo " |--- buildtfw: build TFW baseimage"
echo " |--- build: build TFW baseimage and test"
echo " |--- buildwithfrontend: build TFW baseimage and test, include frontend in image"
echo " |--- releasetfw: tag TFW baseimage and push to upstream"
;;
esac