name: Build Next.js release artifact

on:
  workflow_dispatch:
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    env:
      NEXT_TELEMETRY_DISABLED: 1
      DEPLOYMENT_VERSION: ${{ github.sha }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm
      - run: npm ci
      - run: npm test --if-present
      - run: npm run build
      - name: Assemble standalone artifact
        run: |
          cp -R public .next/standalone/public
          mkdir -p .next/standalone/.next
          cp -R .next/static .next/standalone/.next/static
          tar -C .next/standalone -czf nextjs-${GITHUB_SHA}.tar.gz .
      - uses: actions/upload-artifact@v4
        with:
          name: nextjs-${{ github.sha }}
          path: nextjs-${{ github.sha }}.tar.gz
          if-no-files-found: error
          retention-days: 14
