name: Publish Scrypted Common

on:
  workflow_dispatch:

jobs:
  build:
    name: Push Docker image to Docker Hub
    runs-on: self-hosted
#     runs-on: ubuntu-latest
    strategy:
      matrix:
        NODE_VERSION: [
          "18",
          # "20"
        ]
        BASE: ["jammy"]
        FLAVOR: ["full", "lite"]
    steps:
      - name: Check out the repo
        uses: actions/checkout@v3
      
      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up SSH
        uses: MrSquaare/ssh-setup-action@v2
        with:
          host: ${{ secrets.DOCKER_SSH_HOST_ARM64 }}
          private-key: ${{ secrets.DOCKER_SSH_PRIVATE_KEY }}

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
        with:
          platforms: linux/arm64
          append: |
            - endpoint: ssh://${{ secrets.DOCKER_SSH_USER }}@${{ secrets.DOCKER_SSH_HOST_ARM64 }}
              platforms: linux/arm64

      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Login to Github Container Registry
        uses: docker/login-action@v2
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker image (scrypted-common)
        uses: docker/build-push-action@v4
        with:
          build-args: |
            NODE_VERSION=${{ matrix.NODE_VERSION }}
            BASE=${{ matrix.BASE }}
          context: install/docker/
          file: install/docker/Dockerfile.${{ matrix.FLAVOR }}
          platforms: linux/amd64,linux/arm64
          push: true
          tags: |
            koush/scrypted-common:${{ matrix.NODE_VERSION }}-${{ matrix.BASE }}-${{ matrix.FLAVOR }}
            ghcr.io/koush/scrypted-common:${{ matrix.NODE_VERSION }}-${{ matrix.BASE }}-${{ matrix.FLAVOR }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
