name: Publish Scrypted

on:
  workflow_dispatch:
    inputs:
      docker_tag:
        description: 'Docker Tag'
        required: true
      package_version:
        description: 'Package Version'
        required: false
  release:
    types: [published]

jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest
    strategy:
      matrix:
        BASE: ["18-bullseye-full", "18-bullseye-lite", "18-bullseye-thin"]
        SUPERVISOR: ["", ".s6"]
    steps:
      - name: Check out the repo
        uses: actions/checkout@v3
      
      - name: get-npm-version
        id: package-version
        uses: martinbeentjes/npm-get-version-action@master
        with:
          path: server

      - name: Print Version
        run: echo "Version ${{ github.event.inputs.package_version || steps.package-version.outputs.current-version }}"

      - name: Get current date
        id: date
        run: echo "::set-output name=date::$(date +'%Y-%m-%d')"

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
        
      - 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)
        uses: docker/build-push-action@v3
        with:
          build-args: |
            BASE=${{ matrix.BASE }}
            SCRYPTED_INSTALL_VERSION=${{ github.event.inputs.package_version }}
          context: docker/
          file: docker/Dockerfile${{ matrix.SUPERVISOR }}
          platforms: linux/amd64,linux/arm64,linux/armhf
          push: true
          tags: |
            ${{ format('koush/scrypted:{0}{1}-v{2}', matrix.BASE, matrix.SUPERVISOR, github.event.inputs.package_version || steps.package-version.outputs.current-version) }}
            ${{ matrix.BASE == '18-bullseye-full' && matrix.SUPERVISOR == '.s6' && format('koush/scrypted:{0}', github.event.inputs.docker_tag) || '' }}
            ${{ github.event.inputs.docker_tag == 'latest' && matrix.BASE == '18-bullseye-lite' && matrix.SUPERVISOR == '' && 'koush/scrypted:lite' || '' }}
            ${{ github.event.inputs.docker_tag == 'latest' && matrix.BASE == '18-bullseye-thin' && matrix.SUPERVISOR == '' && 'koush/scrypted:thin' || '' }}

            ${{ format('ghcr.io/koush/scrypted:{0}{1}-v{2}', matrix.BASE, matrix.SUPERVISOR, github.event.inputs.package_version || steps.package-version.outputs.current-version) }}
            ${{ matrix.BASE == '18-bullseye-full' && matrix.SUPERVISOR == '.s6' && format('ghcr.io/koush/scrypted:{0}', github.event.inputs.docker_tag) || '' }}
            ${{ github.event.inputs.docker_tag == 'latest' && matrix.BASE == '18-bullseye-lite' && matrix.SUPERVISOR == '' && 'ghcr.io/koush/scrypted:lite' || '' }}
            ${{ github.event.inputs.docker_tag == 'latest' && matrix.BASE == '18-bullseye-thin' && matrix.SUPERVISOR == '' && 'ghcr.io/koush/scrypted:thin' || '' }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
