#!/usr/bin/env bash

set -euo pipefail
set -x
COMPONENT="${COMPONENT:-audit}"
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
REGION="${REGION:-$AWS_REGION}"
ECR_REGISTRY="${ACCOUNT_ID}.dkr.ecr.${REGION}.amazonaws.com"
ECR_REPO=${ECR_REPO:-dbaas/infra-api/infra-provider}
if ! aws ecr describe-repositories --repository-name ${ECR_REPO} >/dev/null 2>&1
then

    aws ecr create-repository --repository-name ${ECR_REPO}
fi
aws ecr get-login-password --region ${REGION} | docker login --username AWS --password-stdin ${ECR_REGISTRY}
trap "docker logout https://${ECR_REGISTRY}" EXIT

DOCKER_PLATFORMS="linux/amd64,linux/arm64"
echo "TAG_NAME: ${TAG_NAME:-}, GIT_COMMIT: ${GIT_COMMIT:-}, PLATFORMS: ${DOCKER_PLATFORMS}"

IMG="${ECR_REGISTRY}/${ECR_REPO}:${TAG_NAME:-$GIT_COMMIT}"
go mod vendor
# Set up the docker buildx in .github/workflows/pipeline.yaml#333
docker buildx build --push --platform ${DOCKER_PLATFORMS} --file ${COMPONENT}.Dockerfile -t ${IMG} .

echo "${IMG} pushed"
