name: 部署到群晖

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    # 清除可能存在的代理环境变量，防止 Docker 通过代理拉取镜像
    env:
      HTTP_PROXY: ""
      HTTPS_PROXY: ""
      http_proxy: ""
      https_proxy: ""
      NO_PROXY: "*"
      no_proxy: "*"
    steps:
      - name: 安装 Docker CLI
        run: |
          # 先写干净的 daemon.json，让 Docker 启动时就读到无代理配置
          mkdir -p /etc/docker
          echo '{}' > /etc/docker/daemon.json
          # 替换 Debian 源为清华镜像，避免国内连 deb.debian.org 超时
          sed -i 's|deb.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true
          sed -i 's|security.debian.org|mirrors.tuna.tsinghua.edu.cn/debian-security|g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true
          apt-get update -qq
          apt-get install -y -qq docker.io

      - name: 拉取代码
        uses: actions/checkout@v4

      - name: 构建镜像
        run: docker build -t telegram-downloader:latest .

      - name: 停止旧容器
        run: |
          docker stop telegram-downloader 2>/dev/null || true
          docker rm telegram-downloader 2>/dev/null || true

      - name: 启动新容器
        run: |
          docker run -d \
            --name telegram-downloader \
            --restart unless-stopped \
            -p 15001:5001 \
            -v /volume1/docker/apps/telegram-downloader/appdata:/app/appdata \
            -v /volume2/save/tgdowload:/app/downloads \
            telegram-downloader:latest

      - name: 部署完成提示
        run: |
          echo "✅ 部署完成，浏览器访问 http://192.168.1.66:15001 查看"
