name: Build

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

on:
  push:
    branches: [ master, release* ]
  pull_request:
    branches: [ master, release* ]
  workflow_dispatch:

jobs:
  run-build-prod:
    name: Run production build
    runs-on: ubuntu-latest

    steps:
      - name: Check out Git repository
        uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4.1.5

      - name: Setup node environment
        uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
        with:
          node-version: 20
          check-latest: true
          cache: npm

      - name: Install Node.js dependencies
        run: npm ci --no-audit

      - name: Run a production build
        env:
          JELLYFIN_VERSION: ${{ github.event.pull_request.head.sha || github.sha }}
        run: npm run build:production

      - name: Update config.json for testing
        run: |
          jq '.multiserver=true | .servers=["https://demo.jellyfin.org/unstable"]' dist/config.json > dist/config.tmp.json
          mv dist/config.tmp.json dist/config.json

      - name: Upload artifact
        uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3
        with:
          name: jellyfin-web__prod
          path: |
            dist

  pr_context:
    name: Save PR context as artifact
    if: ${{ always() && !cancelled() && github.event_name == 'pull_request' }}
    runs-on: ubuntu-latest
    needs:
      - run-build-prod

    steps:
      - name: Save PR context
        env:
          PR_BRANCH: ${{ github.ref_name }}
          PR_NUMBER: ${{ github.event.number }}
          PR_SHA: ${{ github.event.pull_request.head.sha }}
        run: |
          echo $PR_BRANCH > PR_branch
          echo $PR_NUMBER > PR_number
          echo $PR_SHA > PR_sha

      - name: Upload PR number as artifact
        uses: actions/upload-artifact@65462800fd760344b1a7b4382951275a0abb4808 # v4.3.3
        with:
          name: PR_context
          path: |
            PR_branch
            PR_number
            PR_sha
