# 使用 screen 部署 Django 项目

when:
  branch: main
  event: push

steps:
  - name: test
    image: ghcr.io/astral-sh/uv:python3.14-bookworm
    commands:
      # 安装依赖
      - uv sync
      
      # 数据库迁移
      - uv run python manage.py migrate
      
      # 运行测试
      - uv run python manage.py test

  - name: deploy-with-screen
    image: appleboy/drone-ssh
    settings:
      host: 
        from_secret: deploy_host
      username:
        from_secret: deploy_user
      password:
        from_secret: deploy_password
      port: 22
      script:
        - cd /root/project/erp
        - git pull origin main
        - uv sync
        - uv run python manage.py migrate
        - |
          # 停止旧的 screen 会话
          screen -S erp -X quit || true
          # 等待进程完全退出
          sleep 2
          # 在新的 screen 会话中启动服务
          screen -dmS erp uv run python manage.py runserver 0.0.0.0:8000
          # 验证 screen 会话是否创建成功
          screen -ls | grep erp && echo "ERP server started in screen session"
    when:
      evaluate: 'CI_PIPELINE_STATUS == "success"'
