name: deploying-doc
run-name: Deploying doc — ${{ gitea.run_id }}
on:
  workflow_dispatch:
  schedule:
    - cron: 30 04 * * 1,4
  pull_request:
    types: [closed]
    branches:
      - main

jobs:
  build-and-deploy:
      runs-on: ubuntu-latest
      permissions:
        contents: read
        deployments: write
      steps:
          - name: 🚚 Checkout
            uses: actions/checkout@v4
            with:
              fetch-depth: 0

          - uses: pnpm/action-setup@v4
            name: 🔧Install pnpm
            with:
              version: 10
              run_install: false

          - name: 🔧Install Node.js
            uses: actions/setup-node@v4
            with:
              node-version: 20
              cache: 'pnpm'

          - name: 🔧Install dependencies
            run: pnpm install

          - name: 📥 Download mdluploader
            run: wget http://img-kodo.lionhao.top/rust_release/mdluploader.tar.gz -O /tmp/mdluploader.tar.gz

          - name: 📂 Extract mdluploader
            run: tar -xzf /tmp/mdluploader.tar.gz -C /tmp

          - name: 📤 Upload images to S3
            run: |
              /tmp/mdluploader upload ./src \
                --bucket ${{ secrets.S3_BUCKET }} \
                --ak ${{ secrets.S3_ACCESS_KEY }} \
                --sk ${{ secrets.S3_SECRET_KEY }} \
                --region ${{ secrets.S3_REGION }} \
                --endpoint ${{ secrets.S3_ENDPOINT }} \
                --remote-root ${{ secrets.S3_REMOTE_ROOT }} \
                --domain ${{ secrets.S3_DOMAIN }} \
            env:
              RUST_LOG: info

          - name: 🔴Build doc
            run: pnpm run docs:build
            
          - name: 🚀 Deploy to Cloudflare Pages
            uses: cloudflare/wrangler-action@v3
            with:
              apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
              accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
              command: pages deploy ./src/.vuepress/dist --project-name=jingji-reference --branch=main
