badil

module
v0.33.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: Apache-2.0

README

badil

Per-pull-request preview environments for teams running Kubernetes and ArgoCD. Self-hosted, open source.

Each labelled PR gets a preview in its own namespace, named after its branch or its preview:<name> label: the services changed in the PR run at their PR version, every other service runs at its branch (default main). badil writes the desired state to your gitops repo; your ArgoCD deploys it. badil never touches your previews directly: its only cluster access is its own status ConfigMaps (badil-status-<slug>), in its own namespace, and get on namespaces by name, to refuse a new preview whose namespace exists and is not a preview.

Scope

badil has two core jobs. First, it turns pull request events into preview files in the gitops repo, under a published contract. Second, it reports on those previews through PR comments and its read API. Anything about how the workloads run, how the cluster is policed, or one-time setup is the cluster's, the charts' or the user's job.

So badil never builds images, never writes preview namespaces and holds no workload secrets; registry credentials, app secrets, quotas and network policy come from the cluster or the service charts (see Images and secrets in previews).

Why

A shared staging environment makes changes wait in line to be validated and integrated. badil applies the practices from AI Writes Code in Hours. It Still Takes Weeks to Ship.:

  • One preview per branch, or per preview:<name> label, across repos, each in its own namespace: PRs on the same branch, or with the same preview:<name> label, share one preview.
  • Changed services run at their PR commit, everything else at their branch (default main).
  • CI builds once; badil only writes desired state. It never builds images and never writes preview namespaces: it writes only its own status ConfigMaps, in its own namespace, and reads a namespace by name only before creating a preview.
  • Argo CD deploys from git.
  • Previews are torn down when their PR closes or the label is removed, and swept so they don't accumulate: the sweep also unpins open PRs whose label moved, conflicts or is gone.

