# One Argo CD Application per directory with ApplicationSet

> Use the ApplicationSet Git generator to create an Argo CD Application for every directory in a repo, with excludes, per-app settings and safe deletion.

Published: 2026-11-26
Author: k8wiz Team
URL: https://k8wiz.com/articles/argo-cd-applicationset-one-application-per-directory

Writing one Application manifest per service works until the tenth service, when every new directory in your repo needs a matching YAML file somewhere else. The ApplicationSet Git generator removes that second step. You point it at a path pattern, and it creates one Application for each directory that matches, then deletes the Application again when the directory goes away.

This guide builds that setup from scratch on Argo CD 3.6. It covers the directory generator, Go template parameters, exclude rules, per-directory settings through the files generator and `templatePatch`, and the deletion settings you should decide on before the first directory is removed by mistake.

## Step 1: lay out the repository

The generator only looks at directory names, so the layout is the contract. Put each application in its own directory under a common parent, and keep anything that isn't an application somewhere else.

```text
platform-apps/
├── apps/
│   ├── billing/
│   │   ├── deployment.yaml
│   │   └── kustomization.yaml
│   ├── guestbook/
│   │   ├── deployment.yaml
│   │   ├── kustomization.yaml
│   │   └── service.yaml
│   └── scratch/
│       └── kustomization.yaml
└── appsets/
    └── apps.yaml
```

Each directory under `apps/` here is a Kustomize base, but a Helm chart or a folder of plain manifests works too. Argo CD detects the tool from the files in each directory when it renders the Application. The `scratch` directory stands for the experiment someone always leaves in the repo, and step 4 shows how to keep it out.

## Step 2: write the ApplicationSet

The ApplicationSet below uses one Git generator with a `directories` entry. For every directory matching `apps/*`, the generator produces a set of parameters, and the controller renders the `template` once per set.

```yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: apps
  namespace: argocd
spec:
  goTemplate: true
  goTemplateOptions: ["missingkey=error"]
  generators:
  - git:
      repoURL: https://github.com/example-org/platform-apps.git
      revision: HEAD
      directories:
      - path: apps/*
  template:
    metadata:
      name: '{{.path.basename}}'
    spec:
      project: default
      source:
        repoURL: https://github.com/example-org/platform-apps.git
        targetRevision: HEAD
        path: '{{.path.path}}'
      destination:
        server: https://kubernetes.default.svc
        namespace: '{{.path.basename}}'
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
        syncOptions:
        - CreateNamespace=true
```

With `goTemplate: true`, the directory generator gives you four parameters. `{{.path.path}}` is the matched path, such as `apps/guestbook`, which goes into `source.path`. `{{.path.basename}}` is the right-most directory name, `guestbook`, which names both the Application and its namespace. `{{index .path.segments 0}}` returns one element of the path split on slashes, and `{{.path.basenameNormalized}}` is the basename with characters that aren't valid in a Kubernetes name replaced by `-`.

Keep `missingkey=error`. Without it, a typo such as `{{.path.basname}}` is ignored silently instead of failing, and you find out when Applications appear with broken names. With the option set, the controller stops and reports the error on the ApplicationSet.

The `project` field is a fixed string on purpose. The upstream docs warn that a templated `project` lets whoever controls the Git repo choose which AppProject an Application lands in. If you need that, the repo must be under admin review, and its credentials must not be project scoped.

## Step 3: preview the Applications before you apply

The CLI can render an ApplicationSet without creating anything. The dry run fills in the ApplicationSet's status with the Applications the controller would manage, which you can then list with `jq`.

```bash
argocd appset create --dry-run ./appsets/apps.yaml -o json | jq -r '.status.resources[].name'
```

For the layout in step 1, this prints one name per directory.

```text
billing
guestbook
scratch
```

When the list looks right, create the ApplicationSet for real. Apply it with kubectl or the CLI, then check that the Applications exist.

```bash
kubectl apply -n argocd -f ./appsets/apps.yaml
kubectl get applications -n argocd
argocd appset get apps
```

If no Applications appear, read the conditions on the ApplicationSet. The controller writes generation and validation errors there with the type `ErrorOccurred`, so a missing AppProject, an unknown destination cluster or a template error shows up in this one place.

```bash
kubectl get applicationset apps -n argocd -o jsonpath='{.status.conditions}' | jq
```

## Step 4: exclude directories you don't want deployed

The directory generator already skips directories whose names start with a dot, such as `.git` or `.github`. For anything else, add a second entry to `directories` with `exclude: true`.

```yaml
      directories:
      - path: apps/*
      - path: apps/scratch
        exclude: true
```

Exclude rules always win over include rules, whatever order you list them in. A directory that matches at least one exclude pattern is dropped even if a more specific include also matches it. That means you can't exclude a whole tree and then add one directory back.

```yaml
      # This includes nothing under apps/: the exclude matches apps/web too.
      directories:
      - path: apps/web
      - path: apps/*
        exclude: true
```

Turn it around instead. Include the wide pattern and exclude the specific paths, using `path.Match` syntax when several share a shape.

```yaml
      directories:
      - path: apps/*
      - path: apps/[st]*
        exclude: true
```

## Step 5: avoid name collisions in nested layouts

Many repos group applications by team, as in `apps/team-a/web` and `apps/team-b/web`. A pattern of `apps/*/*` matches both, and `{{.path.basename}}` returns `web` for each. Application names must be unique within the Argo CD namespace, so the controller rejects the pair with a condition message like `ApplicationSet apps contains applications with duplicate name: web` and creates neither.

