name: Deploy Production

on:
  push:
    branches: [main]

concurrency:
  group: production-deploy
  cancel-in-progress: true

jobs:
  deploy:
    runs-on: self-hosted

    steps:
      - name: Deploy portfolio Production
        shell: bash
        run: |
          set -euo pipefail

          echo "============================"
          echo "    Starting deployment"
          echo "============================"

          APP_DIR="/srv/apps/portfolio"

          cd "$APP_DIR"

          echo "Fetching latest code..."
          git fetch origin main

          echo "Resetting local state..."
          git reset --hard origin/main

          echo "Cleaning untracked files (keeping .env)"
          git clean -fd -e .env

          echo "Pulling/building containers..."
          docker compose pull || true
          docker compose up -d --build --remove-orphans

          echo "Cleaning unused Docker images..."
          docker image prune -f

          echo "Deployment finished successfully"
