Skip to content

Set up GitOps with Kustomize, ArgoCD and GitLab CI

Stack: GitLab CI · Kustomize · ArgoCD · Kubernetes
Pattern: Polyrepo GitOps — 1 infra repo + 3 service repos
Last updated: April 2026


1. Architecture Overview

3 service repos ──→ GitLab CI ──→ infra-platform repo ──→ ArgoCD ──→ K8s cluster
  (app code)        (build+push)   (Kustomize overlays)    (sync)    (dev/stg/prod)

The 4 repos

Repo Purpose Who writes to it
your-org/service-a REST API — src, Dockerfile, .gitlab-ci.yml Engineers
your-org/service-b gRPC + Worker — src, Dockerfile, .gitlab-ci.yml Engineers
your-org/service-c GraphQL — src, Dockerfile, .gitlab-ci.yml Engineers
your-org/infra-platform Kustomize base+overlays, ArgoCD CRDs — no app code CI (auto) + Engineers (MR)

The 3 environments

Env Namespace Git branch Sync policy How to promote
dev dev dev Auto-sync on push to dev Push feature branch → merge to dev
staging staging staging Auto-sync on push to staging Merge dev → staging
prod prod main Auto-sync on push to main Merge staging → main

Branch strategy — long-lived environment branches

feature/my-feature
       ↓  merge (MR)
      dev  ──────────────────→  deploys to dev automatically
       ↓  merge (MR) when dev is stable
    staging  ────────────────→  deploys to staging automatically
       ↓  merge (MR) when staging is approved
      main  ──────────────────→  deploys to prod automatically

Branches are never deleted. dev, staging, and main are permanent.
Merging does not delete — it only brings changes forward into the next environment.
Each branch always reflects exactly what is running in its environment.


2. Repo Structure

Service repos

Each service repo contains only application code and its own CI pipeline:

service-a/
├── src/
├── Dockerfile
└── .gitlab-ci.yml

infra-platform repo

infra-platform/
│
├── services/
│   ├── service-a/
│   │   ├── base/                      # plain K8s YAML — no templating
│   │   │   ├── deployment.yaml
│   │   │   ├── service.yaml
│   │   │   └── kustomization.yaml
│   │   └── overlays/
│   │       ├── dev/
│   │       │   ├── kustomization.yaml
│   │       │   ├── patch-image.yaml   # CI patches this on every push
│   │       │   └── patch-env.yaml
│   │       ├── staging/
│   │       │   ├── kustomization.yaml
│   │       │   ├── patch-image.yaml
│   │       │   ├── patch-env.yaml
│   │       │   └── patch-replicas.yaml
│   │       └── prod/
│   │           ├── kustomization.yaml
│   │           ├── patch-image.yaml
│   │           ├── patch-env.yaml
│   │           ├── patch-replicas.yaml
│   │           └── patch-resources.yaml
│   │
│   ├── service-b/                     # own base/ — different K8s resources
│   │   ├── base/
│   │   └── overlays/dev|staging|prod/
│   │
│   └── service-c/                     # own base/ — different K8s resources
│       ├── base/
│       └── overlays/dev|staging|prod/
│
└── argocd/
    ├── root-app.yaml                  # App of Apps — apply ONCE manually
    ├── bootstrap/                     # one-time setup YAMLs — apply once, git owns them after
    │   ├── repo-secret.yaml           # ArgoCD repo credentials (git read access)
    │   └── registry-secrets/
    │       ├── registry-secret-dev.yaml
    │       ├── registry-secret-staging.yaml
    │       └── registry-secret-prod.yaml
    └── apps/                          # 9 Application CRDs (3 services × 3 envs)
        ├── app-service-a-dev.yaml
        ├── app-service-a-staging.yaml
        ├── app-service-a-prod.yaml
        ├── app-service-b-dev.yaml
        ├── app-service-b-staging.yaml
        ├── app-service-b-prod.yaml
        ├── app-service-c-dev.yaml
        ├── app-service-c-staging.yaml
        └── app-service-c-prod.yaml

3. Why Kustomize over Helm

All 3 services are architecturally completely different — different K8s resource types, different networking, different storage. A shared Helm chart would be mostly if/else conditionals.

Concern Helm Kustomize
Services share structure One chart Separate base per service
Services are architecturally different Nested conditionals Each owns its base
What you see is what gets applied Go template syntax Plain YAML
Code review / git diff readability Template changes Plain YAML diffs
ArgoCD integration helm.valueFiles block Just a path:

