name: Publish container

on:
  release:
    types: [published]
  workflow_dispatch:

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository_owner }}/jellyfin-intro-skipper

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    strategy:
      matrix:
        node-version: [18.x]
        jellyfin-container-version: [10.8.9]
        jellyfin-web-version: [10.8.9]

    steps:
      - uses: actions/checkout@v3

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}

      - name: Get npm cache directory
        id: npm-cache-dir
        run: |
          echo "dir=$(npm config get cache)" >> $GITHUB_OUTPUT
      - name: Configure npm cache
        uses: actions/cache@v3
        id: npm-cache
        with:
          path: ${{ steps.npm-cache-dir.outputs.dir }}
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-
      - name: Checkout modified web interface
        uses: actions/checkout@v3
        with:
          repository: ConfusedPolarBear/jellyfin-web
          ref: intros
          path: web

      - name: Store commit of web interface
        id: web-commit
        run: |
          cd web
          echo "commit=$(git log -1 --format='%H' | cut -c -10)" >> $GITHUB_OUTPUT
      - name: Build and copy web interface
        run: |
          cd web
          npm install
          cp -r dist ../docker/
          tar czf dist.tar.gz dist
      - name: Upload web interface
        uses: actions/upload-artifact@v3.1.2
        with:
          name: jellyfin-web-${{ matrix.jellyfin-web-version }}+${{ steps.web-commit.outputs.commit }}.tar.gz
          path: web/dist.tar.gz
          if-no-files-found: error

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

      - name: Setup Docker buildx
        uses: docker/setup-buildx-action@v2

      # Login against a Docker registry except on PR
      # https://github.com/docker/login-action
      - name: Log into ghcr.io registry
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v2.1.0
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      # Extract metadata (tags, labels) for Docker
      # https://github.com/docker/metadata-action
      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          tags: |
            type-raw,value=${{ steps.web-commit.outputs.commit }}
            type=raw,value=latest,enable={{is_default_branch}}
            type=semver,pattern={{version}},value=${{ matrix.jellyfin-container-version }}
      # Build and push Docker image with Buildx
      # https://github.com/docker/build-push-action
      - name: Publish container image
        id: build-and-push
        uses: docker/build-push-action@v4.0.0
        with:
          file: docker/Dockerfile
          context: docker
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
          build-args: |
            JELLYFIN_TAG=${{ matrix.jellyfin-container-version }}
          platforms: |
            linux/amd64
            linux/arm/v7
            linux/arm64/v8
          push: ${{ github.event_name != 'pull_request' }}
          pull: true
          no-cache: true
          cache-from: type=gha
          cache-to: type=gha,mode=max
          provenance: false