when:
  event:
    - push
  branch:
    - master

steps:
  - name: detect-and-validate
    image: alpine:latest
    commands:
      - apk add --no-cache bash git curl jq
      - echo "🔍 Detecting changed apps with values-int.yaml"
      - |
        CHANGED_APPS=$(git diff --name-only origin/master | grep 'manifest/.*/values-int.yaml' | cut -d'/' -f2 | sort -u)

        if [ -z "$CHANGED_APPS" ]; then
          echo "✅ No values-int.yaml changes detected. Skipping."
          exit 0
        fi

        echo "Changed apps: $CHANGED_APPS"

        for app in $CHANGED_APPS; do
          echo "🔧 Validating app: $app"
          helm lint charts/$app || exit 1
          helm template charts/$app -f manifest/$app/values-int.yaml || exit 1
        done

      - echo "✅ All changed apps passed lint and render. ArgoCD will sync them automatically." 
