name: Build and Push Docker Images

on:
  push:
    branches:
      - main
    tags:
      - 'v*'

env:
  REGISTRY: gitea.zuev.company # Замените на реальный домен вашего Gitea
  BACKEND_IMAGE: zuev/magistr-backend
  FRONTEND_IMAGE: zuev/magistr-frontend

jobs:
  build-and-push-backend:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Log in to the Container registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.ZUEV_TOKEN }} # Нужно создать секрет ZUEV_TOKEN в настройках репозитория (Personal Access Token)

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: ./backend
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: |
            ${{ steps.meta.outputs.labels }}
            org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}
  build-and-push-frontend:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Log in to the Container registry
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.ZUEV_TOKEN }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ env.REGISTRY }}/${{ env.FRONTEND_IMAGE }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: ./frontend
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: |
            ${{ steps.meta.outputs.labels }}
            org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}

  deploy-to-k8s:
    needs: [build-and-push-backend, build-and-push-frontend]
    runs-on: ubuntu-latest
    steps:
      - name: Create kubeconfig
        run: |
          mkdir -p ~/.kube
          echo "${{ secrets.KUBECONFIG_DATA }}" | base64 -d > ~/.kube/config
          chmod 600 ~/.kube/config

      - name: Install kubectl
        run: |
          curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
          chmod +x kubectl
          mv kubectl /usr/local/bin/

      - name: Trigger Kubernetes Rollout
        run: |
          # Перезапускаем поды, чтобы они скачали свежий :main образ
          kubectl rollout restart deployment backend frontend -n magistr
          
          # Ждём успешного обновления (5 минут на backend из-за Spring Boot)
          kubectl rollout status deployment/frontend -n magistr --timeout=120s
          kubectl rollout status deployment/backend -n magistr --timeout=300s

