# Auto-deploy OCI Deal Accelerator skill to MCP server on every push to main.
# Requires secrets: OCI_SSH_KEY (private key), OCI_HOST (DNS alias).
name: Deploy Skill to OCI

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    timeout-minutes: 5

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

      - name: Setup SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.OCI_SSH_KEY }}" > ~/.ssh/deploy_key
          chmod 600 ~/.ssh/deploy_key
          ssh-keyscan -H ${{ secrets.OCI_HOST }} >> ~/.ssh/known_hosts 2>/dev/null

      - name: Sync skill to server
        run: |
          tar --exclude='.git' --exclude='.gitea' -czf /tmp/skill.tar.gz .
          scp -i ~/.ssh/deploy_key /tmp/skill.tar.gz ubuntu@${{ secrets.OCI_HOST }}:/tmp/skill.tar.gz
          ssh -i ~/.ssh/deploy_key ubuntu@${{ secrets.OCI_HOST }} '
            rm -rf /home/ubuntu/oci-deal-accelerator-oracle/oci-deal-accelerator/*
            tar -xzf /tmp/skill.tar.gz -C /home/ubuntu/oci-deal-accelerator-oracle/oci-deal-accelerator/
            rm /tmp/skill.tar.gz
          '

      - name: Restart containers
        run: |
          ssh -i ~/.ssh/deploy_key ubuntu@${{ secrets.OCI_HOST }} '
            cd /home/ubuntu/skills-platform &&
            docker compose -f docker-compose.prod.yaml restart tools-service mcp-deal-accelerator
          '

      - name: Wait for healthy
        run: |
          ssh -i ~/.ssh/deploy_key ubuntu@${{ secrets.OCI_HOST }} '
            timeout 60 bash -c "
              until docker inspect --format=\"{{.State.Health.Status}}\" mcp-deal-accelerator 2>/dev/null | grep -q healthy; do
                sleep 3
              done
            "
          '
