> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ubiquex.io/llms.txt
> Use this file to discover all available pages before exploring further.

# sync block

> Pull-based GitOps deployment via ArgoCD or Flux.

The `sync` block declares a GitOps sync target. ubx creates the CRD; a GitOps controller (ArgoCD, Flux) reconciles the actual deployment. Infra outputs are passed as parameters.

## Syntax

```hcl theme={"theme":"css-variables"}
sync "controller_type" "name" {
  # attributes
}
```

## Supported Types

| Type     | CRD                                                     |
| -------- | ------------------------------------------------------- |
| `argocd` | ArgoCD `Application` (`argoproj.io/v1alpha1`)           |
| `flux`   | Flux `Kustomization` (`kustomize.toolkit.fluxcd.io/v1`) |

## ArgoCD Example

```hcl theme={"theme":"css-variables"}
sync "argocd" "backend" {
  repo      = "https://github.com/my-org/app-config"
  path      = "apps/backend"
  target    = "https://kubernetes.default.svc"
  namespace = "argocd"
  values = {
    db_host = ~unit.aws_rds_instance.db.endpoint
    db_port = "5432"
  }
}
```

Generated TypeScript:

```typescript theme={"theme":"css-variables"}
const backend = new k8s.apiextensions.CustomResource("backend", {
    apiVersion: "argoproj.io/v1alpha1",
    kind: "Application",
    metadata: { name: "backend", namespace: "argocd" },
    spec: {
        source: { repoURL: "https://github.com/my-org/app-config", path: "apps/backend" },
        destination: { server: "https://kubernetes.default.svc" },
        helm: {
            parameters: pulumi.all([db.endpoint]).apply(([_dep0]) => ([
                { name: "dbHost", value: _dep0 },
                { name: "dbPort", value: "5432" },
            ]))
        },
    },
} as any);
```

## Attribute Mapping

| ubx attribute | ArgoCD field                                |
| ------------- | ------------------------------------------- |
| `repo`        | `spec.source.repoURL`                       |
| `path`        | `spec.source.path`                          |
| `target`      | `spec.destination.server`                   |
| `namespace`   | `metadata.namespace`                        |
| `values`      | `spec.helm.parameters` as `[{name, value}]` |

## Flux Example

```hcl theme={"theme":"css-variables"}
sync "flux" "infra" {
  repo      = "https://github.com/my-org/fleet-infra"
  path      = "./clusters/prod"
  namespace = "flux-system"
  values = {
    vpc_id = ~unit.aws_vpc.main.id
  }
}
```

## Meta-Arguments

```hcl theme={"theme":"css-variables"}
sync "argocd" "backend" {
  repo       = "https://github.com/my-org/app-config"
  path       = "apps/backend"
  target     = "https://kubernetes.default.svc"
  namespace  = "argocd"
  depends_on = [unit.aws_rds_instance.db]
  when       = input.env == "prod"
}
```

| Argument     | Type            | Description                                                                                                                         |
| ------------ | --------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| `count`      | `number`        | Create N CRD instances. Use `count.index` for per-instance values. Mutually exclusive with `for_each`.                              |
| `for_each`   | `list` or `map` | Create one instance per element. Use `each.key` / `each.value`. Mutually exclusive with `count`.                                    |
| `when`       | `bool` expr     | Conditionally create the sync target. Resolved `bool` → `if (cond) { ... }`. Pending → `.apply((_when) => { if (_when) { ... } })`. |
| `depends_on` | `list` of refs  | Explicit dependency not inferred from `~` refs.                                                                                     |

## count

Create multiple sync targets from one block. Each instance is keyed by index.

```hcl theme={"theme":"css-variables"}
sync "argocd" "apps" {
  count = 3
  repo  = "https://github.com/my-org/app-config"
  path  = "apps/instance-${count.index}"
}
```

Generated TypeScript:

```typescript theme={"theme":"css-variables"}
const apps: {[key: string]: k8s.apiextensions.CustomResource} = {};
[...Array(3)].forEach((_, idx) => {
    apps[String(idx)] = new k8s.apiextensions.CustomResource(`apps-${idx}`, {
        apiVersion: "argoproj.io/v1alpha1",
        kind: "Application",
        metadata: { name: `apps-${idx}` },
        spec: {
            source: { repoURL: "https://github.com/my-org/app-config", path: `apps/instance-${idx}` },
        },
    } as any);
});
```

`count` and `for_each` are mutually exclusive on the same block.

## when

Conditionally create a sync target. The value must be a boolean expression.

```hcl theme={"theme":"css-variables"}
input "env" { default = "dev" }

sync "argocd" "app" {
  when = input.env == "prod"
  repo = "https://github.com/my-org/config"
  path = "k8s/"
}
```

Generated TypeScript (resolved `when`):

```typescript theme={"theme":"css-variables"}
if ((env === "prod")) {
    const app = new k8s.apiextensions.CustomResource("app", {
        apiVersion: "argoproj.io/v1alpha1",
        kind: "Application",
        metadata: { name: "app" },
        spec: {
            source: { repoURL: "https://github.com/my-org/config", path: "k8s/" },
        },
    } as any);
}
```

When the `when` condition references a `Pending<T>` value, ubx wraps the block in `.apply((_when) => { if (_when) { ... } })` instead.
