name: Publish all @bytelyst/* packages

on:
  workflow_dispatch:

concurrency:
  group: publish-all-${{ github.ref }}
  cancel-in-progress: false

defaults:
  run:
    shell: bash

jobs:
  publish-all:
    runs-on: [ubuntu-latest, bytelyst, hostinger]
    container:
      image: node:20-bookworm@sha256:8f693eaa7e0a8e71560c9a82b55fd54c2ae920a2ba5d2cde28bac7d1c01c9ba5
      options: --network host -v /home/gitea-runner/.gitea_publish_npmrc:/run/secrets/gitea_publish_npmrc:ro
    env:
      PACKAGE_FILTER: '@bytelyst/*'
      DRY_RUN: 'false'

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          github-server-url: https://gitea.bytelyst.com

      - name: Print build context
        run: |
          echo "Ref:     ${{ github.ref }}"
          echo "RefName: ${{ github.ref_name }}"
          echo "Commit:  ${{ github.sha }}"
          echo "Runner:  $(hostname)"
          echo "Package filter: $PACKAGE_FILTER"
          echo "Dry run: $DRY_RUN"
          node --version
          npm --version

      - name: Install pinned pnpm
        run: |
          npm install -g pnpm@10.6.5
          pnpm --version

      - name: Configure publish registry
        run: |
          cp /run/secrets/gitea_publish_npmrc /tmp/publish.npmrc
          chmod 600 /tmp/publish.npmrc
          npm whoami --userconfig /tmp/publish.npmrc --registry https://gitea.bytelyst.com/api/packages/learning_ai_user/npm/

      - name: Install workspace deps
        run: HUSKY=0 pnpm install --frozen-lockfile

      - name: Discover, build, test, pack, publish, and verify queued packages
        run: scripts/gitea/ci-publish-packages.sh

      - name: Compute tarball SHA512 manifest
        run: |
          set -euo pipefail
          cd /tmp/tarballs
          if ls *.tgz >/dev/null 2>&1; then
            sha512sum *.tgz > manifest.sha512
            cat manifest.sha512
          else
            echo "No tarballs produced" > manifest.sha512
            cat manifest.sha512
          fi
