name: '🏗️ Build Plugin'

on:
  push:
    branches:
      - master
    paths-ignore:
      - '**/*.md'
  pull_request:
    branches:
      - master
    paths-ignore:
      - '**/*.md'
  workflow_dispatch:

jobs:
  build:
    runs-on: linux/amd64
    container:
      image: gitea.tourolle.paris/dtourolle/srfplay-builder:latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          path: build-${{ github.run_id }}

      - name: Restore dependencies
        working-directory: build-${{ github.run_id }}
        run: dotnet restore Jellyfin.Plugin.SRFPlay.sln

      - name: Build solution
        working-directory: build-${{ github.run_id }}
        run: dotnet build Jellyfin.Plugin.SRFPlay.sln --configuration Release --no-restore --no-self-contained /m:1

      - name: Run tests
        working-directory: build-${{ github.run_id }}
        run: dotnet test Jellyfin.Plugin.SRFPlay.sln --no-build --configuration Release --verbosity normal

      - name: Build Jellyfin Plugin
        id: jprm
        working-directory: build-${{ github.run_id }}
        run: |
          mkdir -p artifacts
          jprm --verbosity=debug plugin build .
          ARTIFACT=$(find . -name "*.zip" -type f -print -quit | sed 's|^\./||')
          LATEST="artifacts/srfplay_latest.zip"
          cp "${ARTIFACT}" "${LATEST}"
          echo "artifact=${LATEST}" >> $GITHUB_OUTPUT
          echo "Found artifact: ${ARTIFACT} -> ${LATEST}"

      - name: Upload build artifact
        uses: actions/upload-artifact@v3
        with:
          name: jellyfin-srfplay-plugin
          path: build-${{ github.run_id }}/${{ steps.jprm.outputs.artifact }}
          retention-days: 30
          if-no-files-found: error

      - name: Cleanup
        if: always()
        run: rm -rf build-${{ github.run_id }}
