2026-01-19 14:22:07 +01:00
|
|
|
# Tekton Task to Detect Changed Services
|
|
|
|
|
# This task analyzes git changes to determine which services need to be built
|
|
|
|
|
|
|
|
|
|
apiVersion: tekton.dev/v1beta1
|
|
|
|
|
kind: Task
|
|
|
|
|
metadata:
|
|
|
|
|
name: detect-changed-services
|
2026-01-20 22:05:10 +01:00
|
|
|
namespace: {{ .Release.Namespace }}
|
2026-01-19 14:22:07 +01:00
|
|
|
labels:
|
|
|
|
|
app.kubernetes.io/name: {{ .Values.labels.app.name }}
|
|
|
|
|
app.kubernetes.io/component: detection
|
|
|
|
|
spec:
|
|
|
|
|
workspaces:
|
|
|
|
|
- name: source
|
|
|
|
|
description: Workspace containing the source code
|
|
|
|
|
results:
|
|
|
|
|
- name: changed-services
|
|
|
|
|
description: Comma-separated list of changed services
|
|
|
|
|
steps:
|
|
|
|
|
- name: detect-changes
|
2026-01-22 17:29:56 +01:00
|
|
|
image: alpine/git:2.43.0
|
|
|
|
|
securityContext:
|
|
|
|
|
runAsNonRoot: true
|
|
|
|
|
runAsUser: 65532
|
|
|
|
|
allowPrivilegeEscalation: false
|
|
|
|
|
capabilities:
|
|
|
|
|
drop:
|
|
|
|
|
- ALL
|
|
|
|
|
seccompProfile:
|
|
|
|
|
type: RuntimeDefault
|
2026-01-22 17:43:34 +01:00
|
|
|
env:
|
|
|
|
|
- name: HOME
|
|
|
|
|
value: /tekton/home
|
2026-01-19 14:22:07 +01:00
|
|
|
script: |
|
2026-01-22 17:53:02 +01:00
|
|
|
#!/bin/sh
|
2026-01-19 14:22:07 +01:00
|
|
|
set -e
|
2026-01-20 10:39:40 +01:00
|
|
|
|
2026-01-22 17:41:20 +01:00
|
|
|
# Mark workspace as safe directory to avoid ownership issues
|
|
|
|
|
git config --global --add safe.directory "$(workspaces.source.path)"
|
|
|
|
|
|
2026-01-19 14:22:07 +01:00
|
|
|
cd $(workspaces.source.path)
|
2026-01-20 10:39:40 +01:00
|
|
|
|
2026-01-22 21:41:07 +01:00
|
|
|
echo "Git log (last 3 commits):"
|
|
|
|
|
git log --oneline -3 || echo "Cannot get git log"
|
|
|
|
|
|
|
|
|
|
# Check if we have enough history for comparison
|
|
|
|
|
COMMIT_COUNT=$(git rev-list --count HEAD 2>/dev/null || echo "0")
|
|
|
|
|
echo "Commit count in history: $COMMIT_COUNT"
|
|
|
|
|
|
|
|
|
|
if [ "$COMMIT_COUNT" -lt 2 ]; then
|
|
|
|
|
echo "Not enough git history for change detection (need at least 2 commits)"
|
|
|
|
|
echo "Building all services as fallback"
|
|
|
|
|
echo "all" > $(results.changed-services.path)
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
|
2026-01-19 14:22:07 +01:00
|
|
|
# Get the list of changed files
|
2026-01-22 17:53:02 +01:00
|
|
|
CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD 2>/dev/null || echo "")
|
2026-01-20 10:39:40 +01:00
|
|
|
|
2026-01-19 14:22:07 +01:00
|
|
|
if [ -z "$CHANGED_FILES" ]; then
|
2026-01-22 21:41:07 +01:00
|
|
|
# Empty commit or something unusual - skip build
|
|
|
|
|
echo "No file changes detected in last commit"
|
|
|
|
|
echo "infrastructure" > $(results.changed-services.path)
|
2026-01-19 14:22:07 +01:00
|
|
|
exit 0
|
|
|
|
|
fi
|
2026-01-20 10:39:40 +01:00
|
|
|
|
2026-01-22 17:53:02 +01:00
|
|
|
echo "Changed files:"
|
|
|
|
|
echo "$CHANGED_FILES"
|
|
|
|
|
|
|
|
|
|
# Initialize empty string to collect changed services
|
|
|
|
|
CHANGED_SERVICES=""
|
|
|
|
|
|
|
|
|
|
# Helper function to add service if not already present
|
|
|
|
|
add_service() {
|
|
|
|
|
svc="$1"
|
|
|
|
|
case ",$CHANGED_SERVICES," in
|
|
|
|
|
*",$svc,"*) ;; # Already present
|
|
|
|
|
*)
|
|
|
|
|
if [ -z "$CHANGED_SERVICES" ]; then
|
|
|
|
|
CHANGED_SERVICES="$svc"
|
|
|
|
|
else
|
|
|
|
|
CHANGED_SERVICES="$CHANGED_SERVICES,$svc"
|
|
|
|
|
fi
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
}
|
2026-01-20 10:39:40 +01:00
|
|
|
|
|
|
|
|
# Check for changes in services/ directory
|
2026-01-22 17:53:02 +01:00
|
|
|
for svc in $(echo "$CHANGED_FILES" | grep '^services/' | cut -d'/' -f2 | sort -u); do
|
|
|
|
|
if [ -n "$svc" ]; then
|
|
|
|
|
add_service "$svc"
|
2026-01-20 10:39:40 +01:00
|
|
|
fi
|
2026-01-22 17:53:02 +01:00
|
|
|
done
|
2026-01-20 10:39:40 +01:00
|
|
|
|
|
|
|
|
# Check for changes in gateway/ directory
|
|
|
|
|
if echo "$CHANGED_FILES" | grep -q '^gateway/'; then
|
2026-01-22 17:53:02 +01:00
|
|
|
add_service "gateway"
|
2026-01-20 10:39:40 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Check for changes in frontend/ directory
|
|
|
|
|
if echo "$CHANGED_FILES" | grep -q '^frontend/'; then
|
2026-01-22 17:53:02 +01:00
|
|
|
add_service "frontend"
|
2026-01-20 10:39:40 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Check for changes in shared/ directory (might affect multiple services)
|
|
|
|
|
if echo "$CHANGED_FILES" | grep -q '^shared/'; then
|
2026-01-22 17:53:02 +01:00
|
|
|
add_service "shared"
|
2026-01-20 10:39:40 +01:00
|
|
|
fi
|
|
|
|
|
|
2026-01-19 14:22:07 +01:00
|
|
|
if [ -z "$CHANGED_SERVICES" ]; then
|
|
|
|
|
# Changes are in infrastructure or other non-service files
|
2026-01-22 17:53:02 +01:00
|
|
|
echo "Detected: infrastructure changes only"
|
2026-01-19 14:22:07 +01:00
|
|
|
echo "infrastructure" > $(results.changed-services.path)
|
|
|
|
|
else
|
2026-01-22 17:53:02 +01:00
|
|
|
echo "Detected changed services: $CHANGED_SERVICES"
|
2026-01-19 14:22:07 +01:00
|
|
|
echo "$CHANGED_SERVICES" > $(results.changed-services.path)
|
|
|
|
|
fi
|