#!/usr/bin/env bash
# Pulumi will only update the deployment if the tag changes.
# So it won't deploy the latest dirty changes we need.
# A simple approach would be to add a timestamp.
# Below is a proper approach that gets a git hash of dirty changes.
# It will work for example when deploying components one-by-one without making changes.
# Ignore tools and deploy directories, the dependency files will be changed during build

set -euo pipefail

default_image_name="cluster-proxy:local"
set -x

{
    make docker-build

    IMAGE_TAG=${IMAGE_TAG:-"$(git rev-parse --short HEAD)"}
    if ! git diff --quiet ; then
        index_file=/tmp/cluster-proxy_git-index
        cp .git/index "$index_file"
        export GIT_INDEX_FILE="$index_file"
        git add -u
        IMAGE_TAG="$IMAGE_TAG-$(git write-tree)"
    fi
    new_image="cluster-proxy:$IMAGE_TAG"
    docker tag "$default_image_name" "$new_image"
} > /dev/null

echo "$new_image"