Rule: Services share structure → Helm. Services are architecturally different → Kustomize.


4. Step 1 — Bootstrap Cluster Tools

Do once. Helm is only used here — to install ArgoCD which ships as a Helm chart. Your own services never use Helm.

Install local tools

brew install kubectl helm argocd

Install ArgoCD into the cluster

kubectl create namespace argocd
kubectl apply -n argocd \
  -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

# Wait for all pods to be Running
kubectl get pods -n argocd -w

# Get the initial admin password
argocd admin initial-password -n argocd

# Port-forward and log in
kubectl port-forward svc/argocd-server -n argocd 8080:443 &
argocd login localhost:8080 --username admin --insecure

# Change the default password immediately
argocd account update-password

Apply bootstrap secrets (from argocd/bootstrap/)

These are the only secret-related kubectl apply commands you ever run. See Step 6 for how to create the tokens and the full YAML content of each file.

# Create namespaces first
kubectl create namespace dev
kubectl create namespace staging
kubectl create namespace prod

# Apply ArgoCD repo secret (gives ArgoCD read access to infra-platform)
kubectl apply -f argocd/bootstrap/repo-secret.yaml

# Apply image pull secrets (gives K8s permission to pull from GitLab registry)
kubectl apply -f argocd/bootstrap/registry-secrets/registry-secret-dev.yaml
kubectl apply -f argocd/bootstrap/registry-secrets/registry-secret-staging.yaml
kubectl apply -f argocd/bootstrap/registry-secrets/registry-secret-prod.yaml

# Patch default ServiceAccount in each namespace
# so imagePullSecrets applies to every pod automatically
for ns in dev staging prod; do
  kubectl patch serviceaccount default \
    -p '{"imagePullSecrets": [{"name": "gitlab-registry-secret"}]}' \
    -n $ns
done

5. Step 2 — Write Base K8s Manifests

Base files are plain valid Kubernetes YAML — no templating, no conditionals.
Use placeholder for anything that changes per env — overlays will patch it.
Each service has its own base/ with exactly the resources it needs.

The example below shows service-a. service-b and service-c follow the exact same pattern but with their own resource types (StatefulSet, PVC, etc.).

services/service-a/base/deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-a
spec:
  replicas: 1
  selector:
    matchLabels:
      app: service-a
  template:
    metadata:
      labels:
        app: service-a
    spec:
      imagePullSecrets:
        - name: gitlab-registry-secret   # allows K8s to pull from GitLab registry
      containers:
        - name: api
          image: registry.gitlab.com/your-org/service-a:placeholder
          # ↑ overlays/dev/patch-image.yaml replaces "placeholder" with real SHA
          ports:
            - containerPort: 3000
          env:
            - name: DB_HOST
              value: placeholder         # overlays/dev/patch-env.yaml patches this
            - name: APP_ENV
              value: placeholder
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
          livenessProbe:
            httpGet:
              path: /healthz
              port: 3000
            initialDelaySeconds: 10
          readinessProbe:
            httpGet:
              path: /ready
              port: 3000
            initialDelaySeconds: 5

services/service-a/base/service.yaml

apiVersion: v1
kind: Service
metadata:
  name: service-a
spec:
  selector:
    app: service-a
  ports:
    - port: 80
      targetPort: 3000

services/service-a/base/kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - deployment.yaml
  - service.yaml

6. Step 3 — Write Overlays

Overlays are tiny — only patch what differs per environment.
The base handles everything else automatically.

services/service-a/overlays/dev/kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../../base          # inherit everything from base
patches:
  - path: patch-image.yaml
  - path: patch-env.yaml
namespace: dev

services/service-a/overlays/dev/patch-image.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-a
spec:
  template:
    spec:
      containers:
        - name: api
          image: registry.gitlab.com/your-org/service-a:placeholder
          # CI replaces "placeholder" with the real commit SHA on every push

services/service-a/overlays/dev/patch-env.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-a
spec:
  template:
    spec:
      containers:
        - name: api
          env:
            - name: DB_HOST
              value: postgres.dev.internal
            - name: APP_ENV
              value: development
            - name: LOG_LEVEL
              value: debug

services/service-a/overlays/staging/patch-replicas.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-a
spec:
  replicas: 2