How it works

  1. CI in a service repo builds and pushes the image.
  2. Adding the preview label to the PR makes badil write previews/<slug>/values.yaml (every catalog service, the PR's at its head SHA where it changes them when they declare paths; with needs in badil.yaml, only the preview's pinned services and what they need) and comment on the PR; a push to the labeled PR updates it. With paths in badil.yaml, a labelled PR pins only the services it changes. The slug is the PR's branch, slugified; a preview:<name> label picks the preview name instead.
  3. An ArgoCD ApplicationSet picks up previews/* and deploys one namespace per preview, one Application per service, and one per dependency: an upstream chart from an OCI registry or a Helm repository (postgres, redis) declared in badil.yaml, with no git repo of its own.
  4. Removing the label or closing the PR unpins its service, or deletes the file when no PR remains, and ArgoCD prunes the preview.
  5. The cleanup sweep, run in the server every CLEANUP_INTERVAL, goes over every preview: it deletes previews with no pinned service, or with no deploy within CLEANUP_TTL when set, and tears down the pins of closed PRs that step 4 missed, and of open PRs whose label moved, conflicts or is gone. It then rewrites each preview it keeps to match badil.yaml. ArgoCD prunes what it removes.

Sharing a preview across repos

To test changes from several repos together, either:

  • use the same branch name in every repo, or
  • label every PR preview:<name> (for example preview:pricing), whatever its branch. Labels are per repo: create the preview:<name> label in each repo first; badil never creates labels.

Each PR pins its own services in the preview <name> (slugified), and every other service runs at its branch (default main).

  • A named label wins over the plain preview label. A PR with two preview:<name> labels for different names gets no preview, and a comment asks to keep one.
  • Moving a PR to another label (plain to named, named to named, named to plain) unpins it from its old preview before deploying it to the new one.
  • A named preview and a branch preview with the same slug are the same preview: preview:main joins the preview of a branch called main.
  • A preview holds one PR per service: a second PR from the same repo is refused, with a comment naming the PR that holds the service. Once that PR leaves the preview, push to the refused PR or re-add its label.

Manual trigger:

POST /v1/deploy
Authorization: Bearer $BADIL_DEPLOY_TOKEN

{ "service": "backend-1", "pr_number": "42", "commit_sha": "<sha>",
  "workflow_run_url": "..." }

Sync every preview, or one, with badil.yaml now, instead of waiting for the push webhook or the next sweep:

POST /v1/sync
Authorization: Bearer $BADIL_DEPLOY_TOKEN

{}  or  { "slug": "feature-pricing" }
→ { "synced": ["feature-pricing"], "unchanged": 3, "failed": [] }

badil reads PR 42 of the service's repository from GitHub and takes its head branch, author and URL from there. A manual deploy always targets the slug of the PR's head branch, and skips the one-PR-per-service check. The cleanup sweep keeps its pin only at the PR's target preview (the slug of its single preview:<name> label, or its branch slug when it has only the plain preview label), or at its branch slug when the PR has no preview label. A PR with conflicting preview:<name> labels keeps no pin. A webhook reconcile of the PR also unpins it from its branch slug and its named-label slugs other than its target.

Install

From an empty cluster to a first preview: docs/INSTALL.md.

The badil CLI (badil list, badil wait) for CI jobs and agents: Install the badil CLI.

The gitops repo can start from the template badilhq/gitops-template.

Status

Pre-1.0, and the v1 scope is complete. The full loop (label a PR, preview deployed by Argo CD, URLs tested, merge, teardown) runs end to end on a k3d testbed with a four-service demo.

Working: label-triggered previews per branch or per preview:<name> label across repos, per-service chart overrides, upstream chart dependencies, teardown when a PR closes or loses its label, an in-server cleanup sweep with optional TTL, the manual /v1/deploy endpoint, previews synced when badil.yaml changes (push webhook, or /v1/sync), PR comments with preview links, Prometheus metrics and a Grafana dashboard, preview status and spin-up time from the renderer's status callback, and GitHub deployments through Argo CD notifications.

Until 1.0, env vars, metric names, annotations and chart values can still change between minor releases; the CHANGELOG marks breaking changes.

Usage

badil serve           # API + webhook server
badil list            # list previews and their status
badil wait            # wait for a PR's or a slug's preview to be ready
badil version         # print the version (also badil --version)

badil reads its whole preview config from badil.yaml in the gitops repo (service catalog, chart settings, image tag format, dependencies, namespace metadata, preview block) and writes everything preview-apps needs into each preview file (see the Configuration reference).

The server's environment variables are listed in the Configuration reference.

Local setup: DEVELOPMENT.md.

Charts

Chart Purpose
charts/badil Installs the badil server; the cleanup sweep runs in the server hourly by default
charts/preview-apps One preview: AppProject, Namespace, one ArgoCD Application per service and per dependency

Install: docs/INSTALL.md; ArgoCD wiring: examples/argocd. Charts are published to oci://ghcr.io/badilhq/charts; see CONTRIBUTING.md for how versions are set.

Docs

License

Apache-2.0

Directories

Path Synopsis
cmd
badil command
fakegithub command
Command fakegithub serves a mock of the GitHub API badil calls, with a control API that changes its state and sends signed webhooks to badil.
Command fakegithub serves a mock of the GitHub API badil calls, with a control API that changes its state and sends signed webhooks to badil.
schemagen command
Command schemagen writes the JSON Schema of the preview-apps chart's values, generated from internal/chartvalues.Values, and the JSON Schema of the preview values contract, generated from internal/preview.Values.
Command schemagen writes the JSON Schema of the preview-apps chart's values, generated from internal/chartvalues.Values, and the JSON Schema of the preview values contract, generated from internal/preview.Values.
internal
api
Package api holds the JSON bodies of badil's HTTP API, shared by the server and its clients.
Package api holds the JSON bodies of badil's HTTP API, shared by the server and its clients.
apiclient
Package apiclient is the HTTP client of badil's API that the badil CLI uses.
Package apiclient is the HTTP client of badil's API that the badil CLI uses.
app
Package app holds badil's application services: the background job queue that runs reconciles, the latest reconcile outcome of each PR, the wake signal of the waits, the lifecycle APIs built over the gitops store and the forge, the handling of the pull_request and push events, the config syncs, the intake of the renderer status events, the manual deploys, the read model of the previews and the evaluation of the waits.
Package app holds badil's application services: the background job queue that runs reconciles, the latest reconcile outcome of each PR, the wake signal of the waits, the lifecycle APIs built over the gitops store and the forge, the handling of the pull_request and push events, the config syncs, the intake of the renderer status events, the manual deploys, the read model of the previews and the evaluation of the waits.
archtest
Package archtest holds the test of the dependency rule of docs/ARCHITECTURE.md on every package's transitive imports.
Package archtest holds the test of the dependency rule of docs/ARCHITECTURE.md on every package's transitive imports.
catalog
Package catalog parses and validates badil.yaml, the services and dependencies of the gitops repo and their preview settings.
Package catalog parses and validates badil.yaml, the services and dependencies of the gitops repo and their preview settings.
chartvalues
Package chartvalues holds the Go types of the preview-apps chart's values, from which cmd/schemagen generates charts/preview-apps/values.schema.json.
Package chartvalues holds the Go types of the preview-apps chart's values, from which cmd/schemagen generates charts/preview-apps/values.schema.json.
expiry
Package expiry keeps the PRs of the previews the sweep expired for the TTL (lifecycle.ExpiryStore) in memory.
Package expiry keeps the PRs of the previews the sweep expired for the TTL (lifecycle.ExpiryStore) in memory.
expiry/kube
Package kube keeps the PRs of the previews the sweep expired for the TTL (lifecycle.ExpiryStore) in the ConfigMap badil-expired of badil's namespace, written by the server.
Package kube keeps the PRs of the previews the sweep expired for the TTL (lifecycle.ExpiryStore) in the ConfigMap badil-expired of badil's namespace, written by the server.
fakegithub
Package fakegithub is a mock of the GitHub REST API badil calls, served under /api/v3/ from in-memory state, with a control API under /_mock/ that changes the state and sends the matching signed webhook to badil.
Package fakegithub is a mock of the GitHub REST API badil calls, served under /api/v3/ from in-memory state, with a control API under /_mock/ that changes the state and sends the matching signed webhook to badil.
forge
Package forge defines what badil needs from the forge hosting the service repos: pull request reads, the preview comment and webhook deliveries, through neutral types.
Package forge defines what badil needs from the forge hosting the service repos: pull request reads, the preview comment and webhook deliveries, through neutral types.
forge/github
Package github implements forge.Forge and forge.WebhookParser over the GitHub API, authenticated as the GitHub App installation.
Package github implements forge.Forge and forge.WebhookParser over the GitHub API, authenticated as the GitHub App installation.
forge/github/githubtest
Package githubtest gives tests the GitHub forge's default repo and pull request URL rules, on https://github.com and without a client.
Package githubtest gives tests the GitHub forge's default repo and pull request URL rules, on https://github.com and without a client.
github
Package github provides GitHub App authentication, webhooks, and API client.
Package github provides GitHub App authentication, webhooks, and API client.
lifecycle
Package lifecycle drives previews: it deploys and tears down a pull request's part of its preview, and sweeps every preview of the gitops repo.
Package lifecycle drives previews: it deploys and tears down a pull request's part of its preview, and sweeps every preview of the gitops repo.
metrics
Package metrics holds badil's Prometheus collectors in their own registry.
Package metrics holds badil's Prometheus collectors in their own registry.
namespace
Package namespace tells whether the namespace of a new preview is free for badil to deploy into: absent, or already a preview namespace of that slug.
Package namespace tells whether the namespace of a new preview is free for badil to deploy into: absent, or already a preview namespace of that slug.
preview
Package preview provides slug generation and values file building.
Package preview provides slug generation and values file building.
server
Package server provides HTTP routes and webhook handlers.
Package server provides HTTP routes and webhook handlers.
status
Package status keeps the latest status the renderer reported for each service of each preview.
Package status keeps the latest status the renderer reported for each service of each preview.
status/kube
Package kube keeps the status of the previews (package status) in one ConfigMap per preview in badil's namespace.
Package kube keeps the status of the previews (package status) in one ConfigMap per preview in badil's namespace.
store
Package store reads and writes the gitops repository: badil.yaml and the preview values files.
Package store reads and writes the gitops repository: badil.yaml and the preview values files.
store/storetest
Package storetest provides bare gitops repositories for tests of the store and of its callers: remotes a store clones, fetches from and pushes to, in-process, with no git binary.
Package storetest provides bare gitops repositories for tests of the store and of its callers: remotes a store clones, fetches from and pushes to, in-process, with no git binary.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL