apiVersion: v1
kind: Pod
metadata:
  name: robot-runner-with-profile
  labels:
    app: robot-tests
spec:
  initContainers:
    - name: git-sync
      image: alpine/git
      command: ["/bin/sh", "-c"]
      args:
        - |
          set -e
          if [ -z "${GIT_REPO}" ]; then
            echo "GIT_REPO not set; skipping clone"
            exit 0
          fi
          mkdir -p /workspace/src
          if [ -d /workspace/src/.git ]; then
            cd /workspace/src
            git fetch --all --prune
            git reset --hard origin/${GIT_REF:-main}
          else
            git clone --branch ${GIT_REF:-main} ${GIT_REPO} /workspace/src
          fi
      env:
        - name: GIT_REPO
          value: "https://github.com/yourorg/yourrepo.git"
        - name: GIT_REF
          value: "main"
      volumeMounts:
        - name: workspace
          mountPath: /workspace

  containers:
    - name: robot-runner
      image: python:3.11-slim
      command: ["/bin/sh", "-c"]
      args:
        - |
          set -euo pipefail
          # use workspace/src if populated by initContainer, otherwise use /workspace
          WORKDIR=/workspace/src
          if [ ! -d "$WORKDIR" ]; then WORKDIR=/workspace; fi
          cd "$WORKDIR"
          python -m venv venv
          . venv/bin/activate
          pip install --upgrade pip
          pip install robotframework selenium robotframework-seleniumlibrary webdriver-manager || true
          mkdir -p results
          # export chrome profile dir if mounted
          if [ -d /workspace/results/chrome-profile ]; then
            export CHROME_USER_DATA_DIR=/workspace/results/chrome-profile
          fi
          robot -d results tests || RC=$?
          exit ${RC-0}
      volumeMounts:
        - name: chrome-profile
          mountPath: /workspace/results/chrome-profile
        - name: workspace
          mountPath: /workspace
  restartPolicy: Never
  volumes:
    - name: chrome-profile
      persistentVolumeClaim:
        claimName: chrome-profile-pvc
    - name: workspace
      emptyDir: {}
