name: build

on:
  push:
    branches:
      - main

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Cleanup
        run: |
          ls -lart
          echo "Initial status:"
          df -h
          echo "Cleaning up resources:"
          sudo swapoff -a
          sudo rm -f /swapfile
          sudo apt clean
          sudo rm -rf /usr/share/dotnet
          sudo rm -rf /opt/ghc
          sudo rm -rf "/usr/local/share/boost"
          sudo rm -rf "$AGENT_TOOLSDIRECTORY"
          docker rmi $(docker image ls -aq)
          echo "Final status:"
          df -h
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v1
      - name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@v1
        with:
          buildkitd-flags: --debug
      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Build and push dev image
        id: docker_build_dev
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.buildx.outputs.name }}
          build-args: |
            BASE_IMAGE=pytorch/pytorch:1.9.0-cuda11.1-cudnn8-devel
          push: true
          tags: |
            tscholak/text-to-sql-dev:${{ github.sha }}
            tscholak/text-to-sql-dev:cache
            tscholak/text-to-sql-dev:devcontainer
          target: dev
          cache-from: type=registry,ref=tscholak/text-to-sql-dev:cache
          cache-to: type=inline
      - name: Build and push train image
        id: docker_build_train
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.buildx.outputs.name }}
          build-args: |
            BASE_IMAGE=pytorch/pytorch:1.9.0-cuda11.1-cudnn8-devel
          push: true
          tags: |
            tscholak/text-to-sql-train:${{ github.sha }}
            tscholak/text-to-sql-train:cache
          target: train
          cache-from: type=registry,ref=tscholak/text-to-sql-train:cache
          cache-to: type=inline
      - name: Build and push eval image
        id: docker_build_eval
        uses: docker/build-push-action@v2
        with:
          builder: ${{ steps.buildx.outputs.name }}
          build-args: |
            BASE_IMAGE=pytorch/pytorch:1.9.0-cuda11.1-cudnn8-devel
          push: true
          tags: |
            tscholak/text-to-sql-eval:${{ github.sha }}
            tscholak/text-to-sql-eval:cache
          target: eval
          cache-from: type=registry,ref=tscholak/text-to-sql-eval:cache
          cache-to: type=inline