Build the name from more than one segment. With `apps/*/*`, segment 0 is `apps`, segment 1 is the team and the basename is the application.

```yaml
  generators:
  - git:
      repoURL: https://github.com/example-org/platform-apps.git
      revision: HEAD
      directories:
      - path: apps/*/*
  template:
    metadata:
      name: '{{index .path.segments 1}}-{{.path.basenameNormalized}}'
```

Using `basenameNormalized` here also protects you from directory names like `web_api`, which are fine in Git but not as Kubernetes object names.

## Step 6: add per-directory settings with the files generator

Sooner or later one application needs a different namespace or no automated sync. You could split the ApplicationSet, but a small config file in each directory scales better. Switch from the `directories` generator to the `files` generator, which matches files instead of directories and turns each file's contents into parameters.

Give each application a `config.yaml` next to its `kustomization.yaml`. Kustomize builds only what `kustomization.yaml` lists, so Argo CD won't try to apply this file as a manifest.

```yaml
namespace: guestbook
autoSync: true
prune: false
```

Template fields can only hold strings, so a plain template can't switch `syncPolicy.automated` on and off. That is what `templatePatch` is for. It is a Go template that renders to YAML and gets merged into each generated Application, and it only works with `goTemplate: true`.

```yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: apps
  namespace: argocd
spec:
  goTemplate: true
  goTemplateOptions: ["missingkey=error"]
  generators:
  - git:
      repoURL: https://github.com/example-org/platform-apps.git
      revision: HEAD
      files:
      - path: apps/*/config.yaml
  template:
    metadata:
      name: '{{.path.basename}}'
    spec:
      project: default
      source:
        repoURL: https://github.com/example-org/platform-apps.git
        targetRevision: HEAD
        path: '{{.path.path}}'
      destination:
        server: https://kubernetes.default.svc
        namespace: '{{.namespace}}'
  templatePatch: |
    spec:
      syncPolicy:
        syncOptions:
        - CreateNamespace=true
      {{- if .autoSync }}
        automated:
          prune: {{ .prune }}
          selfHeal: true
      {{- end }}
```

For the files generator, `{{.path.path}}` is the directory that contains the matched file and `{{.path.basename}}` is that directory's name, so the rest of the template doesn't change. A directory without a `config.yaml` no longer gets an Application, which can be a feature. Because `missingkey=error` is set, a config file that forgets `autoSync` or `prune` stops generation with an error instead of silently rendering a default.

Two limits apply to `templatePatch`. It can't change `spec.project`, and it is a patch, so an empty `spec:` key clears fields the template set. Treat the config files as trusted input, because a patch can change anything else in the Application.

## Step 7: decide what deleting a directory does

With the default policy, the ApplicationSet controller deletes an Application as soon as its directory or config file disappears from Git. Each generated Application also carries the `resources-finalizer.argocd.argoproj.io` finalizer, so Argo CD then deletes everything that Application deployed. A bad merge that removes a directory therefore removes the workload from the cluster.

Two settings in the ApplicationSet's own `syncPolicy` change that. They sit under `spec.syncPolicy`, not under `spec.template.spec.syncPolicy`.

```yaml
spec:
  syncPolicy:
    preserveResourcesOnDeletion: true
    applicationsSync: create-update
```

`preserveResourcesOnDeletion: true` stops the controller from adding the finalizer. Applications can still be deleted, but their Deployments, Services and other resources stay in the cluster. The controller only adds its finalizer when the template defines no finalizers of its own, so a template that lists finalizers explicitly gets exactly those.

`applicationsSync` limits what the controller may do to existing Applications. `create-update` lets it create and update Applications but never delete one because its directory vanished, while `create-only` also blocks updates and `create-delete` blocks updates but allows deletion. The default is `sync`, which allows all three.

The per-ApplicationSet value only counts when the controller allows overrides. If the controller runs with an explicit `--policy` flag, that flag wins and `applicationsSync` is ignored unless `--enable-policy-override` is also set. When `--policy` is left empty, which is the default install, overrides are allowed.

On its own, `applicationsSync` doesn't protect against deleting the ApplicationSet itself. Its Applications have an owner reference to it, so Kubernetes garbage collection removes them whatever the policy says. To remove an ApplicationSet and keep its Applications, delete it without cascading.

```bash
kubectl delete applicationset apps -n argocd --cascade=orphan
```

The orphaned Applications still carry the resources finalizer unless `preserveResourcesOnDeletion` was set, so deleting one of them later still removes its workload.

## Step 8: make new directories show up faster

The controller polls Git generators every three minutes by default. You can change that per generator with `requeueAfterSeconds`, or for the whole controller with the `ARGOCD_APPLICATIONSET_CONTROLLER_REQUEUE_AFTER` environment variable. The generator reads directory listings through the repo server, so its revision cache can delay new commits beyond the polling interval.

To pick up a change right away, annotate the ApplicationSet. The annotation makes the generator resolve the Git reference directly, bypassing the cache, and the controller removes it after reconciling.

```bash
kubectl annotate applicationset apps -n argocd argocd.argoproj.io/application-set-refresh=true
```

For a permanent fix, configure a GitHub or GitLab webhook that points at the ApplicationSet controller's `/api/webhook` endpoint. That endpoint belongs to the ApplicationSet webhook server, which is separate from the Argo CD API server webhook, so it needs its own Ingress.
