name: star-cloud-deploy-demo

on:
  push:
    branches:
      - demo

jobs:
  deploy-demo:
    runs-on: ubuntu-latest  # 使用機器人預設宣告的標籤
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Step 0 - Prepare Environment & Sync Configs
        run: |
          # 1. 確保 Runner 暫存區結構正確 (清理可能因先前失敗誤建的目錄)
          if [ -d "docker/emqx/acl.conf" ]; then
            echo "Removing docker/emqx/acl.conf directory in runner..."
            rm -rf "docker/emqx/acl.conf"
          fi
          mkdir -p docker/emqx

          # 2. 檢查並清理宿主機 (Host) 上被 Docker 誤建為目錄的檔案
          docker run --rm -v /workspace/mama/star-cloud:/target busybox sh -c "
            if [ -d /target/.env ]; then echo 'Removing .env directory on host'; rm -rf /target/.env; fi
            if [ -d /target/docker/emqx/acl.conf ]; then echo 'Removing acl.conf directory on host'; rm -rf /target/docker/emqx/acl.conf; fi
          "

          # 3. 從宿主機載入持久化的 .env 檔案 (若無則複製 .env.example)
          docker run --rm -v /workspace/mama/star-cloud:/data:ro busybox cat /data/.env > .env || cp .env.example .env

          # 4. 將 Runner 最新 Checkout 的設定檔同步至宿主機持久區，確保後續 compose 掛載正確
          docker run --rm -v /workspace/mama/star-cloud:/target busybox mkdir -p /target/docker/emqx
          cat docker/emqx/acl.conf | docker run -i --rm -v /workspace/mama/star-cloud:/target busybox sh -c "cat > /target/docker/emqx/acl.conf"


      - name: Step 1 - Build & Deploy Containers
        run: |
          WWWGROUP=1000 WWWUSER=1000 docker compose -f compose.yaml up -d --build --wait

      - name: Step 2 - Deploy Code
        run: |
          echo ">>> Syncing code to star-cloud-laravel..."
          # 1. 打包：|| [ $? -le 1 ] 讓 exit 1 (file changed 警告) 通過，exit 2+ 才視為錯誤
          tar --exclude='.git' --exclude='node_modules' --exclude='vendor' \
              --exclude='storage' --exclude='bootstrap/cache' \
              -cf /tmp/deploy.tar . || [ $? -le 1 ]

          # 2. 解壓至容器 (三個容器共享 volume，同步一次即可)
          docker exec -i star-cloud-laravel tar -xf - -C /var/www/html/ < /tmp/deploy.tar
          rm -f /tmp/deploy.tar

          # 3. 設定權限
          docker exec star-cloud-laravel sh -c "
            chown -R 1000:1000 /var/www/html &&
            mkdir -p /.npm && chown -R 1000:1000 /.npm &&
            rm -rf /var/www/html/node_modules
          "

      - name: Step 3 - Composer & NPM & Initialization
        run: |
           docker exec -u 1000:1000 -w /var/www/html star-cloud-laravel sh -c "
            composer install --no-dev --optimize-autoloader --no-interaction &&
            npm install &&
            npm run build &&
            php artisan migrate --force &&
            php artisan storage:link &&
            php artisan optimize:clear &&
            php artisan optimize &&
            php artisan view:cache &&
            php artisan queue:restart &&
            php artisan db:seed --class=RoleSeeder --force &&
            php artisan db:seed --class=AdminUserSeeder --force
           "
           docker exec star-cloud-laravel chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache

      - name: Step 4 - Restart Long-running Workers
        run: |
          # 重啟 mqtt:listen 常駐行程以載入最新 PHP 程式碼
          docker restart star-cloud-mqtt-worker

      - name: Step 5 - Cleanup
        run: docker image prune -f
