From e71e7f4a699bc48e540234874062569b509f2c43 Mon Sep 17 00:00:00 2001 From: Nadav Erell Date: Thu, 19 Mar 2026 23:00:21 +0200 Subject: [PATCH] Detect GitOps mode from ArgoCD Application YAML file Check for deploy/argocd/{env}.yaml existence as the primary signal for gitops mode, instead of grepping kustomize build output for the managed-by label. Falls back to label check for backwards compatibility. Includes diagnostic logging of the resolved path, explicit notice when falling back to label detection, and proper error handling for kustomize build failures in the fallback path. --- action.yml | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/action.yml b/action.yml index 130ce77..e9df171 100644 --- a/action.yml +++ b/action.yml @@ -121,21 +121,43 @@ runs: working-directory: ${{ inputs.working_directory }}/${{ inputs.overlay_dir }} run: | echo "Detecting deployment mode..." - - BUILD_RESULT=$(kustomize build) - MANAGED_BY=$(echo "$BUILD_RESULT" | grep "app.kubernetes.io/managed-by:" | head -n1 | sed 's/.*app.kubernetes.io\/managed-by:\s*//' | tr -d '[:space:]' || true) - - echo "managed_by=$MANAGED_BY" >> $GITHUB_OUTPUT - - if [ "$MANAGED_BY" = "argocd" ]; then - echo "mode=gitops" >> $GITHUB_OUTPUT - echo "✓ GitOps mode detected (ArgoCD)" - elif [ "$MANAGED_BY" = "flux" ]; then + + # Primary detection: check for ArgoCD Application YAML file (single source of truth) + # Overlay dir is e.g. deploy/overlays/nurture-prod, ArgoCD dir is deploy/argocd/ + ARGOCD_DIR="../../argocd" + ARGOCD_FILE="${ARGOCD_DIR}/${{ inputs.environment }}.yaml" + ARGOCD_FILE_YML="${ARGOCD_DIR}/${{ inputs.environment }}.yml" + + echo "Checking for ArgoCD Application YAML at: $(cd "$ARGOCD_DIR" 2>/dev/null && pwd || echo "$ARGOCD_DIR [not found]")/${{ inputs.environment }}.yaml" + + if [ -f "$ARGOCD_FILE" ] || [ -f "$ARGOCD_FILE_YML" ]; then + echo "managed_by=argocd" >> $GITHUB_OUTPUT echo "mode=gitops" >> $GITHUB_OUTPUT - echo "✓ GitOps mode detected (Flux)" + echo "✓ GitOps mode detected (ArgoCD Application YAML found)" else - echo "mode=kubectl" >> $GITHUB_OUTPUT - echo "✓ Direct deployment mode (kubectl)" + echo "::notice::ArgoCD Application YAML not found. Falling back to kustomize build label detection." + # Fallback: check managed-by label in kustomize build output for backwards compatibility + if ! BUILD_RESULT=$(kustomize build 2>&1); then + echo "::warning::kustomize build failed during GitOps detection. Defaulting to kubectl mode." + echo "::warning::Build error: $BUILD_RESULT" + echo "managed_by=" >> $GITHUB_OUTPUT + echo "mode=kubectl" >> $GITHUB_OUTPUT + else + MANAGED_BY=$(echo "$BUILD_RESULT" | { grep "app.kubernetes.io/managed-by:" || true; } | head -n1 | sed 's/.*app.kubernetes.io\/managed-by:\s*//' | tr -d '[:space:]') + + echo "managed_by=$MANAGED_BY" >> $GITHUB_OUTPUT + + if [ "$MANAGED_BY" = "argocd" ]; then + echo "mode=gitops" >> $GITHUB_OUTPUT + echo "✓ GitOps mode detected (ArgoCD, via managed-by label fallback)" + elif [ "$MANAGED_BY" = "flux" ]; then + echo "mode=gitops" >> $GITHUB_OUTPUT + echo "✓ GitOps mode detected (Flux)" + else + echo "mode=kubectl" >> $GITHUB_OUTPUT + echo "✓ Direct deployment mode (kubectl)" + fi + fi fi - name: Set deployment mode