#!/bin/bash --norc
set -euo pipefail

image_target="$1"
push_environ_target="$2"
push_version_target="$3"

if [ $# -ne 3 ]; then
  echo "Usage: $0 <image> <environ> <version>" >&2
  exit 1
fi

image_id="$("$image_target" --norun | grep sha256: | sed -E 's#.*sha256:##')"
if ! docker run "$image_id" -h >/dev/null; then
  echo The image target failed to run. Please fix the issue before deploying the image. >&2
  exit 1
fi

"$push_version_target"
"$push_environ_target"
