name: 'Docker Tag and Push'
description: 'Tag and Push Docker Images'

inputs:
  local_image:
    description: 'Local Image Name:Tag'
    required: true
  push_tags:
    description: 'Target Name:Tag (newline-separated list for multiple tags)'
    required: true
  # There isn't a clean way to have an additional tag that is conditional
  # Adding this to handle this use-case (we want multiple tags for main builds)
  conditional_tag:
    description: 'Optional tag for conditionals'
    required: false
  aws_push:
    description: 'Push to AWS Boolean'
    required: false
    default: 'false'
  azure_push:
    description: 'Push to Azure Container Registry (ACR) Boolean'
    required: false
    default: 'false'
  aws_account_id:
    description: 'AWS Account ID'
    required: false
  aws_default_region:
    description: 'AWS Default Region'
    required: false
  azure_acr_hostname:
    description: 'Azure ACR hostname'
    required: false

outputs:
  image_tags:
    description: 'Image Tags'
    value: ${{ inputs.push_tags }}

runs:
  using: "composite"
  steps:
    - name: Set up Docker Buildx
      uses: docker/setup-buildx-action@v3
    - name: ECR Tag and Push
      shell: bash
      if: ${{ inputs.aws_push == 'true' }}
      env:
        LOCAL_IMAGE: ${{ inputs.local_image }}
        PUSH_TAGS: ${{ inputs.push_tags }}
        CONDITIONAL_TAG: ${{ inputs.conditional_tag }}
        ECR_HOSTNAME: ${{ inputs.aws_account_id }}.dkr.ecr.${{ inputs.aws_default_region }}.amazonaws.com
      run: |
        set -euo pipefail
        if [[ ${CONDITIONAL_TAG} != '' ]]; then
            docker tag ${LOCAL_IMAGE} ${ECR_HOSTNAME}/${CONDITIONAL_TAG}
            docker push ${ECR_HOSTNAME}/${CONDITIONAL_TAG}
        fi
        while IFS= read -r TAG; do
          if [ -z "$TAG" ]; then
            continue
          fi
          echo "Tagging and pushing: ${ECR_HOSTNAME}/${TAG}"
          docker tag "${LOCAL_IMAGE}" "${ECR_HOSTNAME}/${TAG}"
          docker push "${ECR_HOSTNAME}/${TAG}"
        done <<< "$PUSH_TAGS"
    - name: ACR Tag and Push
      shell: bash
      if: ${{ inputs.azure_push == 'true' }}
      env:
        LOCAL_IMAGE: ${{ inputs.local_image }}
        PUSH_TAGS: ${{ inputs.push_tags }}
        AZURE_ACR_HOSTNAME: ${{ inputs.azure_acr_hostname }}
      run: |
        set -euo pipefail
        while IFS= read -r TAG; do
          if [ -z "$TAG" ]; then
            continue
          fi
          echo "Tagging and pushing: ${AZURE_ACR_HOSTNAME}/${TAG}"
          docker tag "${LOCAL_IMAGE}" "${AZURE_ACR_HOSTNAME}/${TAG}"
          docker push "${AZURE_ACR_HOSTNAME}/${TAG}"
        done <<< "$PUSH_TAGS"
