# diggerhq/digger architecture

> Architecture diagram of the GitHub repository diggerhq/digger, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/diggerhq/digger
- Repository: https://github.com/diggerhq/digger

## Overview

##### Project type and purpose
- This is an open-source, Go-based full-stack tool (“Digger”) providing CI/CD orchestration for Terraform. It comprises:
a. A CLI that users invoke inside their existing CI pipelines (GitHub Actions, Jenkins, etc.) to run Terraform commands.
b. An orchestrator backend (with two flavors: the original in `backend/` and a newer “next” version in `next/`), which exposes HTTP APIs, handles webhooks, schedules jobs, manages locks, and triggers CI runs.
c. A drift detection service (`drift/`) that periodically compares real infrastructure state with Terraform state.
d. Enterprise extensions (`ee/`) that add RBAC, multi-VCS support, advanced UI templates, and policy enforcement.

##### File structure
- Top-level modules:
- `cli/`: the Digger CLI implementation & platform-agnostic libs under `cli/pkg`.
- `backend/`: original orchestrator service (controllers, middleware, migrations, models, services).
- `next/`: next-generation orchestrator service (controllers, supabase integration, schema, services).
- `drift/`: drift-detection microservice.
- `ee/`: enterprise edition overrides for `backend/`, `cli/`, and `drift/`.
- `libs/`: shared libraries (locking, storage, CI backends, policy, execution, etc.).
- `dgctl/`: a control-plane CLI for ad-hoc actions against the orchestrator.
- `cli_e2e/`: end-to-end tests for the CLI.
- Configuration and deployment: Dockerfiles for each component, GitHub Actions workflows, Fly.toml for Fly.io, `atlas.hcl` for DB migrations.
- The orchestrator uses Atlas for migrations (`backend/migrations/…`) and a relational DB (Postgres, Supabase). Cloud storage for plan persistence and locking (AWS DynamoDB & S3 or equivalents).

a. External Systems
- Git hosting (GitHub, GitLab, Bitbucket) – delivers webhooks & pull-request events.
- CI/CD provider (GitHub Actions, Jenkins, etc.) – executes jobs that run the CLI.
- Cloud provider services – object storage (e.g., S3, Azure Blob, GCS) for plan artifacts; DynamoDB (or equivalent) for locking; relational DB (Postgres/Supabase) for orchestrator state.
b. Digger Components
- CLI Container (docker image invoked by CI)
- Reads `digger.yml`, calls Terraform/terragrunt, interacts with cloud stores, then POSTs status to the orchestrator API.
- Orchestrator API Service (`backend/` or `next/` service)
- HTTP server with controllers (webhooks, connections, plans, runs, policies).
- Middleware for auth (JWT, cookies, job tokens).
- Scheduler & services to enqueue CI jobs via CI-backend libraries.
- Uses Atlas migrations & models to maintain its Postgres schema.
- Drift Service (`drift/`)
- Cron-driven or continuously running Go service
- Scans Terraform states vs. real infra, writes findings back via orchestrator API or notifications.
- Enterprise Edition Extensions (`ee/`)
- Overlay controllers, policy providers, richer templates, and RBAC via OPA.

##### Relationships and data flows
1. A developer comments on a PR (e.g., “/digger plan”) in GitHub → GitHub webhook → Orchestrator API
2. Orchestrator validates the request (middleware/JWT/OPA), enqueues a job via CI-backend library (e.g., calls GitHub Actions API or Jenkins API)
3. CI runner spins up the CLI container → CLI calls Terraform/Terragrunt → CLI stores plan in S3 and lock in DynamoDB → CLI reports result back to Orchestrator API
4. Orchestrator posts a comment on the PR with plan results via GitHub API
5. Upon merge, Orchestrator triggers “apply” jobs similarly
6. Drift Service periodically polls state → detects drift → reports back to Orchestrator or via notification channels

##### Architectural patterns and principles
- Microservices separation: each Go service (CLI, orchestrator, drift) is a self-contained container
- Library reuse: CI-backends, storage, locking are factored into `libs/` for consistency across services
- Event-driven orchestration: webhooks → API → job queue → CI → callbacks
- Cloud-native persistence: RDBMS for core metadata, object store & NoSQL for plan artifacts & locks
- Configuration as code: `digger.yml`, Dockerfiles, Terraform modules

##### Technologies, frameworks, and libraries
- Go modules, Go HTTP server (`net/http`), Go templates for UI
- Atlas for DB migrations
- AWS (S3, DynamoDB), Azure/GCP equivalents
- CI providers: GitHub Actions, Jenkins, Bitbucket Pipelines (via `backend/ci_backends` and `libs/ci/*`)
- Open Policy Agent for RBAC (`middleware/`, `ee/cli/pkg/policy`)

## Diagram (Mermaid)

```mermaid
graph TB
  subgraph "User CI"
    A["Digger CLI Container"]:::cli
  end

  subgraph "Orchestrator (API Service)"
    direction TB
    B1["Original Backend"]:::api
    B2["Next-Gen Service"]:::api
    B1 -.-> B2
  end

  subgraph "Drift Service"
    D["Drift Detection Service"]:::cli
  end

  subgraph "Cloud Services"
    C1["Postgres DB"]:::persistence
    C2["S3 Plan Storage"]:::persistence
    C3["DynamoDB Lock Store"]:::persistence
  end

  subgraph "External Systems"
    E1["Git Hosting (GitHub)"]:::external
    E2["CI/CD Provider"]:::external
  end

  E1 -->|"webhook"| B1
  B1 -->|"enqueue job"| E2
  E2 -->|"runs container"| A
  A -->|"stores plan"| C2
  A -->|"stores lock"| C3
  A -->|"callback"| B1
  B1 -->|"comment on PR"| E1
  D -->|"reports drift"| B1

  click A "https://github.com/diggerhq/digger/tree/develop/cli/"
  click B1 "https://github.com/diggerhq/digger/tree/develop/backend/"
  click B2 "https://github.com/diggerhq/digger/tree/develop/next/"
  click D "https://github.com/diggerhq/digger/tree/develop/ee/drift/"

  classDef api fill:#ADD8E6,stroke:#000,stroke-width:1px
  classDef cli fill:#90EE90,stroke:#000,stroke-width:1px
  classDef persistence fill:#FFA500,stroke:#000,stroke-width:1px
  classDef external fill:#D3D3D3,stroke:#000,stroke-width:1px
  classDef ee fill:#D8BFD8,stroke:#000,stroke-width:1px
```
