name: Deployment

concurrency: production

on:
  push:
    branches:
      - main

# To use Turborepo Remote Caching, set the following environment variables.
# env:
#   TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
#   TURBO_TEAM: ${{ secrets.TURBO_TEAM }}

jobs:
  release:
    runs-on: ubuntu-latest
    environment: production
    permissions:
      contents: write
      pull-requests: write
      id-token: write

    steps:
      - uses: googleapis/release-please-action@v4
        id: release
        with:
          # Use a fine grained token for release-please so this workflow
          # can trigger other workflows like release-pr.yml
          # see https://github.com/peter-evans/create-pull-request/issues/48
          token: ${{ secrets.V10_WORKFLOW_TRIGGER_WORKFLOW_GH_TOKEN }}
          config-file: .github/release-please/release-please-config.json
          manifest-file: .github/release-please/.release-please-manifest.json

      - uses: actions/checkout@v5
        # these if statements ensure that a publication only occurs when
        # a new release is created:
        if: ${{ steps.release.outputs.releases_created == 'true' }}
        with:
          fetch-depth: 0 # Fetch all history for all tags and branches

      - name: Setup pnpm
        if: ${{ steps.release.outputs.releases_created == 'true' }}
        uses: pnpm/action-setup@v4

      - uses: actions/setup-node@v5
        if: ${{ steps.release.outputs.releases_created == 'true' }}
        with:
          node-version-file: '.nvmrc'
          registry-url: 'https://registry.npmjs.org'

      - name: Install
        if: ${{ steps.release.outputs.releases_created == 'true' }}
        run: pnpm i --frozen-lockfile

      - name: Cache turbo build setup
        if: ${{ steps.release.outputs.releases_created == 'true' }}
        uses: actions/cache@v5
        with:
          path: .turbo
          key: ${{ runner.os }}-turbo-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-turbo-

      - name: Clean
        if: ${{ steps.release.outputs.releases_created == 'true' }}
        run: pnpm clean

      - name: Build
        if: ${{ steps.release.outputs.releases_created == 'true' }}
        run: pnpm build:packages

      - name: Build CDN bundles
        if: ${{ steps.release.outputs.releases_created == 'true' }}
        run: pnpm build:cdn

      # The site build emits per-framework markdown to site/dist/docs/framework/{html,react}/.
      # The @videojs/html and @videojs/react packages' prepack scripts (fired by `pnpm publish`
      # below) copy that subtree into each package's docs/ directory so it ships in the tarball.
      # build:cli also depends on the site build transitively; turbo caches it, so this step
      # adds no rebuild cost — it just makes the dependency visible in CI.
      - name: Build site (required by html/react prepack and CLI)
        if: ${{ steps.release.outputs.releases_created == 'true' }}
        run: pnpm build:site

      - name: Build CLI
        if: ${{ steps.release.outputs.releases_created == 'true' }}
        run: pnpm build:cli

      - name: Publish
        if: ${{ steps.release.outputs.releases_created == 'true' }}
        run: pnpm -r publish --filter "./packages/*" --access public --provenance --no-git-checks
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Update site/v10 branch
        if: ${{ steps.release.outputs.releases_created == 'true' }}
        run: git push origin HEAD:refs/heads/site/v10 --force
