name: Build and Push

on:
  push:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      DOCKER_HOST: tcp://localhost:2375
    steps:
      - uses: actions/checkout@v4

      - name: Log in to Gitea Container Registry
        uses: docker/login-action@v3
        with:
          registry: gitea.kazcloud.dev
          username: ${{ secrets.REGISTRY_USERNAME }}
          password: ${{ secrets.REGISTRY_PASSWORD }}

      - name: Build and push
        env:
          DOCKER_HOST: tcp://localhost:2375
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: gitea.kazcloud.dev/${{ gitea.repository }}:latest
      - name: Install kubectl
        run: |
          curl -LO https://dl.k8s.io/release/v1.35.0/bin/linux/amd64/kubectl
          chmod +x kubectl
          mv kubectl /usr/local/bin/kubectl
      - name: Configure kubectl
        run: |
          mkdir -p ~/.kube
          echo "${{ secrets.KUBE_CONFIG }}" | base64 -d > ~/.kube/config
          sed -i 's/127.0.0.1/100.101.31.99/g' ~/.kube/config # Use control plane IP
      - name: Deploy to k3s
        run: |
          kubectl apply -f deployment.yaml
          kubectl rollout restart deployment/ry-site -n default
