name: Deploy Relay

on:
  push:
    branches: [main]
    paths:
      - "repowire/relay/**"
      - "repowire/protocol/**"
      - "repowire/config/**"
      - "repowire/daemon/relay_client.py"
      - "web/**"
      - "Dockerfile"
      - "charts/repowire-relay/**"
      - ".github/workflows/relay.yml"

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v5

      - id: auth
        uses: google-github-actions/auth@v2
        with:
          workload_identity_provider: ${{ secrets.GCP_WIF_PROVIDER }}
          service_account: ${{ secrets.GCP_WIF_SERVICE_ACCOUNT }}

      - uses: google-github-actions/setup-gcloud@v2

      - name: Configure Docker for GCR
        run: gcloud auth configure-docker gcr.io

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: |
            gcr.io/${{ secrets.GCP_PROJECT_ID }}/repowire-relay:${{ github.sha }}
            gcr.io/${{ secrets.GCP_PROJECT_ID }}/repowire-relay:latest
          cache-from: type=registry,ref=gcr.io/${{ secrets.GCP_PROJECT_ID }}/repowire-relay:latest
          cache-to: type=inline

      - uses: google-github-actions/get-gke-credentials@v2
        with:
          cluster_name: ${{ secrets.GKE_CLUSTER }}
          location: ${{ secrets.GKE_REGION }}

      - name: Deploy
        run: |
          helm upgrade --install repowire-relay charts/repowire-relay \
            --namespace repowire \
            --set image.tag=${{ github.sha }} \
            --set image.repository=gcr.io/${{ secrets.GCP_PROJECT_ID }}/repowire-relay
          kubectl rollout status deployment/repowire-relay -n repowire --timeout=120s
