--- name: implement-gitops-workflow description: > Implement GitOps continuous delivery using Argo CD or Flux with app-of-apps pattern, automated sync policies, drift detection, and multi-environment promotion. Manage Kubernetes deployments declaratively from Git with automated reconciliation. Use when implementing declarative infrastructure management, migrating from imperative kubectl commands to Git-driven deployments, setting up multi-environment promotion workflows, enforcing code review gates for production, or meeting audit and compliance requirements. license: MIT allowed-tools: Read Write Edit Bash Grep Glob metadata: author: Philipp Thoss version: "1.0" domain: devops complexity: advanced language: multi tags: gitops, argocd, flux, sync, drift-detection --- # Implement GitOps Workflow Deploy and manage Kubernetes applications using GitOps principles with Argo CD or Flux for automated, auditable, and repeatable deployments. ## When to Use - Implementing declarative infrastructure and application management - Migrating from imperative kubectl/helm commands to Git-driven deployments - Setting up multi-environment promotion workflows (dev → staging → prod) - Enforcing code review and approval gates for production deployments - Achieving compliance and audit requirements with Git history - Implementing disaster recovery with Git as single source of truth ## Inputs - **Required**: Kubernetes cluster with admin access (EKS, GKE, AKS, or self-hosted) - **Required**: Git repository for Kubernetes manifests and Helm charts - **Required**: Argo CD or Flux CLI installed - **Optional**: Sealed Secrets or External Secrets Operator for secrets management - **Optional**: Image Updater for automated image promotion - **Optional**: Prometheus for monitoring sync status ## Procedure > See [Extended Examples](references/EXAMPLES.md) for complete configuration files and templates. ### Step 1: Install Argo CD and Configure Repository Access Deploy Argo CD to cluster and connect to Git repository. ```bash # Create namespace kubectl create namespace argocd # Install Argo CD kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml # Wait for pods to be ready kubectl wait --for=condition=ready pod -l app.kubernetes.io/name=argocd-server -n argocd --timeout=300s # Install Argo CD CLI curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64 sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd rm argocd-linux-amd64 # Port-forward to access UI kubectl port-forward svc/argocd-server -n argocd 8080:443 & # Get initial admin password ARGOCD_PASSWORD=$(kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d) echo "Argo CD Admin Password: $ARGOCD_PASSWORD" # Login via CLI argocd login localhost:8080 --username admin --password "$ARGOCD_PASSWORD" --insecure # Change admin password argocd account update-password # Add Git repository (HTTPS with token) argocd repo add https://github.com/USERNAME/gitops-repo \ --username USERNAME \ --password "$GITHUB_TOKEN" \ --name gitops-repo # Or add via SSH ssh-keygen -t ed25519 -C "argocd@cluster" -f argocd-deploy-key -N "" # Add argocd-deploy-key.pub to GitHub repository deploy keys argocd repo add git@github.com:USERNAME/gitops-repo.git \ --ssh-private-key-path argocd-deploy-key \ --name gitops-repo # Verify repository connection argocd repo list # Configure Ingress for UI (optional) cat < apps/myapp/base/kustomization.yaml < apps/myapp/base/deployment.yaml < apps/myapp/base/service.yaml < apps/myapp/overlays/prod/kustomization.yaml < argocd-apps/myapp-prod.yaml < -o yaml`. ### Step 3: Implement App-of-Apps Pattern for Multi-Environment Management Create root application that manages child applications across environments. ```bash # Create app-of-apps structure mkdir -p argocd-apps/{projects,infra,apps} # Define projects for RBAC cat > argocd-apps/projects/production.yaml < argocd-apps/myapp-prod-autoupdate.yaml < argocd-apps/myapp-strict.yaml <