name: Publish Docker

on:
  workflow_dispatch: ~
  push:
    branches: [master, beta, nightly]
    tags: [v*]

jobs:
  build-docker:
    name: Build Docker Image
    runs-on: ubuntu-latest
    if: ${{ !contains(github.event.head_commit.message, '[skip ci]') }}
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Prepare
        id: prepare
        run: |
          if [[ $GITHUB_REF == refs/tags/* ]]; then
            echo "tag=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
          elif [[ $GITHUB_REF == refs/heads/master ]]; then
            echo "tag=latest" >> $GITHUB_OUTPUT
          else
            echo "tag=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT
          fi
          if [[ $GITHUB_REF == refs/tags/*-beta ]]; then
            echo "branch=beta" >> $GITHUB_OUTPUT
          elif [[ $GITHUB_REF == refs/tags/* ]]; then
            echo "branch=master" >> $GITHUB_OUTPUT
          else
            echo "branch=${GITHUB_REF#refs/heads/}" >> $GITHUB_OUTPUT
          fi
          echo "commit=${GITHUB_SHA}" >> $GITHUB_OUTPUT
          echo "docker_platforms=linux/amd64,linux/arm64/v8,linux/arm/v7,linux/arm/v6" >> $GITHUB_OUTPUT
          echo "docker_image=${{ secrets.DOCKER_REPO }}/tautulli" >> $GITHUB_OUTPUT

      - name: Set Up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        id: buildx
        with:
          version: latest

      - name: Cache Docker Layers
        uses: actions/cache@v4
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-

      - name: Login to DockerHub
        uses: docker/login-action@v3
        if: success()
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_TOKEN }}

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v3
        if: success()
        with:
          registry: ghcr.io
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.GHCR_TOKEN }}

      - name: Extract Docker Metadata
        id: metadata
        uses: docker/metadata-action@v5
        with:
          images: ${{ steps.prepare.outputs.docker_image }}

      - name: Docker Build and Push
        uses: docker/build-push-action@v6
        if: success()
        with:
          context: .
          file: ./Dockerfile
          push: true
          platforms: ${{ steps.prepare.outputs.docker_platforms }}
          build-args: |
            TAG=${{ steps.prepare.outputs.tag }}
            BRANCH=${{ steps.prepare.outputs.branch }}
            COMMIT=${{ steps.prepare.outputs.commit }}
          tags: |
            ${{ steps.prepare.outputs.docker_image }}:${{ steps.prepare.outputs.tag }}
            ghcr.io/${{ steps.prepare.outputs.docker_image }}:${{ steps.prepare.outputs.tag }}
          labels: ${{ steps.metadata.outputs.labels }}
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache

  discord:
    name: Discord Notification
    needs: build-docker
    if: always() && !contains(github.event.head_commit.message, '[skip ci]')
    runs-on: ubuntu-latest
    steps:
      - name: Post Status to Discord
        uses: sarisia/actions-status-discord@v1
        with:
          webhook: ${{ secrets.DISCORD_WEBHOOK }}
          status: ${{ needs.build-docker.result == 'success' && 'success' || contains(needs.*.result, 'failure') && 'failure' || 'cancelled' }}
          title: ${{ github.workflow }}
          nofail: true
