name: Docker Publish

on:
  push:
    branches:
      - main
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: docker-publish-${{ github.ref }}
  cancel-in-progress: true

jobs:
  prepare:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version.outputs.value }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Read application version
        id: version
        shell: bash
        run: |
          VERSION="$(node -p "require('./package.json').version")"
          echo "value=$VERSION" >> "$GITHUB_OUTPUT"

  build:
    needs: prepare
    strategy:
      fail-fast: false
      matrix:
        include:
          - runner: ubuntu-latest
            platform: linux/amd64
            artifact: amd64
          - runner: ubuntu-24.04-arm
            platform: linux/arm64
            artifact: arm64
    runs-on: ${{ matrix.runner }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push architecture image by digest
        id: build
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: ${{ matrix.platform }}
          push: true
          outputs: type=image,name=kuekhaoyang/kvideo,push-by-digest=true,name-canonical=true,push=true
          cache-from: type=gha,scope=${{ matrix.artifact }}
          cache-to: type=gha,mode=max,scope=${{ matrix.artifact }}

      - name: Export digest
        shell: bash
        env:
          DIGEST: ${{ steps.build.outputs.digest }}
        run: |
          mkdir -p /tmp/digests
          touch "/tmp/digests/${DIGEST#sha256:}"

      - name: Upload digest
        uses: actions/upload-artifact@v4
        with:
          name: digest-${{ matrix.artifact }}
          path: /tmp/digests/*
          if-no-files-found: error
          retention-days: 1

  merge:
    needs:
      - prepare
      - build
    runs-on: ubuntu-latest
    steps:
      - name: Download digests
        uses: actions/download-artifact@v4
        with:
          pattern: digest-*
          path: /tmp/digests
          merge-multiple: true

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Create and push multi-architecture tags
        working-directory: /tmp/digests
        shell: bash
        env:
          VERSION: ${{ needs.prepare.outputs.version }}
        run: |
          docker buildx imagetools create \
            --tag "kuekhaoyang/kvideo:$VERSION" \
            --tag "kuekhaoyang/kvideo:latest" \
            $(printf 'kuekhaoyang/kvideo@sha256:%s ' *)

      - name: Inspect published image
        env:
          VERSION: ${{ needs.prepare.outputs.version }}
        run: docker buildx imagetools inspect "kuekhaoyang/kvideo:$VERSION"
