apiVersion: batch/v1 kind: CronJob metadata: name: tekton-cleanup namespace: tekton-pipelines spec: schedule: "0 * * * *" # Every hour successfulJobsHistoryLimit: 1 failedJobsHistoryLimit: 1 jobTemplate: spec: ttlSecondsAfterFinished: 300 template: spec: serviceAccountName: tekton-triggers-sa containers: - name: cleanup image: bitnami/kubectl:latest command: - /bin/sh - -c - | echo "Cleaning up completed PipelineRuns older than 1 hour..." kubectl get pipelineruns -n tekton-pipelines \ -o jsonpath='{range .items[?(@.status.conditions[0].status=="True")]}{.metadata.name}{" "}{.metadata.creationTimestamp}{"\n"}{end}' | \ while read name timestamp; do if [ -n "$name" ]; then age=$(( ($(date +%s) - $(date -d "$timestamp" +%s 2>/dev/null || date -j -f "%Y-%m-%dT%H:%M:%SZ" "$timestamp" +%s)) / 60 )) if [ "$age" -gt 60 ]; then echo "Deleting PipelineRun: $name (age: ${age}m)" kubectl delete pipelinerun "$name" -n tekton-pipelines fi fi done echo "Cleanup complete" restartPolicy: OnFailure