services/service-a/overlays/prod/patch-resources.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service-a
spec:
  template:
    spec:
      containers:
        - name: api
          resources:
            requests:
              cpu: 500m
              memory: 512Mi
            limits:
              cpu: 2000m
              memory: 1Gi

Verify overlays locally (free — no cluster needed)

# See exactly what gets applied
kubectl kustomize services/service-a/overlays/dev

# Check which resource kinds are produced
kubectl kustomize services/service-a/overlays/dev | grep "^kind:"
# kind: Deployment
# kind: Service

# Always run this before first install and after any change

7. Step 4 — First Manual Install

Done once only to prove base + overlays work before ArgoCD takes over.
You will never run kubectl apply -k again after this.

Build and push images manually

# In each service repo
docker build -t registry.gitlab.com/your-org/service-a:v0.1.0 .
docker push registry.gitlab.com/your-org/service-a:v0.1.0

# Repeat for service-b and service-c

Update image tags in overlays for first install

cd infra-platform

sed -i "s|:placeholder|:v0.1.0|" services/service-a/overlays/dev/patch-image.yaml
sed -i "s|:placeholder|:v0.1.0|" services/service-b/overlays/dev/patch-image.yaml
sed -i "s|:placeholder|:v0.1.0|" services/service-c/overlays/dev/patch-image.yaml

git add . && git commit -m "chore: set initial image tags to v0.1.0" && git push

Create namespaces and apply

kubectl create namespace dev
kubectl create namespace staging
kubectl create namespace prod

# Apply each service — Kustomize reads overlay + base automatically
kubectl apply -k services/service-a/overlays/dev
kubectl apply -k services/service-b/overlays/dev
kubectl apply -k services/service-c/overlays/dev

# Verify
kubectl get pods -n dev
kubectl get all -n dev

Troubleshooting

# Pod not starting
kubectl logs -n dev deployment/service-a
kubectl describe pod -n dev -l app=service-a

# ImagePullBackOff  → registry credentials missing — see Step 6 image pull secret
# CrashLoopBackOff  → app error — check logs
# Pending           → not enough cluster resources

8. Step 5 — GitLab CI Pipeline

Setup: create a deploy token for infra-platform

CI needs write access to infra-platform to patch image tags. Do this once — all 3 service repos share the same token.

infra-platform repo:
  Settings → Repository → Deploy tokens → New token
  Name: ci-write
  Scope: write_repository ← tick this only
  → Copy the token value

Each service repo (service-a, b, c):
  Settings → CI/CD → Variables → Add variable
  Key:   INFRA_DEPLOY_TOKEN
  Value: (paste token)
  Flags: Masked ← always tick this

Branch strategy

Each environment maps to a permanent long-lived branch:

Branch Deploys to Triggered by
dev dev namespace Push or merge to dev
staging staging namespace Merge dev → staging
main prod namespace Merge staging → main

Branches are never deleted. Merging only brings changes forward — it does not remove the source branch.

Promotion flow

feature/my-feature  →  dev  →  staging  →  main
                       ↓           ↓          ↓
                   deploys     deploys    deploys
                   to dev    to staging   to prod

.gitlab-ci.yml — same for all 3 service repos

Only the SERVICE variable changes between service-a, service-b, and service-c.
Each job triggers only when its corresponding branch receives a push.

stages:
  - build
  - update-infra

variables:
  SERVICE: service-a            # ← only this line changes per repo
  IMAGE:   $CI_REGISTRY_IMAGE   # auto = registry.gitlab.com/your-org/service-a
  INFRA:   https://ci-write:$INFRA_DEPLOY_TOKEN@gitlab.com/your-org/infra-platform.git

# ── Stage 1: build image — runs on ALL 3 branches ─────────────────
# The same image is built from whatever commit is on that branch.
# Image is tagged with the commit SHA so each env gets the exact
# SHA that was tested on that branch.
build:
  stage: build
  image: docker:24
  services:
    - docker:24-dind
  variables:
    DOCKER_TLS_CERTDIR: "/certs"
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build -t $IMAGE:$CI_COMMIT_SHORT_SHA .
    - docker push  $IMAGE:$CI_COMMIT_SHORT_SHA
  rules:
    - if: $CI_COMMIT_BRANCH == "dev"
    - if: $CI_COMMIT_BRANCH == "staging"
    - if: $CI_COMMIT_BRANCH == "main"

