name: deploy-to-gcloud

on:
  push:
    branches: [ production ]  # Solo desplegar cuando se pushee a rama production

jobs:
  build-and-deploy:
    runs-on: docker
    env:
      REG: gitea.nucleoriofrio.com/nucleo000

    steps:
      - uses: actions/checkout@v3
      
      # Build y push imagen (igual que antes)
      - uses: docker/setup-buildx-action@v2
      - uses: docker/login-action@v2
        with:
          registry: gitea.nucleoriofrio.com
          username: nucleo000
          password: 7bc7b2fcd283bd6a251bef3ede368b7f897c919d

      - name: Build and push snatchgame
        run: |
          docker build -f deploy/Dockerfile -t $REG/snatchgame:latest .
          docker push $REG/snatchgame:latest

      # Deploy a Google Cloud VM
      - name: Deploy to Google Cloud VM
        run: |
          # Instalar gcloud CLI si no está disponible
          if ! command -v gcloud &> /dev/null; then
            curl https://sdk.cloud.google.com | bash
            exec -l $SHELL
            source /root/google-cloud-sdk/path.bash.inc
          fi
          
          # Configurar autenticación (necesitarás configurar service account)
          # echo '${{ secrets.GCLOUD_SERVICE_KEY }}' | gcloud auth activate-service-account --key-file=-
          # gcloud config set project tu-proyecto-gcloud
          
          # Ejecutar deployment en la VM
          gcloud compute ssh snatchgame-vm \
            --zone=us-central1-a \
            --command="sudo /opt/deploy-snatchgame.sh" \
            || echo "Deploy failed - VM might not exist yet"