name: "Docker Push"
description: "Builds and pushes a Docker image with specified tags, or pulls from source registry and re-tags"
inputs:
  version_tag:
    description: "The primary version tag for the image (e.g., 1.0.0)"
    required: true
  push_latest:
    description: 'Whether to also tag and push as "latest"'
    required: false
    default: "true"
  docker_username:
    description: "Docker Hub username"
    required: true
  docker_password:
    description: "Docker Hub password"
    required: true
  source_registry:
    description: "Source registry to pull from (e.g., ECR). If provided, will pull and re-tag instead of building"
    required: false
    default: ""
  source_image_tag:
    description: "Source image tag to pull (without platform suffix). Required if source_registry is provided"
    required: false
    default: ""

runs:
  using: "composite"
  steps:
    - name: Setup Qemu
      uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392 # v3.6.0

    - name: Setup Docker Buildx
      uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0

    - name: Login to Docker Hub
      uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772 # v3.4.0
      with:
        username: ${{ inputs.docker_username }}
        password: ${{ inputs.docker_password }}

    - name: Prepare Docker tags
      id: docker_tags
      shell: bash
      run: |
        image_name="solace/solace-agent-mesh"
        tags="${image_name}:${{ inputs.version_tag }}"
        if [[ "${{ inputs.push_latest }}" == "true" ]]; then
          tags="$tags,${image_name}:latest"
        fi
        echo "TAGS_LIST=$tags" >> $GITHUB_OUTPUT
        echo "Prepared tags: $tags"

    - name: Pull from Source and Push to DockerHub
      if: inputs.source_registry != ''
      shell: bash
      run: |
        source_image="${{ inputs.source_registry }}/solace-agent-mesh:${{ inputs.source_image_tag }}"

        echo "Pulling multi-platform image from ECR: ${source_image}"

        # Pull the multi-platform manifest and both platform images
        docker pull --platform linux/amd64 "${source_image}-amd64"
        docker pull --platform linux/arm64 "${source_image}-arm64"

        # Convert comma-separated tags to array
        IFS=',' read -ra TAGS <<< "${{ steps.docker_tags.outputs.TAGS_LIST }}"

        # For each target tag, create a multi-platform manifest
        for TAG in "${TAGS[@]}"; do
          TAG=$(echo "$TAG" | xargs)  # Trim whitespace
          echo "Creating multi-platform manifest for: ${TAG}"
          
          # Tag the platform-specific images for DockerHub
          docker tag "${source_image}-amd64" "${TAG}-amd64"
          docker tag "${source_image}-arm64" "${TAG}-arm64"
          
          # Push platform-specific images
          docker push "${TAG}-amd64"
          docker push "${TAG}-arm64"
          
          # Create and push multi-platform manifest
          docker buildx imagetools create \
            --tag "${TAG}" \
            "${TAG}-amd64" \
            "${TAG}-arm64"
          
          docker push "${TAG}"
        done
