Files
qaffee/build.sh
2026-04-28 13:22:26 +02:00

115 lines
3.4 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# ---------------------------------------------------------------------------
# build.sh Build and push Docker images
#
# Usage:
# ./build.sh [OPTIONS]
#
# Options:
# -s, --snapshot Version from git ref for prototyping
# -r, --release Version from .env for releases
# -v, --version VERSION Custom version
# -p, --push Push images after build
# -b, --backend-only Only build backend
# -f, --frontend-only Only build frontend
# -h, --help Show this help
#
# Examples:
# ./build.sh -r -p
# ./build.sh -s -frontend-only
# ./build.sh -v dev
# ---------------------------------------------------------------------------
# -e: Exit immediately if any command returns a non-zero exit code
# -u: Treat unset variables as errors
# -o pipefail: Make pipelines fail if any command in the pipe fails, not just the last one
set -euo pipefail
#cd to script dir
pushd "$(dirname "${BASH_SOURCE[0]}")"
SCRIPT_DIR="$(pwd)"
source "${SCRIPT_DIR}/.env"
popd
#default: snapshot
VERSION="${IMAGE_TAG}-SNAPSHOT$(git rev-parse --short HEAD 2>/dev/null || echo "local")"
PUSH=false
BUILD_BACKEND=true
BUILD_FRONTEND=true
usage() {
sed -n '/^# Usage:/,/^# ---/p' "$0" | sed 's/^# //' | sed 's/^#//'
exit 0
}
# --- parse arguments -------------------------------------------------------
while [[ $# -gt 0 ]]; do
case $1 in
-s|--snapshot) VERSION="${IMAGE_TAG}-SNAPSHOT$(git rev-parse --short HEAD 2>/dev/null || echo "local")"; shift ;;
-r|--release) VERSION=${IMAGE_TAG}; shift ;;
-v|--version) VERSION=$2; shift 2 ;;
-p|--push) PUSH=true; shift ;;
-b|--backend-only) BUILD_FRONTEND=false; shift ;;
-f|--frontend-only) BUILD_BACKEND=false; shift ;;
-h|--help) usage ;;
*) echo "Unknown option: $1" >&2; exit 1 ;;
esac
done
# --- derive image names -----------------------------------------------------
registry_prefix="${REGISTRY}/"
BACKEND_IMAGE="${registry_prefix}${IMAGE_NAME}-backend:${VERSION}"
FRONTEND_IMAGE="${registry_prefix}${IMAGE_NAME}-frontend:${VERSION}"
#SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo "============================================"
echo " ${IMAGE_NAME} - Docker Build"
echo " Version : ${VERSION}"
echo " Registry : ${REGISTRY}"
echo " Push : ${PUSH}"
echo "============================================"
# --- build ------------------------------------------------------------------
if $BUILD_BACKEND; then
echo ""
echo ">>> Building backend: ${BACKEND_IMAGE}"
docker build \
--tag "${BACKEND_IMAGE}" \
"${SCRIPT_DIR}/backend"
echo " Backend built successfully."
fi
if $BUILD_FRONTEND; then
echo ""
echo ">>> Building frontend: ${FRONTEND_IMAGE}"
docker build \
--tag "${FRONTEND_IMAGE}" \
"${SCRIPT_DIR}/frontend"
echo " Frontend built successfully."
fi
# --- push -------------------------------------------------------------------
if $PUSH; then
docker login ${REGISTRY}
if $BUILD_BACKEND; then
echo ""
echo ">>> Pushing ${BACKEND_IMAGE}"
docker push "${BACKEND_IMAGE}"
fi
if $BUILD_FRONTEND; then
echo ""
echo ">>> Pushing ${FRONTEND_IMAGE}"
docker push "${FRONTEND_IMAGE}"
fi
echo ""
echo "Images pushed successfully."
fi
echo ""
echo "Done."
if $BUILD_BACKEND; then echo " Backend → ${BACKEND_IMAGE}"; fi
if $BUILD_FRONTEND; then echo " Frontend → ${FRONTEND_IMAGE}"; fi