Files
qaffee/build.sh

106 lines
3.2 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
# -o, --only SERVICE Only build this service (repeatable)
# -h, --help Show this help
#
# Examples:
# ./build.sh -r -p
# ./build.sh -s -o frontend
# ./build.sh -v dev -o backend -o frontend
# ---------------------------------------------------------------------------
# -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]}")" > /dev/null
SCRIPT_DIR="$(pwd)"
source "${SCRIPT_DIR}/build.env"
# read from .env:
# REGISTRY, IMAGE_NAME, IMAGE_VERSION, SNAPSHOT_TAG
# SERVICES (subfolder name = image suffix)
popd > /dev/null
#default: build snapshot
VERSION="${IMAGE_VERSION}-${SNAPSHOT_TAG}" # read from .env
PUSH=false
ONLY=()
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_VERSION}-SNAPSHOT$(git rev-parse --short HEAD 2>/dev/null || echo "local")"; shift ;;
-r|--release) VERSION="${IMAGE_VERSION}"; shift ;;
-v|--version) VERSION="$2"; shift 2 ;;
-p|--push) PUSH=true; shift ;;
-o|--only) ONLY+=("$2"); shift 2 ;;
-h|--help) usage ;;
*) echo "Unknown option: $1" >&2; exit 1 ;;
esac
done
# if --only was given, filter SERVICES down to those entries
if [[ ${#ONLY[@]} -gt 0 ]]; then
SERVICES=("${ONLY[@]}")
fi
echo "============================================"
echo " Docker Build - ${IMAGE_NAME}"
echo ""
echo " Services : ${SERVICES[*]}"
echo " Version : ${VERSION}"
echo " Registry : ${REGISTRY}"
echo " Push : ${PUSH}"
echo "============================================"
# --- build ------------------------------------------------------------------
for SERVICE in "${SERVICES[@]}"; do
IMAGE="${REGISTRY}/${IMAGE_NAME}-${SERVICE}:${VERSION}"
echo ""
echo ">>> Building ${SERVICE}: ${IMAGE}"
docker buildx build \
--platform ${platform} \
-t "${IMAGE}" \
$($PUSH && echo "--push" || echo "") \
"${SCRIPT_DIR}/${SERVICE}"
echo " ${SERVICE} built successfully."
done
# --- push -------------------------------------------------------------------
if $PUSH; then
docker login "${REGISTRY}" # read from .env
for SERVICE in "${SERVICES[@]}"; do
IMAGE="${REGISTRY}/${IMAGE_NAME}-${SERVICE}:${VERSION}"
echo ""
echo ">>> Pushing ${IMAGE}"
docker push "${IMAGE}"
done
echo ""
echo "Images pushed successfully."
fi
echo ""
echo "Done."
for SERVICE in "${SERVICES[@]}"; do
echo " ${SERVICE}${REGISTRY}/${IMAGE_NAME}-${SERVICE}:${VERSION}"
done