# EN: CronJob to sync GitHub → Gitea every 5 minutes
# VI: CronJob dong bo GitHub → Gitea moi 5 phut
#
# Flow: GitHub (hongochai10/Microservices-Development)
#       → git clone --bare via SSH
#       → git push --mirror to Gitea (admin/pos-system)
#
# Prerequisites:
#   1. Create Gitea repo: admin/pos-system on gitea.techbi.org
#   2. Create secrets:
#      kubectl create secret generic pos-gitea-push-creds -n gitea \
#        --from-literal=url=https://admin:Velik%402026@gitea.techbi.org/admin/pos-system.git
#      (git-ssh-key secret already exists in gitea namespace from neon-ui setup)
---
apiVersion: batch/v1
kind: CronJob
metadata:
  name: github-gitea-sync-pos
  namespace: gitea
  labels:
    app: pos-system
    platform: goodgo
spec:
  schedule: "*/5 * * * *"
  concurrencyPolicy: Forbid
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 2
  jobTemplate:
    spec:
      backoffLimit: 1
      ttlSecondsAfterFinished: 120
      template:
        spec:
          containers:
            - name: sync
              image: alpine/git:latest
              command: ["sh", "-c"]
              args:
                - |
                  mkdir -p /root/.ssh
                  cp /ssh/ssh-privatekey /root/.ssh/id_ed25519
                  cp /ssh/known_hosts /root/.ssh/known_hosts
                  chmod 600 /root/.ssh/id_ed25519
                  git clone --bare git@github.com:hongochai10/Microservices-Development.git /tmp/repo
                  cd /tmp/repo
                  git push --mirror "${GITEA_PUSH_URL}"
              env:
                - name: GITEA_PUSH_URL
                  valueFrom:
                    secretKeyRef:
                      name: pos-gitea-push-creds
                      key: url
              volumeMounts:
                - name: git-ssh
                  mountPath: /ssh
                  readOnly: true
              resources:
                requests:
                  cpu: 50m
                  memory: 64Mi
                limits:
                  cpu: 200m
                  memory: 256Mi
          restartPolicy: Never
          volumes:
            - name: git-ssh
              secret:
                secretName: git-ssh-key
                defaultMode: 0600