# ── Stage 2a: push to dev → update dev overlay ────────────────────
update-dev:
  stage: update-infra
  image: alpine/git
  needs: [build]
  script:
    - git clone $INFRA infra && cd infra
    - sed -i "s|image:.*$SERVICE.*|image: $IMAGE:$CI_COMMIT_SHORT_SHA|"
        services/$SERVICE/overlays/dev/patch-image.yaml
    - git config user.email "ci@gitlab.com"
    - git config user.name  "GitLab CI"
    - git add services/$SERVICE/overlays/dev/patch-image.yaml
    - git diff --cached --quiet ||
        git commit -m "chore($SERVICE): dev → $CI_COMMIT_SHORT_SHA [skip ci]"
    - git push
  rules:
    - if: $CI_COMMIT_BRANCH == "dev"    # only triggers on the dev branch

# ── Stage 2b: push to staging → update staging overlay ────────────
update-staging:
  stage: update-infra
  image: alpine/git
  needs: [build]
  script:
    - git clone $INFRA infra && cd infra
    - sed -i "s|image:.*$SERVICE.*|image: $IMAGE:$CI_COMMIT_SHORT_SHA|"
        services/$SERVICE/overlays/staging/patch-image.yaml
    - git config user.email "ci@gitlab.com"
    - git config user.name  "GitLab CI"
    - git add services/$SERVICE/overlays/staging/patch-image.yaml
    - git diff --cached --quiet ||
        git commit -m "chore($SERVICE): staging → $CI_COMMIT_SHORT_SHA [skip ci]"
    - git push
  rules:
    - if: $CI_COMMIT_BRANCH == "staging" # only triggers on the staging branch

# ── Stage 2c: push to main → update prod overlay ──────────────────
update-prod:
  stage: update-infra
  image: alpine/git
  needs: [build]
  script:
    - git clone $INFRA infra && cd infra
    - sed -i "s|image:.*$SERVICE.*|image: $IMAGE:$CI_COMMIT_SHORT_SHA|"
        services/$SERVICE/overlays/prod/patch-image.yaml
    - git config user.email "ci@gitlab.com"
    - git config user.name  "GitLab CI"
    - git add services/$SERVICE/overlays/prod/patch-image.yaml
    - git diff --cached --quiet ||
        git commit -m "chore($SERVICE): prod → $CI_COMMIT_SHORT_SHA [skip ci]"
    - git push
  rules:
    - if: $CI_COMMIT_BRANCH == "main"   # only triggers on the main branch

[skip ci] on infra-platform commits prevents an infinite CI loop.
git diff --cached --quiet || prevents an empty commit error if the tag didn't change.

How promotion works step by step

1. Developer creates feature branch from dev:
   git checkout dev && git checkout -b feature/my-feature

2. Work is done, push to feature branch:
   git push origin feature/my-feature

3. Open MR: feature/my-feature → dev
   → merge (delete source branch)
   → CI triggers on dev branch
   → image built with new SHA
   → dev overlay patched
   → ArgoCD syncs to dev namespace

4. Dev is stable → open MR: dev → staging
   → merge (do NOT delete dev branch)
   → CI triggers on staging branch
   → image built with that SHA
   → staging overlay patched
   → ArgoCD syncs to staging namespace

5. Staging approved → open MR: staging → main
   → merge (do NOT delete staging branch)
   → CI triggers on main branch
   → image built with that SHA
   → prod overlay patched
   → ArgoCD syncs to prod namespace

GitLab MR settings — prevent branch deletion

In each service repo, set the default MR behaviour to not delete source branches:

Settings → Merge requests
→ Uncheck "Enable 'Delete source branch' option by default"

This ensures dev and staging branches are never accidentally deleted on merge.


9. Step 6 — ArgoCD Setup & Credentials

This step involves two completely separate credentials that are often confused:

The difference — deploy token vs image pull secret

ArgoCD repo deploy token Image pull secret
Purpose ArgoCD reads infra-platform git repo to know what to deploy Kubernetes pulls your Docker image from the registry when starting a pod
Authenticates against GitLab git (SSH/HTTPS) GitLab Container Registry
Used by ArgoCD Kubernetes (kubelet)
Scope needed read_repository read_registry
Stored in argocd/bootstrap/repo-secret.yaml argocd/bootstrap/registry-secrets/registry-secret-*.yaml

They solve completely different problems. You need both.


Credential A — ArgoCD repo deploy token

