Rework release tooling

This commit is contained in:
Kristóf Tóth 2019-09-30 15:08:04 +02:00
parent 5e1c763dab
commit 5da0af26a0
4 changed files with 81 additions and 108 deletions

View File

@ -8,112 +8,33 @@ libhack_dir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
source "${libhack_dir}/common.sh" source "${libhack_dir}/common.sh"
baseimage::build_as_latest() { baseimage::assert_exists() {
pushd "${BASEIMAGE_PATH}" if [[ ! -d "${BASEIMAGE_PATH}" ]]; then
baseimage::build exit 0
baseimage::tag_as_latest
popd
}
baseimage::build() {
pushd "${BASEIMAGE_PATH}"
docker build -t "${BASEIMAGE_NAME}:$(releasename)" \
.
popd
}
baseimage::build_if_exists() {
if [[ -d "$BASEIMAGE_PATH" ]]; then
baseimage::build_as_latest
fi fi
} }
baseimage::tag_as_latest() { baseimage::build_if_exists() {
docker tag "${BASEIMAGE_NAME}:$(releasename)" "${BASEIMAGE_NAME}:latest" [[ ! -d "${BASEIMAGE_PATH}" ]] && return ||:
pushd "${BASEIMAGE_PATH}"
local tag
tag="${BASEIMAGE_NAME}:$(releasename)"
docker build -t "${tag}" .
docker tag "${tag}" "${BASEIMAGE_NAME}:latest"
popd
} }
baseimage::release() { baseimage::release() {
pushd "${BASEIMAGE_PATH}" pushd "${BASEIMAGE_PATH}"
check_drone_releasename
local tag local tag
tag="$(releasename)" tag="$(releasename)"
read -p "Tag and push new TFW version \"${tag}\"? [y/N]" -r && echo read -p "Tag and push baseimage \"${tag}\"? [y/N]" -r && echo
if [[ $REPLY =~ ^(y|Y|yes|Yes|YES)$ ]]; then if [[ $REPLY =~ ^(y|Y|yes|Yes|YES)$ ]]; then
prompt_for_tag_description # This command sets $description printf "Pushing git tag to upstream...\n"
tag="${tag}" description="${description}" force_push_tag tag="${tag}" description="${tag}" force_push_tag
printf "\nPushing image to DockerHub...\n"
docker push "${BASEIMAGE_NAME}:${tag}"
docker push "${BASEIMAGE_NAME}:latest"
fi fi
popd popd
} }
baseimage::builddocs() {
mount_point="/mnt/docs"
docker run --rm \
-ti \
-v "${BASEIMAGE_PATH}"/docs:"${mount_point}" \
"${BASEIMAGE_NAME}" \
/bin/bash -c \
"cd ${mount_point}
pip3 install sphinx
make html
exit" \
&> /dev/null
if [ "$?" == "0" ]; then
echo "Built baseimage docs at ${BASEIMAGE_PATH}/docs/build/html!"
else
echo "Building baseimage docs failed!"
fi
}
releasename() {
local version
local date
version="$(cat VERSION | head -n 1)"
date="$(date +%Y%m%d)"
printf "${version}-${date}"
}
check_drone_releasename() {
local drone_releasename
local actual_releasename
drone_releasename="$(grep -oP '(?<=branch:\srefs/tags/).+(?=-.+)' .drone.yml)"
actual_releasename="$(releasename | grep -oP ".+(?=-\d{8})")"
if [ "${drone_releasename}" != "${actual_releasename}" ]; then
echo "Release name '${drone_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() {
local editor
local tempfile
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}"
}

View File

@ -5,3 +5,18 @@ pushd() {
popd() { popd() {
command popd "$@" > /dev/null command popd "$@" > /dev/null
} }
releasename() {
local version
local date
version="$(cat VERSION | head -n 1)"
date="$(date +%Y%m%d)"
printf "${version}-${date}"
}
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}"
}

34
hack/libhack/frontend.sh Normal file
View File

@ -0,0 +1,34 @@
# Requires context:
# - FRONTEND_PATH: absolute path of frontend repo
FRONTEND_IMAGE_NAME="${FRONTEND_IMAGE_NAME:-avatao/frontend-tutorial-framework}"
libhack_dir="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
source "${libhack_dir}/common.sh"
frontend::assert_exists() {
if [[ ! -d "${FRONTEND_PATH}" ]]; then
exit 0
fi
}
frontend::build() {
pushd "${FRONTEND_PATH}"
docker build -t "${FRONTEND_IMAGE_NAME}:$(releasename)" .
popd
}
frontend::release() {
pushd "${FRONTEND_PATH}"
local tag
tag="$(releasename)"
read -p "Tag and push frontend \"${tag}\"? [y/N]" -r && echo
if [[ $REPLY =~ ^(y|Y|yes|Yes|YES)$ ]]; then
printf "Pushing git tag to upstream...\n"
tag="${tag}" description="${tag}" force_push_tag
printf "\nPushing image to DockerHub...\n"
docker push "${FRONTEND_IMAGE_NAME}:${tag}"
fi
popd
}

View File

@ -18,6 +18,7 @@ FRONTEND_PATH="${TFW_PATH}/${FRONTEND_REPO}"
source "${SCRIPT_DIR}/libhack/baseimage.sh" source "${SCRIPT_DIR}/libhack/baseimage.sh"
source "${SCRIPT_DIR}/libhack/challenge.sh" source "${SCRIPT_DIR}/libhack/challenge.sh"
source "${SCRIPT_DIR}/libhack/frontend.sh"
case ${1:-} in case ${1:-} in
@ -29,30 +30,32 @@ case ${1:-} in
run) run)
challenge:run ${@:2} challenge:run ${@:2}
;; ;;
buildtfw) build-baseimage)
baseimage::build_if_exists baseimage::build_if_exists
;; ;;
build) build)
baseimage::build_if_exists baseimage::build_if_exists
challenge::build_no_cache challenge::build_no_cache
;; ;;
releasetfw) build-frontend)
if [[ -d "$BASEIMAGE_PATH" ]]; then frontend::assert_exists
baseimage::release frontend::build
fi
;; ;;
builddocs) release-baseimage)
if [[ -d "$BASEIMAGE_PATH" ]]; then baseimage::assert_exists
baseimage::builddocs baseimage::release
fi ;;
release-frontend)
frontend::assert_exists
frontend::release
;; ;;
*) *)
echo "Usage: tfw.sh [COMMAND]" echo "Usage: tfw.sh [COMMAND]"
echo " |--- start: build & run TFW challenge" echo " |--- start: build & run TFW challenge"
echo " |--- run: run TFW challenge" echo " |--- run: run TFW challenge"
echo " |--- buildtfw: build TFW baseimage" echo " |--- build-baseimage: build TFW baseimage"
echo " |--- build: build TFW baseimage and challenge" echo " |--- build: build TFW baseimage and challenge"
echo " |--- releasetfw: tag TFW baseimage and push to upstream" echo " |--- release-baseimage: tag TFW baseimage and push to DockerHub"
echo " |--- builddocs: build baseimage documentation (in docs/build/html)" echo " |--- release-frontend: tag TFW frontend and push to DockerHub"
;; ;;
esac esac