name: Docker
on:
  workflow_dispatch:

jobs:
  docker:
    name: Docker
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        config:
          - ubuntu: 20.04
            python: 3.9
            label: 3.9
          - ubuntu: 22.04
            python: 3
            label: "3.10"

    # Start a local registry to which we will push trame-common, so that
    # docker buildx may access it in later steps.
    services:
      registry:
        image: registry
        ports:
          - 5000:5000

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      # To emulate other architectures
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1

      # For multi-platform builds
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
        with:
          driver-opts: network=host

      - name: Create month stamp
        run: echo "MONTH_STAMP=$(date +%Y-%m)" >> $GITHUB_ENV

      - name: Login to DockerHub
        uses: docker/login-action@v1
        with:
            username: ${{ secrets.DOCKER_USERNAME }}
            password: ${{ secrets.DOCKER_TOKEN }}

      - name: Build common
        uses: docker/build-push-action@v2
        with:
            platforms: linux/amd64,linux/arm64
            context: docker
            file: docker/Dockerfile.common
            build-args: |
              BASE_IMAGE=ubuntu:${{ matrix.config.ubuntu }}
            push: true
            tags: localhost:5000/trame-common-ubuntu${{ matrix.config.ubuntu }}

      - name: Build common (glvnd)
        uses: docker/build-push-action@v2
        with:
            context: docker
            file: docker/Dockerfile.common
            build-args: |
              BASE_IMAGE=nvidia/opengl:1.2-glvnd-devel-ubuntu${{ matrix.config.ubuntu }}
            push: true
            tags: localhost:5000/trame-common-glvnd-ubuntu${{ matrix.config.ubuntu }}

      - name: Build common (cuda)
        uses: docker/build-push-action@v2
        with:
            context: docker
            file: docker/Dockerfile.common
            build-args: |
              BASE_IMAGE=nvidia/cuda:12.2.0-runtime-ubuntu${{ matrix.config.ubuntu }}
            push: true
            tags: localhost:5000/trame-common-cuda-ubuntu${{ matrix.config.ubuntu }}

      # --- NOT WORKING YET -----------------------------------------
      # - name: Build and push (py3.12-uv)
      #   uses: docker/build-push-action@v2
      #   with:
      #       platforms: linux/amd64,linux/arm64
      #       context: docker
      #       file: docker/Dockerfile.uv
      #       build-args: |
      #         BASE_IMAGE=localhost:5000/trame-common-ubuntu22.04
      #         PYTHON_VERSION=3.12
      #       push: true
      #       tags: |
      #           kitware/trame:py3.12-uv
      # --- NOT WORKING YET -----------------------------------------

      - name: Build and push (py${{ matrix.config.label }}-pip)
        uses: docker/build-push-action@v2
        with:
            platforms: linux/amd64,linux/arm64
            context: docker
            file: docker/Dockerfile.pip
            build-args: |
              BASE_IMAGE=localhost:5000/trame-common-ubuntu${{ matrix.config.ubuntu }}
              PYTHON_VERSION=${{ matrix.config.python }}
            push: true
            tags: |
                kitware/trame:py${{ matrix.config.label }}
                kitware/trame:py${{ matrix.config.label }}-ubuntu${{ matrix.config.ubuntu }}
                kitware/trame:py${{ matrix.config.label }}-${{ env.MONTH_STAMP }}

      - name: Build and push (pip py${{ matrix.config.label }}-glvnd)
        uses: docker/build-push-action@v2
        with:
            context: docker
            file: docker/Dockerfile.pip
            build-args: |
              BASE_IMAGE=localhost:5000/trame-common-glvnd-ubuntu${{ matrix.config.ubuntu }}
              PYTHON_VERSION=${{ matrix.config.python }}
            push: true
            tags: |
                kitware/trame:py${{ matrix.config.label }}-glvnd
                kitware/trame:py${{ matrix.config.label }}-1.2-glvnd-runtime-ubuntu${{ matrix.config.ubuntu }}
                kitware/trame:py${{ matrix.config.label }}-glvnd-${{ env.MONTH_STAMP }}

      - name: Build and push (pip py${{ matrix.config.label }}-cuda)
        uses: docker/build-push-action@v2
        with:
            context: docker
            file: docker/Dockerfile.pip
            build-args: |
              BASE_IMAGE=localhost:5000/trame-common-cuda-ubuntu${{ matrix.config.ubuntu }}
              PYTHON_VERSION=${{ matrix.config.python }}
            push: true
            tags: |
                kitware/trame:py${{ matrix.config.label }}-cuda
                kitware/trame:py${{ matrix.config.label }}-12.2.0-cuda-runtime-ubuntu${{ matrix.config.ubuntu }}
                kitware/trame:py${{ matrix.config.label }}-cuda-${{ env.MONTH_STAMP }}

      - name: Build and push (py${{ matrix.config.label }}-conda)
        uses: docker/build-push-action@v2
        with:
            platforms: linux/amd64,linux/arm64
            context: docker
            file: docker/Dockerfile.conda
            build-args: |
              BASE_IMAGE=localhost:5000/trame-common-ubuntu${{ matrix.config.ubuntu }}
              PYTHON_VERSION=${{ matrix.config.label }}
            push: true
            tags: |
                kitware/trame:py${{ matrix.config.label }}-conda
                kitware/trame:py${{ matrix.config.label }}-conda-ubuntu${{ matrix.config.ubuntu }}
                kitware/trame:py${{ matrix.config.label }}-conda-${{ env.MONTH_STAMP }}

      - name: Build and push (py${{ matrix.config.label }}-conda-glvnd)
        uses: docker/build-push-action@v2
        with:
            context: docker
            file: docker/Dockerfile.conda
            build-args: |
              BASE_IMAGE=localhost:5000/trame-common-glvnd-ubuntu${{ matrix.config.ubuntu }}
              PYTHON_VERSION=${{ matrix.config.label }}
            push: true
            tags: |
                kitware/trame:py${{ matrix.config.label }}-conda-glvnd
                kitware/trame:py${{ matrix.config.label }}-conda-1.2-glvnd-runtime-ubuntu${{ matrix.config.ubuntu }}
                kitware/trame:py${{ matrix.config.label }}-conda-glvnd-${{ env.MONTH_STAMP }}
