1
0
mirror of https://github.com/avatao-content/test-tutorial-framework synced 2024-11-14 22:07:17 +00:00
test-tutorial-framework/hack/tfw.sh

190 lines
5.5 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2018-04-16 18:45:46 +00:00
set -eu
2018-04-15 19:50:43 +00:00
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}"
2018-04-16 14:01:57 +00:00
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" \
2018-03-31 18:54:12 +00:00
.
}
release_baseimage()
{
[ ! -d "$BASEIMAGE_PATH" ] && return || :
cd $BASEIMAGE_PATH
2018-04-16 14:56:54 +00:00
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
}
2018-04-16 14:56:54 +00:00
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"
}
2018-04-16 15:16:16 +00:00
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"
2018-04-16 15:16:16 +00:00
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
2018-03-31 18:54:12 +00:00
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()
{
2018-04-19 15:01:26 +00:00
cd "$TAO_PATH"
2018-05-14 10:45:21 +00:00
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
2018-04-19 15:01:26 +00:00
docker run --rm \
-p $TEST_PORT:$TEST_PORT \
2018-05-14 10:45:21 +00:00
${mount_volumes:-} \
${@:-} \
2018-03-31 18:54:12 +00:00
-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
}
2018-04-19 15:01:59 +00:00
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
;;
*)
2018-04-28 09:28:22 +00:00
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