name: Publish Docker Image
# https://www.docker.com/blog/first-docker-github-action-is-here
# https://github.com/docker/build-push-action
on:  # Trigger the workflow on push or pull request, but only for the master branch
  push: {}
  pull_request:
    branches: [master]
    types: [opened, reopened, ready_for_review, synchronize]

jobs:

  build-push:
    if: github.event.pull_request.draft == false
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
      matrix:
        python_version: ["3.7", "3.8", "3.9"]
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      # https://github.com/docker/setup-buildx-action
      # Set up Docker Buildx - to use cache-from and cache-to argument of buildx command
      - uses: docker/setup-buildx-action@v1
      - name: Login to DockerHub
        if: ${{ github.repository_owner == 'Borda' }}
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Build and Publish to Docker Hub
        uses: docker/build-push-action@v2
        # https://github.com/docker/build-push-action#cache-to-registry
        with:
          build-args: |
            PYTHON_VERSION=${{ matrix.python_version }}
          cache-from: type=registry,ref=borda/birl:SOTA-py${{ matrix.python_version }}
          cache-to: type=inline
          file: bm_experiments/Dockerfile
          push: ${{ github.ref == 'refs/heads/master' && github.event_name == 'push' }}
          tags: borda/birl:SOTA-py${{ matrix.python_version }}
        timeout-minutes: 240
