diff --git a/mailgun-webhook/build-image.sh b/mailgun-webhook/build-image.sh index 7a41675..be7c6f2 100755 --- a/mailgun-webhook/build-image.sh +++ b/mailgun-webhook/build-image.sh @@ -1,13 +1,54 @@ #!/bin/bash -if [ "$1" == "" ] ; then +# 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 1.0.0 [--autopush]\n\n" + printf "\n\nSyntax:\n\n build-image.sh [--auto-push]" + printf "\n build-image.sh --auto-version [--auto-push]\n\n" exit 1 fi -# Check for --autopush flag -if [ "$2" == "--autopush" ]; then +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 @@ -18,11 +59,6 @@ fi printf "\nBUILD START ...\n\n" -REGISTRY_URL="registry.budakova.org" -IMAGE_NAME=$(node -p "require('./package.json').name") -IMAGE_VERSION=$1 - -IMAGE_TAG=$REGISTRY_URL/$IMAGE_NAME:$IMAGE_VERSION docker build . -t $IMAGE_TAG if [[ "$PUSH_IMAGE" =~ ^[Yy]$ ]]