1. Create the token in GitLab

infra-platform repo:
  Settings → Repository → Deploy tokens → New token
  Name:   argocd-read
  Scopes: read_repository ← tick this only
  → Click Create deploy token
  → Copy the Token value (shown only once)
  → Note the Username (usually looks like: gitlab+deploy-token-XXXXX)

2. Paste values into the YAML file

ArgoCD recognises this as a repo credential because of the label argocd.argoproj.io/secret-type: repository. No argocd repo add CLI command needed.

Never commit the real token value. Keep YOUR_READ_TOKEN as a placeholder in git. Substitute the real value only at apply time.

argocd/bootstrap/repo-secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: infra-platform-repo
  namespace: argocd
  labels:
    argocd.argoproj.io/secret-type: repository  # tells ArgoCD this is a repo credential
type: Opaque
stringData:
  type: git
  url: https://gitlab.com/your-org/infra-platform.git
  username: YOUR_DEPLOY_TOKEN_USERNAME   # e.g. gitlab+deploy-token-123456
  password: YOUR_READ_TOKEN              # the token value — never commit the real value

3. Apply once

kubectl apply -f argocd/bootstrap/repo-secret.yaml

# Verify ArgoCD picked it up
argocd repo list
# Should show: https://gitlab.com/your-org/infra-platform.git  STATUS: Successful

Credential B — Image pull secret

1. Create the token in GitLab

infra-platform repo (or any repo in your-org group):
  Settings → Repository → Deploy tokens → New token
  Name:   k8s-registry-pull
  Scopes: read_registry ← tick this only
  → Click Create deploy token
  → Copy the Token value (shown only once)
  → Note the Username (usually looks like: gitlab+deploy-token-XXXXX)

2. Generate the base64 docker config value

# Run this command — it generates the base64 value without creating anything in K8s
kubectl create secret docker-registry gitlab-registry-secret \
  --docker-server=registry.gitlab.com \
  --docker-username=YOUR_DEPLOY_TOKEN_USERNAME \
  --docker-password=YOUR_TOKEN \
  --dry-run=client -o jsonpath='{.data.\.dockerconfigjson}'

# Copy the long base64 string from the output
# Paste it into the .dockerconfigjson field in each file below

3. Paste the value into the YAML files (one per namespace)

Never commit the real base64 value — it is just a base64-encoded version of your token, not encrypted. Keep REPLACE_WITH_BASE64_DOCKER_CONFIG as a placeholder in git. Substitute only at apply time.

argocd/bootstrap/registry-secrets/registry-secret-dev.yaml

apiVersion: v1
kind: Secret
metadata:
  name: gitlab-registry-secret
  namespace: dev
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: REPLACE_WITH_BASE64_DOCKER_CONFIG

argocd/bootstrap/registry-secrets/registry-secret-staging.yaml

apiVersion: v1
kind: Secret
metadata:
  name: gitlab-registry-secret
  namespace: staging
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: REPLACE_WITH_BASE64_DOCKER_CONFIG

argocd/bootstrap/registry-secrets/registry-secret-prod.yaml

apiVersion: v1
kind: Secret
metadata:
  name: gitlab-registry-secret
  namespace: prod
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: REPLACE_WITH_BASE64_DOCKER_CONFIG

4. Apply all 3 secret files

kubectl apply -f argocd/bootstrap/registry-secrets/registry-secret-dev.yaml
kubectl apply -f argocd/bootstrap/registry-secrets/registry-secret-staging.yaml
kubectl apply -f argocd/bootstrap/registry-secrets/registry-secret-prod.yaml

# Verify secrets exist in each namespace
kubectl get secret gitlab-registry-secret -n dev
kubectl get secret gitlab-registry-secret -n staging
kubectl get secret gitlab-registry-secret -n prod

Instead of adding imagePullSecrets to every deployment.yaml, patch the default ServiceAccount in each namespace. Kubernetes then applies the pull secret automatically to every pod.

for ns in dev staging prod; do
  kubectl patch serviceaccount default \
    -p '{"imagePullSecrets": [{"name": "gitlab-registry-secret"}]}' \
    -n $ns
done

With this approach you can remove imagePullSecrets from deployment.yaml entirely — cleaner base manifests.


10. Step 7 — ArgoCD Application CRDs

App of Apps concept

