#!/bin/bash # Parse flags AUTO_VERSION=false AUTO_PUSH=false IMAGE_VERSION="" for arg in "$@"; do case $arg in --auto-version) AUTO_VERSION=true ;; --auto-push) AUTO_PUSH=true ;; *) if [ "$IMAGE_VERSION" == "" ] && [[ ! "$arg" =~ ^-- ]]; then IMAGE_VERSION=$arg fi ;; esac done # Determine version if [ "$AUTO_VERSION" = true ]; then IMAGE_VERSION=$(node -p "require('./package.json').version") printf "\nAuto-version enabled. Using version from package.json: %s\n" "$IMAGE_VERSION" elif [ "$IMAGE_VERSION" == "" ]; then printf "\nYou did not specify the Docker image version to build" printf "\n\nSyntax:\n\n build-image.sh [--auto-push]" printf "\n build-image.sh --auto-version [--auto-push]\n\n" exit 1 fi REGISTRY_URL="registry.budakova.org" IMAGE_NAME=$(node -p "require('./package.json').name") IMAGE_TAG=$REGISTRY_URL/$IMAGE_NAME:$IMAGE_VERSION # Check if image already exists in registry (only when using auto-version) if [ "$AUTO_VERSION" = true ]; then printf "\nChecking if image %s already exists in registry...\n" "$IMAGE_TAG" if docker manifest inspect $IMAGE_TAG > /dev/null 2>&1; then printf "\nERROR: Image %s already exists in registry.\n" "$IMAGE_TAG" printf "Please update the version in package.json before building.\n\n" exit 1 fi printf "Image does not exist in registry. Proceeding with build.\n" fi # Check for push preference if [ "$AUTO_PUSH" = true ]; then PUSH_IMAGE="y" printf "\nAuto-push enabled. Image will be pushed to registry.\n" else read -p "BUILD: Push new image to registry [y/n]? " -n 1 -r echo # (optional) move to a new line PUSH_IMAGE="$REPLY" fi printf "\nBUILD START ...\n\n" docker build . -t $IMAGE_TAG if [[ "$PUSH_IMAGE" =~ ^[Yy]$ ]] then printf "\nPushing image ...\n\n" docker push $IMAGE_TAG fi printf "\nBUILD DONE!\n\n"