name: Build and Deploy

on:
  push:
    branches:
      - main

jobs:
  build-and-push:
    runs-on: ubuntu-latest

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

      - name: Create Kaniko config file
        run: |
          # Escribimos el config en el workspace, que sabemos que existe.
          echo '{"auths":{"${{ secrets.REGISTRY_URL }}":{"username":"${{ secrets.REGISTRY_USER }}","password":"${{ secrets.ACTIONS_PAT }}"}}}' > ${{ gitea.workspace }}/config.json

      - name: Build and Push Backend
        run: |
          docker run --rm \
            -v ${{ gitea.workspace }}:/workspace \
            # Montamos el config desde el workspace a la ruta que Kaniko espera.
            -v ${{ gitea.workspace }}/config.json:/kaniko/.docker/config.json \
            gcr.io/kaniko-project/executor:v1.9.0 \
            --context=/workspace \
            --dockerfile=/workspace/Backend/GestionIntegral.Api/Dockerfile \
            --destination=${{ secrets.REGISTRY_URL }}/${{ gitea.actor }}/${{ toLower(gitea.repository_name) }}-backend:latest \
            --destination=${{ secrets.REGISTRY_URL }}/${{ gitea.actor }}/${{ toLower(gitea.repository_name) }}-backend:${{ gitea.sha_short }} \
            --insecure

      - name: Build and Push Frontend
        run: |
          docker run --rm \
            -v ${{ gitea.workspace }}:/workspace \
            # Hacemos lo mismo para el frontend.
            -v ${{ gitea.workspace }}/config.json:/kaniko/.docker/config.json \
            gcr.io/kaniko-project/executor:v1.9.0 \
            --context=/workspace \
            --dockerfile=/workspace/Frontend/Dockerfile \
            --destination=${{ secrets.REGISTRY_URL }}/${{ gitea.actor }}/${{ toLower(gitea.repository_name) }}-frontend:latest \
            --destination=${{ secrets.REGISTRY_URL }}/${{ gitea.actor }}/${{ toLower(gitea.repository_name) }}-frontend:${{ gitea.sha_short }} \
            --insecure

  deploy:
    runs-on: ubuntu-latest
    needs: build-and-push
    
    steps:
      - name: Deploy to Production
        run: |
          echo "Deploying to production from within the Docker network..."
          
          # Lanzamos un contenedor con docker-compose, lo conectamos a la red de gitea,
          # y le damos acceso al socket de Docker y al directorio del proyecto.
          docker run --rm \
            --network gitea-stack_gitea_net \
            -v /var/run/docker.sock:/var/run/docker.sock \
            -v /opt/gestion-integral:/opt/gestion-integral \
            -w /opt/gestion-integral \
            --env DB_SA_PASSWORD='${{ secrets.DB_SA_PASSWORD_SECRET }}' \
            --env JWT_KEY='${{ secrets.JWT_KEY_SECRET }}' \
            docker/compose:latest \
            sh -c "docker login ${{ secrets.REGISTRY_URL }} -u ${{ secrets.REGISTRY_USER }} -p ${{ secrets.ACTIONS_PAT }} && docker compose pull && docker compose up -d"

          echo "Cleaning up dangling images on host..."
          docker image prune -af