name: Cluster Run (K3d)

on: [push, pull_request]

# Cancel in progress workflows on pull_requests.
# https://docs.github.com/en/actions/using-jobs/using-concurrency#example-using-a-fallback-value
concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

env:
  ECHO_SERVER_HOST_URL: http://host.k3d.internal:18080

jobs:
  # In order to be able to have required checks, a workflow can't be entirely
  # skipped: see https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/collaborating-on-repositories-with-code-quality-features/troubleshooting-required-status-checks#handling-skipped-but-required-checks
  paths-filter:
    name: "Changed files?"
    runs-on: ubuntu-latest
    outputs:
      matches: ${{ steps.filter.outputs.matches }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 2  # important, to fetch previous commit

      # workaround for https://github.com/dorny/paths-filter/issues/240
      - id: previous-sha
        run: 'echo "sha=$(git rev-parse HEAD^1)" >> $GITHUB_OUTPUT'

      - uses: dorny/paths-filter@v3
        id: filter
        with:
          base: "${{ steps.previous-sha.outputs.sha }}"
          filters: |
            matches:
              - 'backend/**'
              - 'chart/**'
              - '.github/workflows/k3d-ci.yaml'

  btrix-k3d-test:
    timeout-minutes: 45
    runs-on: ubuntu-latest
    needs: paths-filter
    if: needs.paths-filter.outputs.matches == 'true'
    steps:
      - name: Create k3d Cluster
        uses: AbsaOSS/k3d-action@v2
        with:
          k3d-version: v5.6.0
          cluster-name: btrix-1
          args: >-
            -p "30870:30870@agent:0:direct"
            --agents 1
            --no-lb
            --k3s-arg "--disable=traefik,servicelb,metrics-server@server:*"

      - name: Checkout
        uses: actions/checkout@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2
        with:
          driver-opts: network=host

      - name: Build Backend
        uses: docker/build-push-action@v3
        with:
          context: backend
          load: true
          #outputs: type=tar,dest=backend.tar
          tags: webrecorder/browsertrix-backend:latest
          cache-from: type=gha,scope=backend
          cache-to: type=gha,scope=backend,mode=max

      - name: Build Frontend
        uses: docker/build-push-action@v3
        with:
          context: frontend
          load: true
          #outputs: type=tar,dest=frontend.tar
          tags: webrecorder/browsertrix-frontend:latest
          cache-from: type=gha,scope=frontend
          cache-to: type=gha,scope=frontend,mode=max

      - name: 'Import Images'
        run: |
          k3d image import webrecorder/browsertrix-backend:latest -m direct -c btrix-1 --verbose
          k3d image import webrecorder/browsertrix-frontend:latest -m direct -c btrix-1 --verbose

      - name: Install Kubectl
        uses: azure/setup-kubectl@v3
        with:
            version: v1.28.2

      - name: Install Helm
        uses: azure/setup-helm@v3
        with:
          version: 3.10.2

      - name: Create secret
        run: kubectl create secret generic btrix-subs-app-secret --from-literal=BTRIX_SUBS_APP_URL=${{ env.ECHO_SERVER_HOST_URL }}/portalUrl

      - name: Start Cluster with Helm
        run: |
          helm upgrade --install -f ./chart/values.yaml -f ./chart/test/test.yaml btrix ./chart/

      - name: Install Python
        uses: actions/setup-python@v5
        with:
          python-version: 3.x

      - name: Install Python Libs
        run: pip install -r ./backend/test-requirements.txt

      - name: Wait for all pods to be ready
        run: kubectl wait --for=condition=ready pod --all --timeout=240s

      - name: Run Tests
        timeout-minutes: 30
        run: pytest -vv ./backend/test/test_*.py

      - name: Print Backend Logs (API)
        if: ${{ failure() }}
        run: kubectl logs svc/browsertrix-cloud-backend -c api

      - name: Print Backend Logs (Operator)
        if: ${{ failure() }}
        run: kubectl logs svc/browsertrix-cloud-backend -c op

      - name: Print K8S Events
        if: ${{ failure() }}
        run: kubectl events --all-namespaces
