name: Deploy To Dev

on:
  push:
    branches:
      - 'dev'

jobs:
  deploy:
    runs-on: ubuntu-22.04
    container:
      volumes:
        - /app/lighttpd-content:/app/xsy-www

    steps:
      - name: Checkout code 
        uses: actions/checkout@v4

      - name: Copy specific directory to runner's host machine
        run: |
          TARGET_DIR="./dist"

          # 检查 dist 目录是否存在
          if [ ! -d "$TARGET_DIR" ]; then
            echo "Error: The source directory '$TARGET_DIR' does not exist in the repository."
            # 如果目录不存在，报错并退出当前步骤
            exit 1
          fi
          ls -lha /app/xsy-www/
          rm -rf /app/xsy-www/*
          cp -r ./dist/* /app/xsy-www/

      - name: Find and restart the app container
        run: |
          # 1. 使用 docker ps 过滤包含 'xsy-lighttpd' 服务的容器
          # 2. 提取容器 ID 或名称
          CONTAINER_ID=$(docker ps -a --filter "name=xsy-lighttpd" --format "{{.ID}}")

          if [ -z "$CONTAINER_ID" ]; then
            echo "Error: Could not find any container matching name 'app1'."
            exit 1
          else
            echo "Found container ID: $CONTAINER_ID. Restarting..."
            # 使用标准的 docker restart 命令
            docker restart "$CONTAINER_ID"
            echo "Container restarted successfully."
          fi

