#!/usr/bin/env bash

# see jenkinsfile for version to build
PROM_VERSION=${PROM_VERSION:-v2.25.0}
LOCAL_IMAGE=xaicorp/prometheus
TAG=${PROM_VERSION}-${BUILD_NUMBER:-dev}
REMOTE_IMAGE=dkregistry.xai-corp.net:5000/${LOCAL_IMAGE}:${TAG}

LOG=$(mktemp)

export LOCAL_IMAGE
export REMOTE_IMAGE
export TAG



function trap_exit() {
  code=$?
  if [ $code -gt 0 ]; then
    echo
    cat "$LOG"
    rm "$LOG"
    dc logs --tail=10
    dc down
    echo -e "\033[31mFailed to build functional image\033[39m"
    exit $code
  fi

  dc down
  rm "$LOG"
  echo -e "\033[32mSuccess:\033[39m ${LOCAL_IMAGE}:${TAG} successfully built"
}
trap  trap_exit EXIT
