The user wants to design a GitOps configuration promotion workflow.
Key elements required:
- Stages:
dev -> staging -> prod.
- Mechanisms: Promotion strategy, Gates (quality checks/approvals), Rollbacks.
Mental Model of the Solution:
- Tooling: I need to pick a standard GitOps toolset to make the explanation concrete. Argo CD and Flux are the big two. I'll focus on Argo CD as it's very visual and widely used for this pattern, but I'll mention that the concepts apply generally.
- Repository Structure: How are the manifests stored?
- Option A: Monorepo (Single repo, branches or folders for environments).
- Option B: Multirepo (App repo + Config/Manifest repo).
- Selection: The "App-of-Apps" or "ApplicationSet" pattern with Kustomize overlays is the industry standard for this. It maps well to
dev, staging, prod.
- Promotion Flow: How does code move?
- Developer commits -> CI builds image -> CI updates Git repo (Config Repo) for
dev.
- GitOps operator syncs
dev.
- Promotion to
staging: A PR is created to merge dev config to staging config (or change the image tag in the staging kustomization).
- Promotion to
prod: A PR is created for prod.
- Gates: What stops it from moving?
- Automated: Unit tests, integration tests, SAST/DAST.
- Manual: PR approvals (GitHub/GitLab branch protection), Argo CD "Sync Windows" or manual syncs.
- Rollbacks: How to fix it when it breaks?
- Git-based: Revert the commit/PR.
- Operational:
argocd app rollback (ephemeral).
Structuring the Response:
- Architecture Overview: Define the Repo structure (Kustomize is best for this) and the Tools (Argo CD/Flux).
- The Promotion Workflow (Step-by-Step):
- Dev: Automated, continuous deployment.
- Staging: PR-based or Automated after tests.
- Prod: PR-based with manual approval.
- Configuration (The "How-To"):
- Show a directory tree (Overlays).
- Show the CI pipeline logic (yq/sed or ytt).
- Gates (Quality Enforcement):
- Automated (CI checks).
- Manual (Code Owners/Approvals).
- Runtime (Health checks/Analysis - Argo Rollouts).
- Rollbacks:
- The "GitOps Way" (Revert).
- The "Emergency Way" (CLI).
Refining the "Promotion" mechanism:
There are two main ways to do promotion in GitOps:
- Branch-based: Merge
dev branch to main. (Messy with Kustomize sometimes).
- Tag/Overlay-based: Keep the config static, just change the
image tag in the specific environment's overlay. This is cleaner.
Self-Correction/Review during drafting:
- Initial thought: Just say "merge branches".
- Correction: That's bad practice for GitOps at scale. It causes merge conflicts if
staging has different replica counts than prod. Better to use Kustomize Overlays where environments patch specific values (like image tags), or use a specialized tool like Argo CD ApplicationSets with "Progressive Sync".
- Better approach: Stick to the "Image Updater" or CI-driven tag update pattern. CI updates the manifest file in the git repo for that specific environment.
Drafting the Content - Section by Section:
- Repo Structure: Use Kustomize.
bases/ (common)
overlays/dev/
overlays/staging/
overlays/prod/
- The Flow:
- Dev: Dev commits code. CI builds
img:1.0. CI commits img:1.0 to overlays/dev/kustomization.yaml. Argo syncs immediately.
- Staging: Tests pass. CI (or bot) opens PR to update
overlays/staging/kustomization.yaml to img:1.0. Auto-merge or quick approval. Argo syncs.
- Prod: Staging looks good. CI opens PR to update
overlays/prod/kustomization.yaml to img:1.0. Requires 2 reviewers.
- Gates:
- Policy: OPA/Gatekeeper or Kyverno (No privileged containers, etc.).
- Testing: Integration tests in Staging.
- Canary: Argo Rollouts (Canary strategy). This is a crucial "Gate" before full prod.
- Rollback:
- Revert PR -> Git detects revert -> Argo syncs previous state.
Final Polish: Make sure to distinguish between CI Pipeline logic and CD GitOps logic. The CI drives the "Write" to git, the CD (Argo/Flux) drives the "Read/Apply" to cluster.
Let's create a specific example using Argo CD + Kustomize + GitHub Actions (as a generic CI stand-in) as it represents the most common stack.
Key components to highlight:
- Environment Isolation: Kustomize Overlays.
- The "Update" Mechanism: CI updating the image tag in Git.
- The "Gate" Mechanism: Branch protection rules & Automated Analysis.
- Rollback:
git revert.
Let's write it out.
This design outlines a robust GitOps promotion workflow using Argo CD (as the GitOps operator) and Kustomize (for configuration management). This pattern relies on the "Environment per Directory" strategy, ensuring that the Git repository is the single source of truth.
1. Repository Strategy & Configuration Management
We will use a Monorepo approach with Kustomize Overlays. This allows us to define common resources once and patch differences (replicas, CPU limits, feature flags) per environment.
Directory Structure:
gitops-repo/
├── apps/
│ └── my-app/
│ ├── base/ # Common Kubernetes manifests
│ │ ├── deployment.yaml
│ │ ├── service.yaml
│ │ └── kustomization.yaml
│ └── overlays/
│ ├── dev/
│ │ └── kustomization.yaml # Patches: 1 replica, debug logs
│ ├── staging/
│ │ └── kustomization.yaml # Patches: 2 replicas, staging secrets
│ └── prod/
│ └── kustomization.yaml # Patches: 5 replicas, strict PDB
└── infrastructure/ # Argo CD App-of-Apps definitions
2. The Promotion Workflow (Dev → Staging → Prod)
In GitOps, "Promotion" is essentially a "Git Commit" that updates the desired state (usually the image tag) in a specific environment folder.
Stage 1: Dev (Continuous Deployment)
Goal: Rapid feedback. Every merge to the main code branch updates Dev.
- Trigger: Developer merges code to
main in the App Repo.
- CI Pipeline:
- Builds Docker image
my-app:sha-abc1234.
- Runs Unit Tests.
- Automated Commit: Updates
gitops-repo/apps/my-app/overlays/dev/kustomization.yaml with the new image tag.
- Gate (Automated):
- Argo CD detects the drift and syncs automatically (Auto-Sync).
- Health Check: Argo CD waits until the pod is "Healthy".
- Outcome: Changes are live in Dev.
Stage 2: Staging (Pre-production)
Goal: Integration testing in a production-like environment.
- Trigger: The "Promote to Staging" button is clicked in the CI/CD dashboard (or a label is added to a Pull Request).
- Action: A script copies the specific image tag from the
dev overlay folder to the staging overlay folder and opens a Pull Request (PR).
- Gate (Approval):
- Automated: CI runs
kustomize build to ensure YAML validity.
- Manual: A senior engineer reviews the PR diff (checking config changes) and clicks "Merge".
- Deployment:
- Upon merge, Argo CD syncs the
staging application.
- Gate (Automated): Integration tests (e.g., E2E Selenium/Cypress) run against the Staging URL.
Stage 3: Prod (Production)
Goal: Maximum safety and uptime.
- Trigger: Staging tests pass. A "Promote to Prod" PR is created.
- Gate (Manual Approval):
- Requires approval from a "Release Manager" group.
- Optional: "Business Hours" check (blocks deployment on Friday night).
- Deployment Strategy (Canary/Blue-Green):
- Argo Rollouts takes over the deployment.
- The Canary Gate: Argo sends 10% of traffic to the new pods.
- Analysis: Argo queries Prometheus/Datadog. If Error Rate > 0.5% or Latency P99 > 500ms, the deployment is automatically paused.
- Full Rollout: If metrics are green, Argo promotes the canary to 100%.
- Outcome: Production is live.
3. Gate Implementation Details
A. Automated Policy Gates (OPA/Kyverno)
Before Argo CD syncs, we can use an Admission Controller to block non-compliant resources.
- Scenario: Prevent deploying an image with the
:latest tag to Prod.
- Implementation: A Kyverno policy in the
prod namespace rejects any Deployment creating a pod with image: *:latest.
B. Analysis Gates (Argo Rollouts)
Instead of a simple RollingUpdate, use Argo Rollouts to define hard gates within the cluster:
# rollout.yaml (Prod Only)
spec:
strategy:
canary:
steps:
- setWeight: 10
- pause: {duration: 10m} # Soak time
- setWeight: 50
- pause: {duration: 10m}
analysis:
templates:
- templateName: success-rate
startingStep: 2 # Start checking metrics after step 2
4. Rollback Strategy
GitOps makes rollbacks intuitive because the history is stored in Git.
Scenario A: Immediate "Oh No" (Operator Rollback)
Used when a deployment is actively causing an outage and you don't have time for a Git PR.
- CLI Method:
# Tells Argo to abandon the current bad state and revert to the previous revision
argocd app rollback my-app-prod
- Result: Argo re-applies the previous Kubernetes manifests immediately.
- Warning: This creates "Drift" (Cluster state != Git state). You must fix the Git repo later.
Scenario B: The "GitOps Way" (Git Revert)
Used when you want to maintain history and audit trails.
- Action: Find the commit that introduced the bug.
git revert <bad-commit-sha>
- Process: This creates a new commit that undoes the changes.
- Gate: Depending on branch protection, this might require approval, OR you can configure Argo to allow "Auto-Sync" on
git revert commits for emergency speed.
Summary Matrix
| Feature | Dev | Staging | Prod |
|---|
| Trigger | Auto (CI Commit) | PR Merge | PR Merge + Manual Click |
| Sync Policy | Auto-Prune, Auto-Sync | Auto-Sync | Manual Sync (or Auto with Canaries) |
| Gate Type | Unit Tests | Integration Tests + Code Review | Canary Analysis + Manual Approval |
| K8s Strategy | Recreate | Rolling Update | Blue/Green or Canary (Argo Rollouts) |
| Rollback | git revert | git revert | argocd rollback -> git revert |