name: Publish Docker image
on:
  workflow_dispatch:
  push:
    branches:
    - main
    - develop
    tags:
    - 'v*'
jobs:

  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, [Linux, ARM], [Linux, ARM64]]
        include:
          - os: ubuntu-latest
            arch: amd64
          - os: [Linux, ARM]
            arch: armv7
          - os: [Linux, ARM64]
            arch: armv8
    steps:
      - name: Clean the workspace
        uses: docker://alpine
        with:
          args: /bin/sh -c "rm -rf /github/workspace/.* || rm -rf /github/workspace/*"
      - name: Checkout
        uses: actions/checkout@v3
        with:
          submodules: recursive
      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_PASS }}

      - name: Set Image Name (development)
        id: name
        if: startsWith(github.ref, 'refs/tags/v') != true
        run: echo "LUXONIS_IMAGE_TAG=${{ github.sha }}-${{ matrix.arch }}" >> $GITHUB_ENV

      - name: Set Image Name (release)
        if: startsWith(github.ref, 'refs/tags/v') == true
        run: echo "LUXONIS_IMAGE_TAG=${GITHUB_REF#refs/*/}-${{ matrix.arch }}" >> $GITHUB_ENV

      - name: Build and push
        uses: docker/build-push-action@v3
        with:
          push: true
          file: ci/Dockerfile
          tags: luxonis/depthai-library:${{ env.LUXONIS_IMAGE_TAG }}

      - name: Cleanup the created image
        run: docker image rm luxonis/depthai-library:${{ env.LUXONIS_IMAGE_TAG }}

  upload:
    needs: [build]
    name: Combine into a single multi-arch image and upload tags
    runs-on: ubuntu-latest
    steps:

      - name: Login to DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USER }}
          password: ${{ secrets.DOCKERHUB_PASS }}

      - name: Get Version
        id: vars
        run: echo ::set-output name=short_ref::${GITHUB_REF#refs/*/}

      - name: Build and push (development)
        if: startsWith(github.ref, 'refs/tags/v') != true
        run: |
          docker manifest create \
          luxonis/depthai-library:${{ github.sha}} \
          --amend luxonis/depthai-library:${{ github.sha }}-amd64 \
          --amend luxonis/depthai-library:${{ github.sha }}-armv7 \
          --amend luxonis/depthai-library:${{ github.sha }}-armv8
          # Push
          docker manifest push luxonis/depthai-library:${{ github.sha }}

      - name: Build and push (release)
        if: startsWith(github.ref, 'refs/tags/v') == true
        run: |
          docker manifest create \
          luxonis/depthai-library:${{ steps.vars.outputs.short_ref }} \
          --amend luxonis/depthai-library:${{ steps.vars.outputs.short_ref }}-amd64 \
          --amend luxonis/depthai-library:${{ steps.vars.outputs.short_ref }}-armv7 \
          --amend luxonis/depthai-library:${{ steps.vars.outputs.short_ref }}-armv8
          # Push
          docker manifest push luxonis/depthai-library:${{ steps.vars.outputs.short_ref }}
          docker manifest create \
          luxonis/depthai-library:latest \
          --amend luxonis/depthai-library:${{ steps.vars.outputs.short_ref }}-amd64 \
          --amend luxonis/depthai-library:${{ steps.vars.outputs.short_ref }}-armv7 \
          --amend luxonis/depthai-library:${{ steps.vars.outputs.short_ref }}-armv8
          docker manifest push luxonis/depthai-library:latest
