# When pushing a tag. this workflow will trigger site deployment and fixed version address comments

name: Deploy website
on:
  push:
    tags:
      - '5.*'
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build-site:
    runs-on: ubuntu-latest
    if: (startsWith(github.ref, 'refs/tags/') && (contains(github.ref_name, '-') == false)) || github.event_name == 'workflow_dispatch'
    steps:
      - name: checkout
        uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2

      - run: bun install

      - name: build site
        run: bun run predeploy
        env:
          NODE_OPTIONS: --max_old_space_size=4096

      - name: build dist and bundle analyzer report
        run: bun run dist
        env:
          ANALYZER: 1
          NODE_OPTIONS: --max_old_space_size=4096

      - name: move report.html to _site
        run: |
          if [ -f report.html ]; then
            mv report.html _site && echo "report.html moved to _site"
          fi

      - name: upload site artifact
        uses: actions/upload-artifact@v4
        with:
          name: real-site
          path: _site/
          retention-days: 1 # Not need to keep for too long

  deploy-to-pages:
    runs-on: ubuntu-latest
    needs: build-site
    steps:
      - uses: oven-sh/setup-bun@v2

      - name: download site artifact
        uses: actions/download-artifact@v4
        with:
          name: real-site
          path: _site

      - name: Get version
        id: publish-version
        run: echo "VERSION=$(echo ${{ github.ref_name }} | sed 's/\./-/g')" >> $GITHUB_OUTPUT

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v4
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./_site
          exclude_files: ./_site/report.html # 👈 这个功能是 beta, 但即便不排除,也不 care
          force_orphan: true

      # Since force_orphan will not trigger Sync to Gitee, we need to force run it here
      - name: Sync to Gitee
        uses: wearerequired/git-mirror-action@v1
        env:
          SSH_PRIVATE_KEY: ${{ secrets.GITEE_SSH_PRIVATE_KEY }}
        with:
          source-repo: 'git@github.com:ant-design/ant-design.git'
          destination-repo: 'git@gitee.com:ant-design/ant-design.git'

      - name: Deploy to Surge (with TAG)
        run: |
          export DEPLOY_DOMAIN=ant-design-${{ steps.publish-version.outputs.VERSION }}.surge.sh
          bunx surge --project ./_site --domain $DEPLOY_DOMAIN --token ${{ secrets.SURGE_TOKEN }}

      - name: Create Commit Comment
        uses: peter-evans/commit-comment@v3
        with:
          body: |
            - Documentation site for this release: https://ant-design-${{ steps.publish-version.outputs.VERSION }}.surge.sh
            - Webpack bundle analyzer report page: https://ant-design-${{ steps.publish-version.outputs.VERSION }}.surge.sh/report.html

  # https://github.com/ant-design/ant-design/pull/49213/files#r1625446496
  upload-to-release:
    runs-on: ubuntu-latest
    # 仅在 tag 的时候工作,因为我们要将内容发布到以 tag 为版本号的 release 里
    if: startsWith(github.ref, 'refs/tags/')
    needs: build-site
    steps:
      - name: download site artifact
        uses: actions/download-artifact@v4
        with:
          name: real-site
          path: _site

      - name: Tarball site
        run: |
          cd ./_site
          VERSION=$(echo ${{ github.ref_name }} | sed 's/\./-/g')
          tar -czf ../website.tar.gz --transform 's|^|antd-${VERSION}-website/|' .
          cd ..

      - name: Upload to Release
        uses: softprops/action-gh-release@01570a1f39cb168c169c802c3bceb9e93fb10974 # v2.1.0
        with:
          fail_on_unmatched_files: true
          files: website.tar.gz