#!/usr/bin/env bash set -euo pipefail # -------------------------------------------------- # Configuration # -------------------------------------------------- ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" FRAPPE_REPO="${FRAPPE_DOCKER_DIR:-${ROOT_DIR}/../frappe_docker}" REGISTRY_IMAGE="git.prothmann.com/christian/erpnexthp" BUILD_IMAGE="${REGISTRY_IMAGE}:build-local" # -------------------------------------------------- # Optional command-line arguments # -------------------------------------------------- REQUESTED_TAG="" usage() { echo "Usage: $0 [--tag TAG]" echo "" echo "Options:" echo " --tag TAG Override the automatic ERPNext version tag" echo " -h, --help Show this help message" } while [[ $# -gt 0 ]]; do case "$1" in --tag) if [[ $# -lt 2 || -z "$2" ]]; then echo "ERROR: --tag requires a value." exit 1 fi REQUESTED_TAG="$2" shift 2 ;; --tag=*) REQUESTED_TAG="${1#--tag=}" shift ;; -h|--help) usage exit 0 ;; *) echo "ERROR: Unknown argument: $1" usage exit 1 ;; esac done # Validate custom Docker tag if [[ -n "$REQUESTED_TAG" ]]; then if [[ ! "$REQUESTED_TAG" =~ ^[a-zA-Z0-9_][a-zA-Z0-9_.-]{0,127}$ ]]; then echo "ERROR: Invalid Docker image tag: $REQUESTED_TAG" exit 1 fi fi # -------------------------------------------------- # Validate required files # -------------------------------------------------- if [[ ! -f "${FRAPPE_REPO}/images/layered/Containerfile" ]]; then echo "ERROR: frappe_docker repository not found." exit 1 fi if [[ ! -f "${ROOT_DIR}/Containerfile.cups" ]]; then echo "ERROR: Missing Containerfile.cups" exit 1 fi if [[ ! -f "${ROOT_DIR}/docker-compose.override.yml" ]]; then echo "ERROR: Missing docker-compose.override.yml" exit 1 fi cd "$FRAPPE_REPO" APPS_FILE="$(mktemp)" COMPOSE_ENV_FILE="$(mktemp)" BUILD_CONTAINERFILE="$(mktemp)" trap 'rm -f "$APPS_FILE" "$COMPOSE_ENV_FILE" "$BUILD_CONTAINERFILE"' EXIT # -------------------------------------------------- # 1. Define applications # -------------------------------------------------- cat > "$APPS_FILE" <<'JSON' [ { "url": "https://github.com/frappe/erpnext", "branch": "version-16" }, { "url": "https://git.prothmann.com/christian/manufacturing_overview", "branch": "version-15" }, { "url": "https://git.prothmann.com/christian/label_printing", "branch": "version-15" }, { "url": "https://git.prothmann.com/christian/manufacturing_utilities", "branch": "version-16" }, { "url": "https://github.com/alyf-de/eu_einvoice.git", "branch": "version-16" } ] JSON # -------------------------------------------------- # 2. Prepare Containerfile with CUPS support # -------------------------------------------------- echo "Preparing Frappe Docker image with CUPS support..." cat \ "${FRAPPE_REPO}/images/layered/Containerfile" \ "${ROOT_DIR}/Containerfile.cups" \ > "$BUILD_CONTAINERFILE" # -------------------------------------------------- # 3. Build AMD64 production image # -------------------------------------------------- echo "Building ERPNext 16 for linux/amd64..." docker buildx build \ --platform linux/amd64 \ --pull \ --load \ --build-arg FRAPPE_PATH=https://github.com/frappe/frappe \ --build-arg FRAPPE_BRANCH=version-16 \ --build-arg "CACHE_BUST=$(date -u +%s)" \ --secret "id=apps_json,src=${APPS_FILE}" \ --tag "${BUILD_IMAGE}" \ --file "$BUILD_CONTAINERFILE" \ . # -------------------------------------------------- # 4. Verify CUPS support # -------------------------------------------------- echo "Verifying pycups installation..." docker run --rm \ --platform linux/amd64 \ --entrypoint /home/frappe/frappe-bench/env/bin/python \ "${BUILD_IMAGE}" \ -c "import cups; print('pycups is available')" echo "CUPS support verified." # -------------------------------------------------- # 5. Detect actual ERPNext version # -------------------------------------------------- ERP_VERSION="$( docker run --rm \ --platform linux/amd64 \ --entrypoint /bin/cat \ "${BUILD_IMAGE}" \ /home/frappe/frappe-bench/apps/erpnext/erpnext/__init__.py \ | awk -F '"' '/^__version__ = / {print $2; exit}' )" if [[ ! "$ERP_VERSION" =~ ^16\.[0-9]+\.[0-9]+$ ]]; then echo "ERROR: Could not determine ERPNext version." exit 1 fi echo "Detected ERPNext version: ${ERP_VERSION}" # -------------------------------------------------- # 6. Determine final image tag # -------------------------------------------------- if [[ -n "$REQUESTED_TAG" ]]; then IMAGE_TAG="$REQUESTED_TAG" echo "Using custom image tag: ${IMAGE_TAG}" else IMAGE_TAG="$ERP_VERSION" echo "Using automatic image tag: ${IMAGE_TAG}" fi FINAL_IMAGE="${REGISTRY_IMAGE}:${IMAGE_TAG}" docker tag "${BUILD_IMAGE}" "${FINAL_IMAGE}" # -------------------------------------------------- # 7. Prepare temporary Compose environment # -------------------------------------------------- { printf 'ERPNEXT_VERSION=%s\n' "$ERP_VERSION" printf 'CUSTOM_IMAGE=%s\n' "$REGISTRY_IMAGE" printf 'CUSTOM_TAG=%s\n' "$IMAGE_TAG" cat <<'ENV' DB_PASSWORD=COMPOSE_TEMPLATE_ONLY PULL_POLICY=always RESTART_POLICY=unless-stopped SITES_RULE=Host(`erp16.prothmann.com`) ENV } > "$COMPOSE_ENV_FILE" # -------------------------------------------------- # 8. Generate official production Compose # -------------------------------------------------- echo "Generating production Docker Compose..." docker compose \ --env-file "$COMPOSE_ENV_FILE" \ --project-name erpnext-16 \ -f compose.yaml \ -f overrides/compose.mariadb.yaml \ -f overrides/compose.redis.yaml \ config > "${ROOT_DIR}/docker-compose.yml" # -------------------------------------------------- # 9. Validate final configuration # -------------------------------------------------- echo "Validating Docker Compose..." docker compose \ --env-file "$COMPOSE_ENV_FILE" \ --project-name erpnext-16 \ -f "${ROOT_DIR}/docker-compose.yml" \ -f "${ROOT_DIR}/docker-compose.override.yml" \ config --quiet echo "Docker Compose validation successful." # -------------------------------------------------- # 10. Push image to Gitea # -------------------------------------------------- echo "Publishing ${FINAL_IMAGE}..." docker push "${FINAL_IMAGE}" # -------------------------------------------------- # 11. Summary # -------------------------------------------------- echo "" echo "============================================" echo " ERPNext production build completed" echo "============================================" echo "" echo "ERPNext version: ${ERP_VERSION}" echo "Image tag: ${IMAGE_TAG}" echo "Docker image: ${FINAL_IMAGE}" echo "CUPS support: Installed and verified" echo "Compose file: ${ROOT_DIR}/docker-compose.yml" echo "" echo "To deploy on hyp1, set:" echo "CUSTOM_TAG=${IMAGE_TAG}" echo ""