# Publish Docker image to Docker Hub
#
# Triggers:
#   - After PyPI Publish workflow completes successfully
#   - Manual dispatch
#
# Images are published to: docker.io/mvfolino68/pathway-viz
#
# Required secrets:
#   - DOCKERHUB_USERNAME: Your Docker Hub username (mvfolino68)
#   - DOCKERHUB_TOKEN: Docker Hub access token (create at hub.docker.com/settings/security)

name: Docker Publish

on:
  workflow_run:
    workflows: ["PyPI Publish"]
    types: [completed]
  workflow_dispatch:

env:
  REGISTRY: docker.io
  IMAGE_NAME: mvfolino68/pathway-viz

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    # Only run if PyPI publish succeeded (or manual trigger)
    if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' }}

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          # Use the ref from the triggering workflow for workflow_run events
          ref: ${{ github.event.workflow_run.head_branch || github.ref }}

      - name: Get version from pyproject.toml
        id: version
        run: |
          VERSION=$(grep -m1 'version = ' pyproject.toml | cut -d'"' -f2)
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Version: $VERSION"

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: |
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.version.outputs.version }}
            ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max
