name: Deploy web clipper extension

on:
  push:
    branches:
      - main
    paths:
      - "apps/web-clipper/**"
    tags:
      - "web-clipper-v*"

  pull_request:
    paths:
      - "apps/web-clipper/**"

permissions:
  contents: write
  discussions: write

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: ubuntu-latest
    name: Build web clipper extension

    permissions:
      contents: read
      deployments: write

    steps:
      - uses: actions/checkout@v6
      - uses: pnpm/action-setup@v6
      - name: Set up node & dependencies
        uses: actions/setup-node@v6
        with:
          node-version: 24
          cache: "pnpm"

      - name: Install dependencies
        run: pnpm install --filter web-clipper --frozen-lockfile --ignore-scripts

      - name: Build the web clipper extension
        run: |
          pnpm --filter web-clipper zip
          pnpm --filter web-clipper zip:firefox

      - name: Upload build artifacts
        uses: actions/upload-artifact@v7
        if: ${{ !startsWith(github.ref, 'refs/tags/web-clipper-v') }}
        with:
          name: web-clipper-extension
          path: apps/web-clipper/.output/*.zip
          include-hidden-files: true
          if-no-files-found: error
          compression-level: 0

      - name: Release web clipper extension
        uses: softprops/action-gh-release@v3.0.0
        if: ${{ startsWith(github.ref, 'refs/tags/web-clipper-v') }}
        with:
          draft: false
          fail_on_unmatched_files: true
          files: apps/web-clipper/.output/*.zip
          discussion_category_name: Releases
          make_latest: false
          token: ${{ secrets.RELEASE_PAT }}
