name: Build & Push Docker

on:
  workflow_dispatch:
  release:
    types: [created]
  push:
    tags:
      - v[0-9]+.[0-9]+.[0-9]+*

jobs:
  typecheck:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Use Latest Corepack
        run: |
          npm install -g corepack@latest
          corepack enable && corepack enable pnpm
          pnpm --version

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22.13.0
          cache: "pnpm"

      - name: Install dependencies
        run: pnpm install

      - name: Build server
        run: pnpm turbo typecheck --filter=@tunarr/server

      - name: Build web
        run: pnpm turbo build --filter=@tunarr/web

  build_and_push:
    needs: typecheck
    runs-on: ${{ matrix.builds.os }}
    continue-on-error: true
    strategy:
      max-parallel: 2
      matrix:
        builds:
          - base_tag: "7.1.1"
            platform: linux/amd64
            suffix: ""
            target: linux-x64
            os: ubuntu-latest

          - base_tag: "7.1.1"
            platform: linux/arm64
            suffix: -arm64
            target: linux-arm64
            os: ubuntu-24.04-arm

    steps:
      - uses: actions/checkout@v4

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v5
        with:
          flavor: |
            suffix=${{ matrix.builds.suffix }},onlatest=true
          images: |
            chrisbenincasa/tunarr
            ghcr.io/chrisbenincasa/tunarr
          tags: |
            # Update version tags on proper releases
            type=semver,pattern={{version}}
            # Update edge tag when we manually trigger the release
            type=raw,value=edge,enable=${{github.ref == format('refs/heads/{0}', 'dev') && github.event_name == 'workflow_dispatch'}}
            # Update alpha "latest" on any alpha release.
            type=raw,value=alpha,enable=${{ contains(github.ref_name, '-alpha') || contains(github.event.release.tag_name, '-alpha') }}
            # Update dev floating tag on any alpha release.
            type=raw,value=dev,enable=${{ contains(github.ref_name, '-dev') || contains(github.event.release.tag_name, '-dev') }}

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

      - name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

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

      - uses: actions/cache@v4 # Cache previous HEAD SHA
        with:
          path: previous_head_sha
          key: ${{ runner.os }}-edge-build-${{ github.sha }}
          restore-keys: | # Check for previous SHA in cache
            ${{ runner.os }}-edge-build-

      - name: Generate Version Number
        run: |
          echo "TUNARR_VERSION=${{github.ref_name}}" >> $GITHUB_ENV
          echo "TUNARR_BUILD=$(git rev-parse --short HEAD)" >> $GITHUB_ENV

      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          platforms: ${{matrix.builds.platform}}
          build-args: |
            base_image_tag=${{ matrix.builds.base_tag }}
            is_edge_build=${{github.ref == format('refs/heads/{0}', 'dev') && github.event_name == 'schedule'}}
            tunarr_version=${{ env.TUNARR_VERSION }}
            tunarr_build=${{ env.TUNARR_BUILD }}
            exec_target=${{ matrix.builds.target }}
          target: full-stack
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          cache-from: type=gha
          cache-to: type=gha,mode=max
