diff --git a/build_new.sh b/build_new.sh index 22c24bcd..22343568 100755 --- a/build_new.sh +++ b/build_new.sh @@ -4,6 +4,8 @@ TAG="" REMOTE_HOST="erpnext.lasthourhosting.org" REMOTE_USER="root" MIGRATE=false +BUILD_ONLY=false +ALREADY_EXISTING=false while [[ $# -gt 0 ]]; do case "$1" in @@ -42,6 +44,10 @@ while [[ $# -gt 0 ]]; do echo "Usage: $0 [--tag ] [--remote-host ] [--remote-user ]" exit 0 ;; + --build-only) + BUILD_ONLY=true + shift 1 + ;; *) echo "Usage: $0 [--tag ] [--remote-host ] [--remote-user ]" exit 1 @@ -55,6 +61,63 @@ if [[ -z "$TAG" ]]; then exit 1 fi +cleanup() { + set +e + echo "🚨 Build failed! Starting cleanup..." + + IMAGE_NAME="githaven.org/shiloh/brotherton_frappe_docker:$TAG" + + # Local cleanup + if docker image inspect "$IMAGE_NAME" >/dev/null 2>&1; then + echo "🗑️ Removing local image $IMAGE_NAME..." + docker rmi -f "$IMAGE_NAME" + else + echo "Local image $IMAGE_NAME not found." + fi + + # Remote cleanup + echo "Checking remote registry for $IMAGE_NAME..." + if skopeo inspect "docker://$IMAGE_NAME" >/dev/null 2>&1; then + echo "🗑️ Removing remote image $IMAGE_NAME..." + skopeo delete "docker://$IMAGE_NAME" + else + echo "Remote image $IMAGE_NAME not found." + fi +} + +trap 'cleanup' ERR + +IMAGE_NAME="githaven.org/shiloh/brotherton_frappe_docker:$TAG" +echo "🔍 Checking for pre-existing tag: $TAG" + +if skopeo inspect "docker://$IMAGE_NAME" >/dev/null 2>&1; then + echo "❗ Tag $TAG already exists in the remote registry." + ALREADY_EXISTING=true +else + echo "✅ Tag $TAG is available in registry." +fi + +if docker image inspect "$IMAGE_NAME" >/dev/null 2>&1; then + echo "❗ Tag $TAG already exists in the local Docker images." + ALREADY_EXISTING=true +else + echo "✅ Tag $TAG is available locally." +fi + +if [[ -n "$REMOTE_HOST" ]] && [ "$BUILD_ONLY" = false ]; then + echo "Checking remote server $REMOTE_HOST for existing image..." + if ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" "docker image inspect $IMAGE_NAME >/dev/null 2>&1"; then + echo "❗ Tag $TAG already exists on remote server $REMOTE_HOST." + ALREADY_EXISTING=true + else + echo "✅ Tag $TAG is available on remote server." + fi +fi + +if [ "$ALREADY_EXISTING" = true ]; then + echo "⚠️ Build aborted due to existing tag. View logs above to identify where the tag exists." + exit 1 +fi export TAG export APPS_JSON_BASE64=$(base64 -w 0 ./apps_new.json) echo "Building Brotherton Frappe Docker Image with tag: $TAG" @@ -77,7 +140,7 @@ if [ "$MIGRATE" = true ]; then MIGRATE_COMMAND="--migrate" fi -if [[ -n "$REMOTE_HOST" ]]; then +if [[ -n "$REMOTE_HOST" ]] && [ "$BUILD_ONLY" = false ]; then echo "🔗 Connecting to remote server $REMOTE_USER@$REMOTE_HOST..." ssh "$REMOTE_USER@$REMOTE_HOST" "./re-deploy.sh --tag $TAG $MIGRATE_COMMAND" echo "✅ Remote deployment completed!"