name: Publish container

on:
  release:
    types: [published]
  workflow_dispatch:

env:
  REGISTRY: ghcr.io

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

    strategy:
      matrix:
        node-version: [14.x]

    steps:
    - uses: actions/checkout@v3

    - name: Login to GHCR
      uses: docker/login-action@v2.0.0
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}

    - 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.0
      with:
        name: jellyfin-web-10.8.0+${{ steps.web-commit.outputs.commit }}.tar.gz
        path: web/dist.tar.gz
        if-no-files-found: error

    - name: Publish container
      uses: docker/build-push-action@v3.1.1
      with:
        file: docker/Dockerfile
        context: docker/
        push: true
        tags: ${{ env.REGISTRY}}/confusedpolarbear/jellyfin-intro-skipper:${{ steps.web-commit.outputs.commit }}