kind: pipeline
type: docker
name: xlx-teacher-app-build

# 触发条件：任何分支的 push 都会触发构建
trigger:
  event:
    - push
    - pull_request

# 定义构建步骤
steps:
  # 步骤 1: 安装 pnpm 并缓存依赖
  - name: install-and-build
    image: node:18-alpine
    commands:
      # 设置国内镜像
      - npm config set registry https://registry.npmmirror.com

      # 安装 pnpm (使用项目指定版本)
      - npm install -g pnpm@10.10.0

      # 设置 pnpm 国内镜像和缓存配置
      - pnpm config set registry https://registry.npmmirror.com
      - pnpm config set store-dir /drone/src/.pnpm-store
      - pnpm config set cache-dir /drone/src/.pnpm-cache

      # 显示版本信息
      - node --version
      - npm --version
      - pnpm --version

      # 安装依赖
      - pnpm install --no-frozen-lockfile

      # 构建 H5 版本
      - pnpm run build:h5

      # 显示构建结果
      - ls -la dist/
      - echo "H5 版本构建完成"
    volumes:
      - name: pnpm-store
        path: /drone/src/.pnpm-store
      - name: pnpm-cache
        path: /drone/src/.pnpm-cache
      - name: node-modules
        path: /drone/src/node_modules
      - name: build-h5-output
        path: /drone/src/dist

  - name: deploy-h5
    image: alpine:3.18
    commands:
      # 确保目标目录存在
      - mkdir -p /home/xlx_teacher_deploy/h5

      # 备份当前版本（如果存在）
      - |
        if [ -d /home/xlx_teacher_deploy/h5/backup ]; then
          rm -rf /home/xlx_teacher_deploy/h5/backup
        fi
      - |
        if [ -d /home/xlx_teacher_deploy/h5/assets ] || [ -f /home/xlx_teacher_deploy/h5/index.html ]; then
          mkdir -p /home/xlx_teacher_deploy/h5/backup
          cp -r /home/xlx_teacher_deploy/h5/* /home/xlx_teacher_deploy/h5/backup/ 2>/dev/null || true
          echo "已备份当前 H5 版本到 backup 目录"
        fi

      # 清理目标目录（保留 backup）
      - find /home/xlx_teacher_deploy/h5 -mindepth 1 -maxdepth 1 -not -name backup -exec rm -rf {} +

      # 同步构建文件到目标目录
      - cp -r /drone/src/dist/* /home/xlx_teacher_deploy/h5/

      # 设置正确的文件权限
      - chmod -R 755 /home/xlx_teacher_deploy/h5

      # 显示部署结果
      - ls -la /home/xlx_teacher_deploy/h5/
      - echo "H5 版本部署完成"
    volumes:
      - name: build-h5-output
        path: /drone/src/dist
      - name: deploy-h5-target
        path: /home/xlx_teacher_deploy
    depends_on:
      - install-and-build
    when:
      branch:
        - main
        - master
        - develop
      event:
        - push

# 定义卷挂载，用于缓存和部署
volumes:
  # pnpm 依赖存储缓存
  - name: pnpm-store
    host:
      path: /tmp/drone-cache/xlx-teacher/pnpm-store
  # pnpm 缓存目录
  - name: pnpm-cache
    host:
      path: /tmp/drone-cache/xlx-teacher/pnpm-cache
  # node_modules 缓存
  - name: node-modules
    temp: {}
  # H5 构建输出临时存储
  - name: build-h5-output
    temp: {}
  # 微信小程序构建输出临时存储
  - name: build-mp-output
    temp: {}
  # APP 构建输出临时存储
  - name: build-app-output
    temp: {}
  # H5 部署目标目录
  - name: deploy-h5-target
    host:
      path: /home/xlx_teacher_deploy
  # 微信小程序部署目标目录
  - name: deploy-mp-target
    host:
      path: /home/xlx_teacher_deploy
  # APP 部署目标目录
  - name: deploy-app-target
    host:
      path: /home/xlx_teacher_deploy
  # cache-cleanup pipeline 用到的缓存目录
  - name: cache-dir
    host:
      path: /tmp/drone-cache/xlx-teacher

---
# 清理过期缓存的管道（每周运行一次）
kind: pipeline
type: docker
name: cache-cleanup

trigger:
  cron:
    - cleanup-cache
  event:
    - cron

steps:
  - name: cleanup-old-cache
    image: alpine:3.18
    commands:
      - echo "清理过期的 xlx_teacher_app 缓存..."
      # 删除 7 天前的缓存文件
      - find /tmp/drone-cache/xlx-teacher -type f -mtime +7 -delete 2>/dev/null || true
      - echo "缓存清理完成"
    volumes:
      - name: cache-dir
        path: /tmp/drone-cache/xlx-teacher

volumes:
  - name: cache-dir
    host:
      path: /tmp/drone-cache/xlx-teacher
