name: Publish Official Plugins

# 构建官方插件并把产物(bundle + 清单)强推到孤儿分支 plugins-dist。
# 与 github-pages.yml 一致,在打版本 tag 时触发 —— 发版时 web 与官方插件一起发布。
# 前端经 jsDelivr 从该分支远程拉取;构建产物不进 main、不进 git。
on:
  push:
    tags: ["v*"]
  workflow_dispatch:

permissions:
  contents: write

concurrency:
  group: publish-plugins
  cancel-in-progress: false

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 22

      - name: Install build dependencies
        working-directory: plugins/canvas/registry
        run: npm install --no-audit --no-fund

      - name: Build official plugins
        working-directory: plugins/canvas/registry
        run: npm run build

      - name: Publish to plugins-dist branch
        working-directory: plugins/canvas/registry/dist
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git init -q
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git checkout -q -b plugins-dist
          git add -A
          git commit -q -m "publish official plugins @ ${GITHUB_SHA::7}"
          git push -f "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" plugins-dist