Once root-app.yaml is applied, ArgoCD manages two folders automatically — argocd/bootstrap/ and argocd/apps/. You never need to manually apply anything from those folders again.

kubectl apply -f argocd/root-app.yaml   ← the only manual command
         ↓
root-app watches argocd/  (both bootstrap/ and apps/)
         ↓
├── bootstrap/                          ← ArgoCD keeps secrets in sync
│   ├── repo-secret.yaml                   re-applies if deleted or changed
│   └── registry-secrets/*.yaml
│
└── apps/                               ← ArgoCD creates and manages Applications
    ├── app-service-a-dev               → syncs services/service-a/overlays/dev → dev namespace
    ├── app-service-b-dev               → syncs services/service-b/overlays/dev → dev namespace
    ├── app-service-c-dev               → syncs services/service-c/overlays/dev → dev namespace
    └── ... (9 Application CRDs total)

Application CRD — dev (auto-sync)

argocd/apps/app-service-a-dev.yaml

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: service-a-dev
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://gitlab.com/your-org/infra-platform.git
    targetRevision: main
    path: services/service-a/overlays/dev   # ArgoCD detects Kustomize automatically
  destination:
    server: https://kubernetes.default.svc
    namespace: dev
  syncPolicy:
    automated:
      prune: true       # remove K8s resources deleted from manifests
      selfHeal: true    # revert any manual kubectl changes
    syncOptions:
      - CreateNamespace=true

For app-service-b-dev.yaml and app-service-c-dev.yaml — change name and path. Everything else is identical.

Application CRD — prod (auto-sync via main branch)

argocd/apps/app-service-a-prod.yaml

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: service-a-prod
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://gitlab.com/your-org/infra-platform.git
    targetRevision: main
    path: services/service-a/overlays/prod
  destination:
    server: https://kubernetes.default.svc
    namespace: prod
  syncPolicy:
    automated:
      prune: true
      selfHeal: true
    syncOptions:
      - CreateNamespace=true

Root App of Apps — watches argocd/ folder (bootstrap + apps)

argocd/root-app.yaml

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: root-app
  namespace: argocd
spec:
  project: default
  source:
    repoURL: https://gitlab.com/your-org/infra-platform.git
    targetRevision: main
    path: argocd              # watches the entire argocd/ folder
                              # picks up both bootstrap/ and apps/ automatically
  destination:
    server: https://kubernetes.default.svc
    namespace: argocd
  syncPolicy:
    automated:
      prune: true
      selfHeal: true

Why path: argocd instead of path: argocd/apps?
Pointing at the parent argocd/ folder means root-app also manages the bootstrap secrets. If someone accidentally deletes a registry secret or the repo credential, ArgoCD will recreate it automatically on the next sync cycle — no manual intervention needed.

Apply and verify

# Commit all ArgoCD files to infra-platform (bootstrap/ + apps/ + root-app.yaml)
git add argocd/ && git commit -m "feat: add argocd bootstrap secrets and application crds" && git push

# Apply bootstrap secrets manually FIRST — ArgoCD needs these before it can sync anything
kubectl apply -f argocd/bootstrap/repo-secret.yaml
kubectl apply -f argocd/bootstrap/registry-secrets/registry-secret-dev.yaml
kubectl apply -f argocd/bootstrap/registry-secrets/registry-secret-staging.yaml
kubectl apply -f argocd/bootstrap/registry-secrets/registry-secret-prod.yaml

# THE LAST MANUAL COMMAND YOU EVER RUN
# After this, root-app watches argocd/ and keeps bootstrap/ + apps/ in sync forever
kubectl apply -f argocd/root-app.yaml

# Watch all apps sync
argocd app list

# Expected output:
# NAME                SYNC      HEALTH
# root-app            Synced    Healthy
# service-a-dev       Synced    Healthy
# service-b-dev       Synced    Healthy
# service-c-dev       Synced    Healthy
# service-a-staging   Synced    Healthy
# service-b-staging   Synced    Healthy
# service-c-staging   Synced    Healthy
# service-a-prod      Synced    Healthy
# service-b-prod      Synced    Healthy
# service-c-prod      Synced    Healthy

# Verify bootstrap secrets are being managed by ArgoCD
kubectl get secret infra-platform-repo -n argocd
kubectl get secret gitlab-registry-secret -n dev

11. The Full Automated Loop

Once all steps are complete, the flow for each environment is:

── DEV ──────────────────────────────────────────────────────────────
Developer merges feature/my-feature → dev branch
         ↓
GitLab CI triggers on dev branch
  docker build → push image:abc1234 to registry
         ↓
CI patches services/service-a/overlays/dev/patch-image.yaml
  git commit "chore(service-a): dev → abc1234 [skip ci]"
         ↓
ArgoCD detects change → kustomize build → kubectl apply → dev namespace
         ↓
ArgoCD: service-a-dev = Synced + Healthy

── STAGING ──────────────────────────────────────────────────────────
Dev is stable → engineer opens MR: dev → staging (do not delete dev)
  → merge
         ↓
GitLab CI triggers on staging branch
  docker build → push image:abc1234
         ↓
CI patches services/service-a/overlays/staging/patch-image.yaml
  git commit "chore(service-a): staging → abc1234 [skip ci]"
         ↓
ArgoCD detects change → kustomize build → kubectl apply → staging namespace
         ↓
ArgoCD: service-a-staging = Synced + Healthy

── PROD ─────────────────────────────────────────────────────────────
Staging approved → engineer opens MR: staging → main (do not delete staging)
  → merge
         ↓
GitLab CI triggers on main branch
  docker build → push image:abc1234
         ↓
CI patches services/service-a/overlays/prod/patch-image.yaml
  git commit "chore(service-a): prod → abc1234 [skip ci]"
         ↓
ArgoCD detects change → kustomize build → kubectl apply → prod namespace
         ↓
ArgoCD: service-a-prod = Synced + Healthy

Tool usage summary

Stage Tool Frequency
Install ArgoCD helm install Once ever
Create namespaces + secrets kubectl create Once per namespace
First deploy of services kubectl apply -k Once per service
Bootstrap ArgoCD App of Apps kubectl apply -f root-app.yaml Once ever
Deploy to dev Merge any branch → dev Every feature
Deploy to staging Merge dev → staging When dev is stable
Deploy to prod Merge staging → main When staging is approved
Verify locally kubectl kustomize Anytime, free

12. Key Rules & Gotchas

The two ArgoCD rules

  1. Overlay folder present + ArgoCD Application CRD present = service deployed to that env
  2. No Application CRD = ArgoCD ignores the folder entirely, even if it exists

Overlay folder and Application CRD must always be added or removed together.

Credentials summary

Secret YAML file Scope Used by
ci-write deploy token GitLab CI variable INFRA_DEPLOY_TOKEN (masked) write_repository on infra-platform CI — writes image tag to infra repo
argocd-read deploy token argocd/bootstrap/repo-secret.yaml read_repository on infra-platform ArgoCD — reads K8s manifests
gitlab-registry-secret argocd/bootstrap/registry-secrets/registry-secret-*.yaml read_registry Kubernetes — pulls Docker images

CI commit rules

Rule Why
Add [skip ci] to all infra-platform commits from CI Prevents infinite CI loop
Use git diff --cached --quiet \|\| before git commit Prevents empty commit error
Mark INFRA_DEPLOY_TOKEN as Masked Never visible in logs

Branch rules

Rule Why
dev, staging, main are permanent — never delete They always reflect exactly what is running in each environment
Always uncheck "Delete source branch" on MR Prevents accidental deletion of environment branches
Feature branches are merged into dev — not staging or main directly Changes must pass through every environment in order
dev → staging → main is the only valid promotion order Guarantees staging always has what dev tested

ArgoCD sync policy

Env Branch automated: block Behaviour
dev dev present Syncs automatically on every push to dev
staging staging present Syncs automatically on every push to staging
prod main present Syncs automatically on every push to main

All 3 environments are now fully automated — no manual ArgoCD sync needed. The gate is the MR approval process between branches, not a manual click in ArgoCD.

Adding a new service

  1. Add services/service-d/base/ with its K8s manifests
  2. Add services/service-d/overlays/dev|staging|prod/
  3. Add argocd/apps/app-service-d-dev.yaml (and staging/prod)
  4. Add argocd/bootstrap/registry-secrets/registry-secret-dev.yaml for the new namespace if needed
  5. Commit to infra-platform → root-app picks up the new Application CRD automatically
  6. Create dev, staging, main branches in service-d repo
  7. Add .gitlab-ci.yml to service-d repo with SERVICE: service-d

No additional kubectl apply needed — ArgoCD handles the rest from the commit.


For questions, open an issue or MR against this wiki page.