# Tekton TriggerTemplate for Bakery-IA CI/CD
# This template defines how PipelineRuns are created when triggers fire

apiVersion: triggers.tekton.dev/v1beta1
kind: TriggerTemplate
metadata:
  name: bakery-ia-trigger-template
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ .Values.labels.app.name }}
    app.kubernetes.io/component: triggers
spec:
  params:
    - name: git-repo-url
      description: The git repository URL
    - name: git-revision
      description: The git revision/commit hash
    - name: git-branch
      description: The git branch name
      default: "main"
    - name: git-repo-name
      description: The git repository name
      default: "bakery-ia"
    - name: git-repo-full-name
      description: The full repository name (org/repo)
      default: "bakery-admin/bakery-ia"
    # Registry URL - keep in sync with pipeline-config ConfigMap
    - name: registry-url
      description: Container registry URL
      default: {{ .Values.global.registry.url | quote }}
  resourcetemplates:
    - apiVersion: tekton.dev/v1beta1
      kind: PipelineRun
      metadata:
        generateName: bakery-ia-ci-run-
        labels:
          app.kubernetes.io/name: {{ .Values.labels.app.name }}
          tekton.dev/pipeline: bakery-ia-ci
          triggers.tekton.dev/trigger: bakery-ia-gitea-trigger
        annotations:
          # Track the source commit
          bakery-ia.io/git-revision: $(tt.params.git-revision)
          bakery-ia.io/git-branch: $(tt.params.git-branch)
          # Automatic cleanup: delete PipelineRun after completion
          # Default: 1 hour (3600 seconds). Configure via values.yaml
          tekton.dev/pipelinerunDeletionPropagation: "foreground"
      spec:
        pipelineRef:
          name: bakery-ia-ci
        serviceAccountName: {{ .Values.serviceAccounts.pipeline.name }}
        workspaces:
          - name: shared-workspace
            volumeClaimTemplate:
              spec:
                accessModes: ["ReadWriteOnce"]
                resources:
                  requests:
                    storage: {{ .Values.pipeline.workspace.size }}
          - name: docker-credentials
            secret:
              secretName: gitea-registry-credentials
          - name: git-credentials
            secret:
              secretName: gitea-git-credentials
        params:
          - name: git-url
            value: $(tt.params.git-repo-url)
          - name: git-revision
            value: $(tt.params.git-revision)
          - name: git-branch
            value: $(tt.params.git-branch)
          # Use template parameter for registry URL
          - name: registry
            value: $(tt.params.registry-url)
          - name: skip-tests
            value: "true"
          - name: dry-run
            value: "false"
        # Timeout for the entire pipeline run
        timeouts:
          pipeline: "1h0m0s"
          tasks: "45m0s"